0

How can I return multiple similar matches with levenshtein in PHP? For example if I have this code:

$input = "Hello what is your name. My namer is Jack.";
$output = "nam";
echo levenshtein($input, $output);

It must not only output name, but name, namer.

How can I do that?

  • By looping through each word in your $input string, executing the levenshtein call against each word and outputting when the resulting distance is above your threshold for similarity – Mark Baker Jan 02 '14 at 15:55
  • @MarkBaker How do I do that? –  Jan 02 '14 at 16:00

1 Answers1

1

Something like:

$input = "Hello what is your name. My namer is Jack.";
$output = "nam";
$threshold = 3;
foreach(str_word_count($input,1) as $word) {
    if (levenshtein($word, $output) < $threshold) {
        echo $word, PHP_EOL;
    }
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385