Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 19 additions & 12 deletions src/Search/Bitap/computeScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

namespace Fuse\Search\Bitap;

use function Fuse\Core\config;

function computeScore(string $pattern, array $options = [])
{
$errors = $options['errors'] ?? 0;
$currentLocation = $options['currentLocation'] ?? 0;
$expectedLocation = $options['expectedLocation'] ?? 0;
$distance = $options['distance'] ?? config('distance');
$ignoreLocation = $options['ignoreLocation'] ?? config('ignoreLocation');

/**
* @param string $pattern
* @param int $errors
* @param int $currentLocation
* @param int $expectedLocation
* @param int $distance
* @param bool $ignoreLocation
* @return float
*/
function computeScore(
$pattern,
$errors,
$currentLocation,
$expectedLocation,
$distance,
$ignoreLocation
) {
$accuracy = $errors / mb_strlen($pattern);

if ($ignoreLocation) {
Expand All @@ -20,9 +27,9 @@ function computeScore(string $pattern, array $options = [])

$proximity = abs($expectedLocation - $currentLocation);

if (!$distance) {
if ($distance === 0) {
// Dodge divide by zero error.
return $proximity ? 1.0 : $accuracy;
return $proximity === 0 ? 1.0 : $accuracy;
}

return $accuracy + $proximity / $distance;
Expand Down
31 changes: 4 additions & 27 deletions src/Search/Bitap/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ function search(string $text, string $pattern, array $patternAlphabet, array $op

// Get all exact matches, here for speed up
while (($index = mb_strpos($text, $pattern, $bestLocation)) !== false) {
$score = computeScore($pattern, [
'currentLocation' => $index,
'expectedLocation' => $expectedLocation,
'distance' => $distance,
'ignoreLocation' => $ignoreLocation,
]);
$score = computeScore($pattern, 0, $index, $expectedLocation, $distance, $ignoreLocation);

$currentThreshold = min($score, $currentThreshold);
$bestLocation = $index + $patternLen;
Expand Down Expand Up @@ -85,13 +80,7 @@ function search(string $text, string $pattern, array $patternAlphabet, array $op
$binMid = $binMax;

while ($binMin < $binMid) {
$score = computeScore($pattern, [
'errors' => $i,
'currentLocation' => $expectedLocation + $binMid,
'expectedLocation' => $expectedLocation,
'distance' => $distance,
'ignoreLocation' => $ignoreLocation,
]);
$score = computeScore($pattern, $i, $expectedLocation + $binMid, $expectedLocation, $distance, $ignoreLocation);

if ($score <= $currentThreshold) {
$binMin = $binMid;
Expand Down Expand Up @@ -136,13 +125,7 @@ function search(string $text, string $pattern, array $patternAlphabet, array $op
}

if ($bitArr[$j] & $mask) {
$finalScore = computeScore($pattern, [
'errors' => $i,
'currentLocation' => $currentLocation,
'expectedLocation' => $expectedLocation,
'distance' => $distance,
'ignoreLocation' => $ignoreLocation,
]);
$finalScore = computeScore($pattern, $i, $currentLocation, $expectedLocation, $distance, $ignoreLocation);

// This match will almost certainly be better than any existing match.
// But check anyway.
Expand All @@ -163,13 +146,7 @@ function search(string $text, string $pattern, array $patternAlphabet, array $op
}

// No hope for a (better) match at greater error levels.
$score = computeScore($pattern, [
'errors' => $i + 1,
'currentLocation' => $expectedLocation,
'expectedLocation' => $expectedLocation,
'distance' => $distance,
'ignoreLocation' => $ignoreLocation,
]);
$score = computeScore($pattern, $i + 1, $expectedLocation, $expectedLocation, $distance, $ignoreLocation);

if ($score > $currentThreshold) {
break;
Expand Down