Skip to content

Instantly share code, notes, and snippets.

@GordonLesti
Created February 5, 2014 12:21
Show Gist options
  • Select an option

  • Save GordonLesti/8822532 to your computer and use it in GitHub Desktop.

Select an option

Save GordonLesti/8822532 to your computer and use it in GitHub Desktop.
Levenshtein distance 1
<?php
function getLevenshtein1($word)
{
$words = array();
for ($i = 0; $i < strlen($word); $i++) {
// insertions
$words[] = substr($word, 0, $i) . '_' . substr($word, $i);
// deletions
$words[] = substr($word, 0, $i) . substr($word, $i + 1);
// substitutions
$words[] = substr($word, 0, $i) . '_' . substr($word, $i + 1);
}
// last insertion
$words[] = $word . '_';
return $words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment