Using PHP's example along with Patrick's comparison percentage function, I have come up with a function that returns the closest word from an array, and assigns the percentage to a referenced variable:
<?php
function closest_word($input, $words, &$percent = null) {
$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;
}
}
$percent = 1 - levenshtein($input, $closest) / max(strlen($input), strlen($closest));
return $closest;
}
?>
Usage:
<?php
$input = 'carrrot';
$words = array('apple','pineapple','banana','orange',
'radish','carrot','pea','bean','potato');
$percent = null;
$found = closest_word($input, $words, $percent);
printf('Closest word to "%s": %s (%s%% match)', $input, $found, round($percent * 100, 2));
?>
I found that lowercasing the array prior to comparing yields a better comparison when the case is not of importance, for example: comparing a user-inputted category to a list of existing categories.
I also found that when the percentage was above 75%, it was usually the match that I was looking for.