diff --git a/src/Search/Bitap/computeScore.php b/src/Search/Bitap/computeScore.php index 5adc874..ebaa308 100644 --- a/src/Search/Bitap/computeScore.php +++ b/src/Search/Bitap/computeScore.php @@ -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) { @@ -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; diff --git a/src/Search/Bitap/search.php b/src/Search/Bitap/search.php index 2b09d8e..ac00b3b 100644 --- a/src/Search/Bitap/search.php +++ b/src/Search/Bitap/search.php @@ -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; @@ -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; @@ -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. @@ -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;