qwidjib0

Pspell usage example

Dec 8th, 2011
1,772
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * SpellChecker
  3.  * Checks a block of text for spelling mistakes
  4.  * @parameter string $PS_TEXT: The text to check
  5.  * @return array. The key will be the possible mis-spelt work the data an array
  6.  *            with the alternatives as the values.
  7.  */
  8. function SpellChecker($PS_TEXT) {
  9.     $LA_RETURN = array() ;
  10.  
  11.     // remove HTML tags from the text, there are guaranteed to be errors in that
  12.     $pspell_config = pspell_config_create ("en");
  13.     pspell_config_ignore($pspell_config, 3);
  14.     pspell_config_mode($pspell_config, PSPELL_FAST);
  15.     $pspell_link = pspell_new_config($pspell_config);
  16.  
  17.     // break the text into words, put the words as the basis for the return array
  18.     $LA_ALL_WORDS = preg_split ("/[\s,.]+/", preg_replace(array('/[^a-z\-\'" ]/i', '/\-{1,}/'), array('', ' '), $PS_TEXT), -1, PREG_SPLIT_NO_EMPTY);
  19.     $LA_ALL_WORDS = array_flip($LA_ALL_WORDS) ;
  20.  
  21.     // loop the word array
  22.     foreach ( $LA_ALL_WORDS as $LS_WORD => $LV_DUMMY )
  23.     {
  24.  
  25.         $LA_ALTERNATIVES = pspell_suggest($pspell_link, $LS_WORD) ;
  26.  
  27.         // if there are no alternatives
  28.         if ( count($LA_ALTERNATIVES) == 0 || strtolower($LS_WORD) == strtolower($LA_ALTERNATIVES[0]) )
  29.         {
  30.  
  31.             // remove the item from the array
  32.             unset($LA_ALL_WORDS[$LS_WORD]) ;
  33.  
  34.         // otherwise
  35.         } else
  36.         {
  37.  
  38.             // add the items to the array
  39.             $LA_ALL_WORDS[$LS_WORD] = array() ;
  40.  
  41.             // search the alternatives until 5 viable ones are found
  42.             foreach ( $LA_ALTERNATIVES as $LS_ALTERNATIVE )
  43.             {
  44.                 $LA_ALT_CHECK = pspell_suggest($pspell_link, $LS_ALTERNATIVE) ;
  45.                 if ( ! preg_match('/[ \-]/', $LS_ALTERNATIVE) && strtolower($LS_ALTERNATIVE) == $LA_ALT_CHECK[0])
  46.                 {
  47.                     $LA_ALL_WORDS[$LS_WORD][] = $LS_ALTERNATIVE ;
  48.                 }
  49.                 if ( count($LA_ALL_WORDS[$LS_WORD]) == 5 )
  50.                 {
  51.                     break ;
  52.                 }
  53.             }
  54.  
  55.         // fi no alternatives
  56.         }
  57.  
  58.     // wend the work array
  59.     }
  60.  
  61.     // return the array
  62.     return($LA_ALL_WORDS) ;
  63. }
RAW Paste Data