/** * SpellChecker * Checks a block of text for spelling mistakes * @parameter string $PS_TEXT: The text to check * @return array. The key will be the possible mis-spelt work the data an array * with the alternatives as the values. */ function SpellChecker($PS_TEXT) { $LA_RETURN = array() ; // remove HTML tags from the text, there are guaranteed to be errors in that $pspell_config = pspell_config_create ("en"); pspell_config_ignore($pspell_config, 3); pspell_config_mode($pspell_config, PSPELL_FAST); $pspell_link = pspell_new_config($pspell_config); // break the text into words, put the words as the basis for the return array $LA_ALL_WORDS = preg_split ("/[\s,.]+/", preg_replace(array('/[^a-z\-\'" ]/i', '/\-{1,}/'), array('', ' '), $PS_TEXT), -1, PREG_SPLIT_NO_EMPTY); $LA_ALL_WORDS = array_flip($LA_ALL_WORDS) ; // loop the word array foreach ( $LA_ALL_WORDS as $LS_WORD => $LV_DUMMY ) { $LA_ALTERNATIVES = pspell_suggest($pspell_link, $LS_WORD) ; // if there are no alternatives if ( count($LA_ALTERNATIVES) == 0 || strtolower($LS_WORD) == strtolower($LA_ALTERNATIVES[0]) ) { // remove the item from the array unset($LA_ALL_WORDS[$LS_WORD]) ; // otherwise } else { // add the items to the array $LA_ALL_WORDS[$LS_WORD] = array() ; // search the alternatives until 5 viable ones are found foreach ( $LA_ALTERNATIVES as $LS_ALTERNATIVE ) { $LA_ALT_CHECK = pspell_suggest($pspell_link, $LS_ALTERNATIVE) ; if ( ! preg_match('/[ \-]/', $LS_ALTERNATIVE) && strtolower($LS_ALTERNATIVE) == $LA_ALT_CHECK[0]) { $LA_ALL_WORDS[$LS_WORD][] = $LS_ALTERNATIVE ; } if ( count($LA_ALL_WORDS[$LS_WORD]) == 5 ) { break ; } } // fi no alternatives } // wend the work array } // return the array return($LA_ALL_WORDS) ; }