diff --git a/src/TNTFuzzyMatch.php b/src/TNTFuzzyMatch.php index 8c42562..212e823 100644 --- a/src/TNTFuzzyMatch.php +++ b/src/TNTFuzzyMatch.php @@ -94,6 +94,8 @@ public function fuzzyMatchFromFile($pattern, $path) if ($lines) { while (!feof($lines)) { $line = fgets($lines, 4096); + $line = str_replace("\r", "", $line); + $line = str_replace("\n", "", $line); if ($this->hasCommonSubsequence($pattern, $line)) { $res[] = $line; } @@ -118,7 +120,6 @@ public function fuzzyMatchFromFile($pattern, $path) } arsort($sorted); - return $sorted; } @@ -140,7 +141,12 @@ public function fuzzyMatch($pattern, $items) $wordVector = $this->wordToVector($word); $normalizedPaternVector = $this->makeVectorSameLength($wordVector, $paternVector); - $angle = $this->angleBetweenVectors($wordVector, $normalizedPaternVector); + $angle = $this->angleBetweenVectors($wordVector, $normalizedPaternVector); + + if (strpos($word, $pattern) !== false) { + $angle += 0.2; + } + $sorted[$word] = $angle; } diff --git a/tests/TNTFuzzyMatchTest.php b/tests/TNTFuzzyMatchTest.php index 8c16203..dd6725a 100644 --- a/tests/TNTFuzzyMatchTest.php +++ b/tests/TNTFuzzyMatchTest.php @@ -72,39 +72,43 @@ public function testFuzzyMatchFromFile() { $res = $this->fm->fuzzyMatchFromFile('search', __DIR__.'/_files/english_wordlist_2k.txt'); - $this->assertEquals([ - 'search' => 1, - 'research' => 0.86602345529065 - ], $res); + $equal = bccomp($res['search'], 1.2, 2); + $this->assertEquals(0, $equal); + + $equal = bccomp($res['research'], 1.06, 2); + $this->assertEquals(0, $equal); } public function testFuzzyMatchFromFileFunction() { $res = fuzzyMatchFromFile('search', __DIR__.'/_files/english_wordlist_2k.txt'); - $this->assertEquals([ - 'search' => 1, - 'research' => 0.86602345529065 - ], $res); + $equal = bccomp($res['search'], 1.2, 2); + $this->assertEquals(0, $equal); + + $equal = bccomp($res['research'], 1.06, 2); + $this->assertEquals(0, $equal); } public function testFuzzyMatch() { $res = $this->fm->fuzzyMatch('search', ['search', 'research', 'something']); - $this->assertEquals([ - 'search' => 1, - 'research' => 0.86602345529065 - ], $res); + $equal = bccomp($res['search'], 1.2, 2); + $this->assertEquals(0, $equal); + + $equal = bccomp($res['research'], 1.06, 2); + $this->assertEquals(0, $equal); } public function testFuzzyMatchFunction() { $res = fuzzyMatch('search', ['search', 'research', 'something']); - $this->assertEquals([ - 'search' => 1, - 'research' => 0.86602345529065 - ], $res); + $equal = bccomp($res['search'], 1.2, 2); + $this->assertEquals(0, $equal); + + $equal = bccomp($res['research'], 1.06, 2); + $this->assertEquals(0, $equal); } }