PHP code example of oefenweb / damerau-levenshtein

1. Go to this page and download the library: Download oefenweb/damerau-levenshtein library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oefenweb / damerau-levenshtein example snippets


$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'
 */