Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight stop words if they appear in the query #132

Merged
merged 11 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix style issues
Signed-off-by: Philipp Daun <[email protected]>
  • Loading branch information
daun committed Dec 7, 2024
commit ef2508b7c24d33a93475d80191fe6aec34bda048
77 changes: 38 additions & 39 deletions src/Internal/Search/Highlighter/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,45 +72,6 @@ public function highlight(
return new HighlightResult($highlightedText, $matches);
}

/**
* @param array<array{start:int, length:int}> $matches
* @return array<array{start:int, length:int}> $matches
*/
private function removeStopWordMatches(array $matches): array
{
$maxCharDistance = 1;
$maxWordDistance = 1;

foreach ($matches as $i => $match) {
if (!$match['stopword']) {
continue;
}

$hasNonStopWordNeighbor = false;

for ($j = 1; $j <= $maxWordDistance; $j++) {
$prevMatch = $matches[$i - $j] ?? null;
$nextMatch = $matches[$i + $j] ?? null;

// Keep stopword matches between non-stopword matches of interest
$hasNonStopWordNeighbor = $hasNonStopWordNeighbor
|| ($prevMatch && $prevMatch['stopword'] === false && ($prevMatch['start'] + $prevMatch['length']) >= $match['start'] - $maxCharDistance)
|| ($nextMatch && $nextMatch['stopword'] === false && $nextMatch['start'] <= $match['start'] + $match['length'] + $maxCharDistance);

if ($hasNonStopWordNeighbor) {
break;
}

}

if (!$hasNonStopWordNeighbor) {
unset($matches[$i]);
}
}

return $matches;
}

/**
* @param array<array{start:int, length:int}> $matches
* @return array{starts: array<int>, ends: array<int>}
Expand Down Expand Up @@ -197,4 +158,42 @@ private function matches(Token $textToken, TokenCollection $queryTokens): bool

return false;
}

/**
* @param array<array{start:int, length:int}> $matches
* @return array<array{start:int, length:int}> $matches
*/
private function removeStopWordMatches(array $matches): array
{
$maxCharDistance = 1;
$maxWordDistance = 1;

foreach ($matches as $i => $match) {
if (!$match['stopword']) {
continue;
}

$hasNonStopWordNeighbor = false;

for ($j = 1; $j <= $maxWordDistance; $j++) {
$prevMatch = $matches[$i - $j] ?? null;
$nextMatch = $matches[$i + $j] ?? null;

// Keep stopword matches between non-stopword matches of interest
$hasNonStopWordNeighbor = $hasNonStopWordNeighbor
|| ($prevMatch && $prevMatch['stopword'] === false && ($prevMatch['start'] + $prevMatch['length']) >= $match['start'] - $maxCharDistance)
|| ($nextMatch && $nextMatch['stopword'] === false && $nextMatch['start'] <= $match['start'] + $match['length'] + $maxCharDistance);

if ($hasNonStopWordNeighbor) {
break;
}
}

if (!$hasNonStopWordNeighbor) {
unset($matches[$i]);
}
}

return $matches;
}
}
2 changes: 1 addition & 1 deletion src/Internal/Tokenizer/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private function doTokenize(string $string, ?string $language, ?int $maxTokens =
}
}

$stopword = in_array($term, $stopWords);
$stopword = \in_array($term, $stopWords, true);

$token = new Token(
$id++,
Expand Down