Created
December 15, 2015 01:43
-
-
Save purinda/926ac823afa2589dc22f to your computer and use it in GitHub Desktop.
PHP Spelling suggester function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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