Skip to content

Instantly share code, notes, and snippets.

@purinda
Created December 15, 2015 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purinda/926ac823afa2589dc22f to your computer and use it in GitHub Desktop.
Save purinda/926ac823afa2589dc22f to your computer and use it in GitHub Desktop.
PHP Spelling suggester function
<?php
function get_suggestions($user_input)
{
$pspell_link = pspell_new("en");
$suggestions = pspell_suggest($pspell_link, $user_input);
$lowest_distance = null;
$most_relevant_words = [];
foreach ($suggestions as $word) {
$distance = levenshtein($user_input, $word);
if (null === $lowest_distance) {
$lowest_distance = $distance;
}
if ($lowest_distance >= $distance) {
$lowest_distance = $distance;
$most_relevant_words[] = $word;
}
}
$most_similar = null;
$most_similar_words = [];
foreach ($most_relevant_words as $word) {
$similarity = similar_text($user_input, $word);
if (null == $most_similar) {
$most_similar = $similarity;
}
if ($most_similar <= $similarity) {
$most_similar_words[$similarity] = $word;
}
}
return $most_similar_words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment