Spell Checking with Aspell and PHP

By reeset / On / In Digital Libraries, Programming

One of the things that I like about PHP is the many built in functions found in the language. Within our metasearch tool, one of the things that I wanted to start looking at adding was spell checking. Since Aspell works as a good open source dictionary and is available as a php extension (pspell) — I thought that this would be a good place to start. However, one of the things that I found quickly was that a true dictionary spell checker really isn’t particularly useful. The reason being of course, that a mispelling will likely result in multiple matches with the best match not being the first match. But not to worry, PHP provides a solution…metaphonics. Metaphonics is the analysis of sounds. Using Aspell in conjunction with php’s metaphonics functionality allows one to do a better job matching the user’s query against a correct spelling. Here is a simple example — a modification of a couple of examples found on the web…


function spellcheck ( $string ) {
$res = “”;
$res2 = “”;
$words = split(‘ ‘,$string);
$misspelled = $return = array();
$int = pspell_new(‘en’);
foreach ($words as $value) {
$check = preg_split(‘/[\W]+?/’,$value);
if ((isset($check[1])) and (strpos(“‘”,$value) > 0) ) {$check[0] = $value;}
if ((!pspell_check($int, $check[0]) )) {
$res .= $value;
$poss = pspell_suggest($int,$value);
$orig = metaphone($value);
foreach ($poss as $suggested)
$ranked[metaphone($suggested)] = $suggested;
if ($ranked[$orig] <> ”) {$poss[1] = $ranked[$orig];}

$res2 .= $poss[1] . ‘ ‘;

} else {
$res .= $value . ‘ ‘;
$res2 .= $value . ‘ ‘;

$n[1] = $res;
$n[2] = $res2;
if (trim($string)!=trim($res2)) {
return $res2;
} else {
return “”;