note 48577 deleted from function.pspell-suggest by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:13:56 +0000 |
| Subject: | note 48577 deleted from function.pspell-suggest by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196916@lists.php.net to get a copy of this message | ||
Note Submitter: andoryuusama at gmail dot com
----
To get a more meaningful "did you mean", I use metaphones on the suggestion list, this
helps to insure that the word is one that "sounds" closer to the word spelled wrong.
<?php
function spellcheck ( $string ) {
$words = split(' ',$string);
$misspelled = $return = array();
pspell_config_create("en",PSPELL_NORMAL);
$int = pspell_new('en');
foreach ($words as $value) {
$check = preg_split('/[\W]+?/',$value);
if (($check[1] != '') and (strpos("'",$value) > 0) ) {$check[0] =
$value;}
if (($check[0] + 1 == 1) and (!pspell_check($int, $check[0]) )) {
$res .= '<SPAN class="misspelled" style="color:#FF0000;
font-weight:bold;">' . $value . ' </SPAN> ';
$poss = pspell_suggest($int,$value);
$orig = metaphone($value);
foreach ($poss as $suggested)
{
$ranked[metaphone($suggested)] = $suggested;
}
if ($ranked[$orig] <> '') {$poss[1] = $ranked[$orig];}
$res2 .= '<SPAN style="color:#CC8800; font-weight:bold">' . $poss[1]
. ' </SPAN> ';
} else {
$res .= $value . ' ';
$res2 .= $value . ' ';
}
}
$n[1] = $res;
$n[2] = $res2;
return $n;
}
?>
this helps to correct for the fact that pspell_suggest's first idea of a replacement is just
the first in the alphabet.