Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the PHP levenshtein() function buggy?

Tags:

php

On this page levenshtein(), I am using the example #1 with following variables:

// input misspelled word
$input = 'htc corporation';

// array of words to check against
$words = array('htc', 'Sprint Nextel', 'Sprint', 'banana', 'orange',
        'radish', 'carrot', 'pea', 'bean');

Could someone please tell me why the expected result is carrot rather than htc? Thanks

like image 214
lomse Avatar asked Jan 13 '23 17:01

lomse


1 Answers

Because the levenshtein distance from htc corporation is 12 whereas the distance to carrot is only 11.

The levenshtein function calculates how many characters it has to add or replace to get to a certain word, and because htc corporation has 12 extra characters than htc it has to remove 12 to get to just htc. To get to the word carrot from htc corporation it takes 11 changes.

like image 161
Novocaine Avatar answered Jan 22 '23 20:01

Novocaine