-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8b2b1a
commit 2009e0c
Showing
6 changed files
with
254 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,62 @@ | ||
<?php | ||
|
||
namespace Designbycode\FuzzySearch; | ||
|
||
class LevenshteinDistance { | ||
/** | ||
* Calculate the Levenshtein distance between two strings. | ||
* | ||
* The Levenshtein distance is a measure of the minimum number of single-character edits | ||
* (insertions, deletions or substitutions) required to change one word into the other. | ||
* | ||
* @param string $str1 The first string. | ||
* @param string $str2 The second string. | ||
* @return int The Levenshtein distance between the two strings. | ||
*/ | ||
public static function calculate(string $str1, string $str2): int | ||
{ | ||
// Create a 2D array to store the distances between substrings of $str1 and $str2 | ||
$distanceMatrix = array(); | ||
|
||
// Initialize the first row and column of the matrix | ||
$str1Length = strlen($str1); | ||
$str2Length = strlen($str2); | ||
for ($i = 0; $i <= $str1Length; $i++) { | ||
$distanceMatrix[$i][0] = $i; // Distance from empty string to $str1 substrings | ||
} | ||
for ($j = 0; $j <= $str2Length; $j++) { | ||
$distanceMatrix[0][$j] = $j; // Distance from empty string to $str2 substrings | ||
} | ||
namespace Designbycode\FuzzySearch; | ||
|
||
// Iterate through the characters of $str1 and $str2 | ||
for ($i = 1; $i <= $str1Length; $i++) { | ||
for ($j = 1; $j <= $str2Length; $j++) { | ||
// Calculate the cost of substitution (0 if characters match, 1 if they don't) | ||
$substitutionCost = ($str1[$i - 1] == $str2[$j - 1]) ? 0 : 1; | ||
use TypeError; | ||
|
||
// Calculate the minimum distance between the current substrings | ||
$insertionDistance = $distanceMatrix[$i - 1][$j] + 1; // Insert a character into $str1 | ||
$deletionDistance = $distanceMatrix[$i][$j - 1] + 1; // Delete a character from $str1 | ||
$substitutionDistance = $distanceMatrix[$i - 1][$j - 1] + $substitutionCost; // Substitute a character in $str1 | ||
class LevenshteinDistance | ||
{ | ||
/** | ||
* Calculate the Levenshtein distance between two strings. | ||
* | ||
* The Levenshtein distance is a measure of the minimum number of single-character edits | ||
* (insertions, deletions or substitutions) required to change one word into the other. | ||
* | ||
* @param string $str1 The first string. | ||
* @param string $str2 The second string. | ||
* @return int The Levenshtein distance between the two strings. | ||
*/ | ||
public static function calculate(mixed $str1, mixed $str2): int | ||
{ | ||
|
||
// Choose the minimum distance | ||
$distanceMatrix[$i][$j] = min($insertionDistance, $deletionDistance, $substitutionDistance); | ||
} | ||
} | ||
if (!is_string($str1)) { | ||
throw new TypeError('Argument 1 passed to LevenshteinDistance::calculate() must be of the type string'); | ||
} | ||
|
||
if (!is_string($str2)) { | ||
throw new TypeError('Argument 2 passed to LevenshteinDistance::calculate() must be of the type string'); | ||
} | ||
|
||
// Create a 2D array to store the distances between substrings of $str1 and $str2 | ||
$distanceMatrix = []; | ||
|
||
// Return the Levenshtein distance between the entire strings | ||
return $distanceMatrix[$str1Length][$str2Length]; | ||
// Initialize the first row and column of the matrix | ||
$str1Length = strlen($str1); | ||
$str2Length = strlen($str2); | ||
for ($i = 0; $i <= $str1Length; $i++) { | ||
$distanceMatrix[$i][0] = $i; // Distance from empty string to $str1 substrings | ||
} | ||
for ($j = 0; $j <= $str2Length; $j++) { | ||
$distanceMatrix[0][$j] = $j; // Distance from empty string to $str2 substrings | ||
} | ||
|
||
// Iterate through the characters of $str1 and $str2 | ||
for ($i = 1; $i <= $str1Length; $i++) { | ||
for ($j = 1; $j <= $str2Length; $j++) { | ||
// Calculate the cost of substitution (0 if characters match, 1 if they don't) | ||
$substitutionCost = ($str1[$i - 1] == $str2[$j - 1]) ? 0 : 1; | ||
|
||
// Calculate the minimum distance between the current substrings | ||
$insertionDistance = $distanceMatrix[$i - 1][$j] + 1; // Insert a character into $str1 | ||
$deletionDistance = $distanceMatrix[$i][$j - 1] + 1; // Delete a character from $str1 | ||
$substitutionDistance = $distanceMatrix[$i - 1][$j - 1] + $substitutionCost; // Substitute a character in $str1 | ||
|
||
// Choose the minimum distance | ||
$distanceMatrix[$i][$j] = min($insertionDistance, $deletionDistance, $substitutionDistance); | ||
} | ||
} | ||
|
||
// Return the Levenshtein distance between the entire strings | ||
return $distanceMatrix[$str1Length][$str2Length]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
<?php | ||
|
||
use Designbycode\FuzzySearch\FuzzySearch; | ||
use Designbycode\FuzzySearch\FuzzySearch; | ||
|
||
test('search returns similar matches', function () { | ||
$index = ['apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('aple'); | ||
expect($results)->toEqual(['apple']); | ||
}); | ||
test('search returns similar matches', function () { | ||
$index = ['apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('aple'); | ||
expect($results)->toEqual(['apple']); | ||
}); | ||
|
||
test('search returns no results for non-existent query', function () { | ||
$index = ['apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('xyz'); | ||
expect($results)->toEqual([]); | ||
}); | ||
test('search returns no results for non-existent query', function () { | ||
$index = ['apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('xyz'); | ||
expect($results)->toEqual([]); | ||
}); | ||
|
||
test('search returns exact match', function () { | ||
$index = ['apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('apple'); | ||
expect($results)->toEqual(['apple']); | ||
}); | ||
test('search returns exact match', function () { | ||
$index = ['apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('apple'); | ||
expect($results)->toEqual(['apple']); | ||
}); | ||
|
||
test('search is case-insensitive', function () { | ||
$index = ['Apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('aPpLe'); | ||
expect($results)->toEqual(['Apple']); | ||
}); | ||
test('search is case-insensitive', function () { | ||
$index = ['Apple', 'banana', 'orange', 'grape', 'pear']; | ||
$fuzzySearch = new FuzzySearch($index); | ||
$results = $fuzzySearch->search('aPpLe'); | ||
expect($results)->toEqual(['Apple']); | ||
}); |
Oops, something went wrong.