datingvip / damerau-levenshtein
Get text similarity level with Damerau-Levenshtein distance
Installs: 1 044
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 9
Open Issues: 0
Requires
- php: >=7.1.0
- ext-mbstring: *
- composer/installers: >1
Requires (Dev)
- oefenweb/cakephp-codesniffer: ^2.0
- phpmd/phpmd: ^2.0
- phpstan/phpstan: ^0.12.66
- phpunit/phpunit: ^7.5|^8|^9
- sebastian/phpcpd: ^4.0|^5.0|^6.0
README
Get text similarity level with Damerau-Levenshtein distance.
Requirements
- PHP 7.1.0 or greater.
Installation
composer require oefenweb/damerau-levenshtein
Usage
$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.
License
MIT
Author Information
Mischa ter Smitten (based on work of Ph4r05)