public function suggest($word) {
/**
* setup our suggestion array
*/
$suggestions = array();
/**
* init removed array
*/
$removed = array('end' => '','start' => '');
/**
* any numbers at the begininng to remove
*/
if(true == preg_match('/^(\d+)/',$word,$digits)) {
/**
* add to our removed
*/
$removed['start'] = $digits[1];
/**
* remove from the word
*/
$word = str_replace($removed['start'],'',$word);
}
/**
* any numbers at the end to remove
*/
if(true == preg_match('/(\d+)$/',$word,$digits)) {
/**
* add to our removed
*/
$removed['end'] = $digits[1];
/**
* remove from the word
*/
$word = str_replace($removed['end'],'',$word);
}
/**
* loop through and look for suggestions
*/
foreach($this->container as $lang => $int) {
/**
* make sure we check for single word
*/
if(true === pspell_check($int, $word)) {
/**
* were correct :D
*/
$suggestions[$lang] = $removed['start'].' '.$word.' '.$removed['end'];
/**
* nothing more to see
*/
continue;
}
/**
* get the suggestions
*/
$suggest = pspell_suggest($int, $word);
/**
* did we get any suggestions
*/
if(count($suggest) > 0) {
/**
* make sure lowercase
*/
$suggest[0] = strtolower(trim($suggest[0]));
/**
* does it equal our word?
*/
if($word == str_replace(' ','',$suggest[0])) {
/**
* were correct :D
*/
$suggestions[$lang] = $removed['start'].' '.$suggest[0].' '.$removed['end'];
/**
* nothing more to see
*/
continue;
}
}
/**
* init our variables
*/
$extra = array();
$found = array();
/**
* split word into an array
*/
$wordArray = str_split($word);
/**
* loop through all possible suggestions
*/
do{
/**
* add to the begining of extra
*/
array_unshift($extra,array_pop($wordArray));
/**
* any suggestions
*/
$suggest = pspell_suggest($int, implode('',$wordArray));
/**
* did we get one?
*/
if(count($suggest) > 0) {
/**
* need lowercase
*/
$suggest[0] = strtolower(trim($suggest[0]));
/**
* add to found array
*/
$found[$suggest[0]] = implode('',$extra);
}
/**
* if no more to process end
*/
}while(count($wordArray) > 1);
/**
* sort array by key length
*/
uksort($found, array($this,'sortLength'));
/**
* loop through all found
*/
foreach($found as $string => $extra) {
/**
* is the extra a word
*/
if(pspell_check($int,$extra)) {
/**
* add to end of string
*/
$string = $string.' '.$extra;
}else{
/**
* not a word look for suggestions
*/
$suggest = pspell_suggest($int, $extra);
/**
* did we get any
*/
if(count($suggest) == 0) {
continue;
}
/**
* add best suggestion to end
*/
$string = $string.' '.strtolower(trim($suggest[0]));
}
/**
* remove spaces
*/
$stringCompare = str_replace(' ','',$string);
/**
* compare with original word
*/
if($removed['start'].$stringCompare.$removed['end'] == $removed['start'].$word.$removed['end']) {
/**
* this is our keywords :D
*/
$suggestions[$lang] = $removed['start'].' '.$string.' '.$removed['end'];
/**
* break the loop
*/
break;
}
}
}
if (true === isset($suggestions['en']) AND true === isset($suggestions['fr']))
{
$suggestions['bilingual'] = true;
}
/**
* return our suggestions
*/
return $suggestions;
}