Get text similarity level with Damerau-Levenshtein distance.
- PHP 7.1.0 or greater.
composer require oefenweb/damerau-levenshtein
$pattern = 'foo bar';
$string = 'fuu baz';
$damerauLevenshtein = new DamerauLevenshtein($pattern, $string);
$damerauLevenshtein->getSimilarity(); // absolute edit distance; == 3
$damerauLevenshtein->getRelativeDistance(); // relative edit distance; == 0.57142857142857
$damerauLevenshtein->getMatrix(); // get complete distance matrix
/* ==
* [
* [0,1,2,3,4,5,6,7],
* [1,0,1,2,3,4,5,6],
* [2,1,1,2,3,4,5,6],
* [3,2,2,2,3,4,5,6],
* [4,3,3,3,2,3,4,5],
* [5,4,4,4,3,2,3,4],
* [6,5,5,5,4,3,2,3],
* [7,6,6,6,5,4,3,3],
* ]
*/
$damerauLevenshtein->displayMatrix(); // get readable and formatted distance matrix
/*
* ' foo bar' . PHP_EOL
* . ' 01234567' . PHP_EOL
* . 'f10123456' . PHP_EOL
* . 'u21123456' . PHP_EOL
* . 'u32223456' . PHP_EOL
* . ' 43332345' . PHP_EOL
* . 'b54443234' . PHP_EOL
* . 'a65554323' . PHP_EOL
* . 'z76665433'
*/
Different costs are supported by the constructor and getters / setters.
Character comparison (equal check) can easily be overridden by parent class (see DamerauLevenshtein::compare
).
For more examples look at /tests/DamerauLevenshteinTest.php
or RTFC.
MIT
Mischa ter Smitten (based on work of Ph4r05)