You are here

public function SearchApiSpellcheck::suggestString in Search API Spellcheck 7

Check if any of the added spellcheck suggestions match either all or part of the string.

This is a simple method which at best only returns a string. You might be better of using getSuggestion() which returns a SearchApiSpellcheckSuggestion.

Parameters

string $original_string: A single word or full sentance.

Return value

A string if any suggestions could be applied or FALSE if none could be applied.

Overrides SearchApiSpellcheckInterface::suggestString

See also

getSuggestion()

1 call to SearchApiSpellcheck::suggestString()
SearchApiSpellcheck::getSuggestion in includes/SearchApiSpellcheck.inc
Pass a string and if it can be improved by any of the suggestions or sub-services added then return a suggesiton. If no change can be suggested return FALSE.

File

includes/SearchApiSpellcheck.inc, line 68
Spellcheck Service Interface and Class.

Class

SearchApiSpellcheck

Code

public function suggestString($original_string) {
  $suggest_string = $original_string;
  foreach ($this->suggestions as $ws) {

    // Use a regular expression to replace only full words and not part of a
    // word.
    $suggest_string = preg_replace('/\\b' . $ws->original . '\\b/u', $ws->suggestion, $suggest_string);
  }

  // Loop through any sub services to check if they can improve our string.
  // They'll return false if no change has been made.
  foreach ($this->spellchecks as $spellcheck) {
    if ($spellcheck_suggest_string = $spellcheck
      ->suggestString($suggest_string)) {
      $suggest_string = $spellcheck_suggest_string;
    }
  }

  // If the $suggest_string is the same as $original_string then no
  // suggestions were suitable to applied and we return FALSE. Otherwise
  // return the suggested string.
  return $suggest_string == $original_string ? FALSE : $suggest_string;
}