datingvip/damerau-levenshtein

Get text similarity level with Damerau-Levenshtein distance

v3.0.2 2022-01-31 09:36 UTC

This package is auto-updated.

Last update: 2024-11-29 06:47:42 UTC


README

CI PHP 7 ready codecov Packagist downloads Code Climate Scrutinizer Code Quality

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)