-
-
Notifications
You must be signed in to change notification settings - Fork 569
Align Utils::suggestionList()
with the reference implementation
#1075
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0080423
Align `Utils::suggestionList()` with the reference implementation
vhenzl 3202bc4
Add changelog
vhenzl 9189132
Merge branch 'master' into align-suggestion-list
vhenzl 858ac32
Fix typo
vhenzl 38bffd7
Add comments
vhenzl c1e7e99
Fix tests
vhenzl 18da3dd
Fix `suggestionList` to always return string values
vhenzl a325b81
Rename variable in a loop
vhenzl 2e4bccb
Remove unnecessary initialisation
vhenzl ea1c8d8
Merge branch 'master' into align-suggestion-list
vhenzl cb5f979
Add comment about `measure()` returning `int|null`
vhenzl b455fbb
Remove unnecessary class field
vhenzl 7251b80
Apply php-cs-fixer changes
vhenzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace GraphQL\Utils; | ||
|
||
/** | ||
* Computes the lexical distance between strings A and B. | ||
* | ||
* The "distance" between two strings is given by counting the minimum number | ||
* of edits needed to transform string A into string B. An edit can be an | ||
* insertion, deletion, or substitution of a single character, or a swap of two | ||
* adjacent characters. | ||
* | ||
* Includes a custom alteration from Damerau-Levenshtein to treat case changes | ||
* as a single edit which helps identify mis-cased values with an edit distance | ||
* of 1. | ||
* | ||
* This distance can be useful for detecting typos in input or sorting | ||
* | ||
* Unlike the native levenshtein() function that always returns int, LexicalDistance::measure() returns int|null. | ||
* It takes into account the threshold and returns null if the measured distance is bigger. | ||
*/ | ||
class LexicalDistance | ||
{ | ||
private string $input; | ||
|
||
private string $inputLowerCase; | ||
|
||
/** | ||
* List of char codes in the input string. | ||
* | ||
* @var array<int> | ||
*/ | ||
private array $inputArray; | ||
|
||
public function __construct(string $input) | ||
{ | ||
$this->input = $input; | ||
$this->inputLowerCase = \strtolower($input); | ||
$this->inputArray = self::stringToArray($this->inputLowerCase); | ||
} | ||
|
||
public function measure(string $option, float $threshold): ?int | ||
{ | ||
if ($this->input === $option) { | ||
return 0; | ||
} | ||
|
||
$optionLowerCase = \strtolower($option); | ||
|
||
// Any case change counts as a single edit | ||
if ($this->inputLowerCase === $optionLowerCase) { | ||
return 1; | ||
} | ||
|
||
$a = self::stringToArray($optionLowerCase); | ||
$b = $this->inputArray; | ||
|
||
if (\count($a) < \count($b)) { | ||
$tmp = $a; | ||
$a = $b; | ||
$b = $tmp; | ||
} | ||
|
||
$aLength = \count($a); | ||
$bLength = \count($b); | ||
|
||
if ($aLength - $bLength > $threshold) { | ||
return null; | ||
} | ||
|
||
/** @var array<array<int>> $rows */ | ||
$rows = []; | ||
for ($i = 0; $i <= $bLength; ++$i) { | ||
$rows[0][$i] = $i; | ||
} | ||
|
||
for ($i = 1; $i <= $aLength; ++$i) { | ||
$upRow = &$rows[($i - 1) % 3]; | ||
$currentRow = &$rows[$i % 3]; | ||
|
||
$smallestCell = ($currentRow[0] = $i); | ||
for ($j = 1; $j <= $bLength; ++$j) { | ||
$cost = $a[$i - 1] === $b[$j - 1] ? 0 : 1; | ||
|
||
$currentCell = \min( | ||
$upRow[$j] + 1, // delete | ||
$currentRow[$j - 1] + 1, // insert | ||
$upRow[$j - 1] + $cost, // substitute | ||
); | ||
|
||
if ($i > 1 && $j > 1 && $a[$i - 1] === $b[$j - 2] && $a[$i - 2] === $b[$j - 1]) { | ||
// transposition | ||
$doubleDiagonalCell = $rows[($i - 2) % 3][$j - 2]; | ||
$currentCell = \min($currentCell, $doubleDiagonalCell + 1); | ||
} | ||
|
||
if ($currentCell < $smallestCell) { | ||
$smallestCell = $currentCell; | ||
} | ||
|
||
$currentRow[$j] = $currentCell; | ||
} | ||
|
||
// Early exit, since distance can't go smaller than smallest element of the previous row. | ||
if ($smallestCell > $threshold) { | ||
return null; | ||
} | ||
} | ||
|
||
$distance = $rows[$aLength % 3][$bLength]; | ||
|
||
return $distance <= $threshold ? $distance : null; | ||
} | ||
|
||
/** | ||
* Returns a list of char codes in the given string. | ||
* | ||
* @return array<int> | ||
vhenzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private static function stringToArray(string $str): array | ||
{ | ||
$array = []; | ||
foreach (\mb_str_split($str) as $char) { | ||
$array[] = \mb_ord($char); | ||
} | ||
|
||
return $array; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.