Levenshtein Sensitivity
Demonstration of a function which utilizes the levenshtein function with sensitivity.
This is a great function for checking to see if a similar record exists before adding a new record.
This is a great function for checking to see if a similar record exists before adding a new record.
$word = 'PINEEEEAPPLE';
$words = array('apple','pineapple','banana','orange',
'radish','carrot','pea','bean','potato');
echo wordMatch($words, strtolower($word), 2);
function wordMatch($words, $input, $sensitivity){
$shortest = -1;
foreach ($words as $word) {
$lev = levenshtein($input, $word);
if ($lev == 0) {
$closest = $word;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word;
$shortest = $lev;
}
}
if($shortest <= $sensitivity){
return $closest;
} else {
return 0;
}
}