Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Pspell Functions : pspell_suggest

pspell_suggest

Suggest spellings of a word (PHP 4 >= 4.0.2, PHP 5)
array pspell_suggest ( int dictionary_link, string word )

Example 2039. pspell_suggest() example

<?php
$pspell_link
= pspell_new("en");

if (!
pspell_check($pspell_link, "testt")) {
   
$suggestions = pspell_suggest($pspell_link, "testt");

   foreach (
$suggestions as $suggestion) {
       echo
"Possible spelling: $suggestion<br />";
   }
}
?>

Code Examples / Notes » pspell_suggest

andoryuusama

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.


webmaster

It seems if you are trying to do something similar to the Google 'Did you mean:' suggestions and are selecting the first word given by the pspell_suggest() function, then it will not work well with custom dictionaries and replacements. Take the following code for example:
<?php
   $pspell_config = pspell_config_create("en");
   pspell_config_personal($pspell_config, "/home/user/public_html/custom.pws");
   pspell_config_repl($pspell_config, "/home/user/public_html/custom.repl");
   $pspell_link = pspell_new_config($pspell_config);
   $words = preg_split ("/\s+/", $query);
   $ii = count($words);
   global $spellchecked;
   $spellchecked = "";
   
   for($i=0;$i<$ii;$i++){
       if (pspell_check($pspell_link, $words[$i]))
       {
           $spellchecked .= $words[$i]." ";
       }
       else
       {
           $erroneous = "yes";
           $suggestions = pspell_suggest($pspell_link, $words[$i]);
           $spellchecked .= $suggestions[0]." ";
       }
   }
   if($erroneous == "yes")
   {
       echo "Did you mean: <i>".$spellchecked."?";
   }
   else
   {
       echo $spellchecked . " is a valid word/phrase";
   }
?>
This works fine most of the time, and gives suggestions to what you meant when inserting a spelling mistake with most inputs. However, if you specify a custom replacement and then search for the misspelt word that you specified, then if it is not the first returned suggestion it wont be used in the 'Did you mean' end result. What you need to do is open up the custom dictionary using fopen and fread, and then for each of the suggested words, check if they are in the dictionary. If the suggested word is in the custom dictionary then use it in the 'Did you mean' part, if not, discard it and try the next. Hope this helps anyone who comes across this problem with trying to get more accurate suggestions.


abhishek dot ratani

http://aspell.net/metaphone/
This shows that the pspell library uses metaphones already. I don't know how much better the code andoryuusama at gmail dot com.


Change Language


Follow Navioo On Twitter
pspell_add_to_personal
pspell_add_to_session
pspell_check
pspell_clear_session
pspell_config_create
pspell_config_data_dir
pspell_config_dict_dir
pspell_config_ignore
pspell_config_mode
pspell_config_personal
pspell_config_repl
pspell_config_runtogether
pspell_config_save_repl
pspell_new_config
pspell_new_personal
pspell_new
pspell_save_wordlist
pspell_store_replacement
pspell_suggest
eXTReMe Tracker