You are here

function fuzzysearch_parse_word in Fuzzy Search 7

Insert the words into the database as they are indexed.

Parameters

string $word: Word to insert into the index.

array $fuzzy: Ngram lenghth.

Return value

array Score modifier given to the node from hook_search_score.

1 call to fuzzysearch_parse_word()
FuzzySearchService::createKeysQuery in includes/service.inc
Helper method for creating a SELECT query for given search keys.

File

./fuzzysearch.module, line 127
Fuzzysearch module.

Code

function fuzzysearch_parse_word($word, array $fuzzy = array()) {

  // Cleanse and remove spaces.
  $ngrams = array(
    'ngrams' => array(),
  );
  $word = str_replace(' ', '', fuzzysearch_cleanse($word));
  $length = drupal_strlen($word);
  $ngrams['completeness'] = $length - $fuzzy['nlength'] + 1 == 0 ? 100 : number_format(100 / ($length - $fuzzy['nlength'] + 1), 3);

  // $comp_min is the minumum completeness an ngram can have to return words.
  // If > 0 $extra_letters assumes the searcher has missing letters in the
  // search term. It's configurable by the admin. Increasing the number of
  // extra letters lowers the completeness and returns more words, making
  // things fuzzier.
  $ngrams['comp_min'] = number_format(100 / ($length - $fuzzy['nlength'] + 1 + $fuzzy['missing_letters']), 3);

  // $comp_max is the maximum completeness an ngram can have and still return
  // words. In this case we assume the search term has extra letters, and this
  // lets us include shorter words in the results. Increasing this raises the
  // completeness of an ngram and returns more grams with higher completeness.
  // Completeness can never be higher than 100%.
  if ($length - $fuzzy['nlength'] + 1 - $fuzzy['extra_letters'] <= 0) {
    $ngrams['comp_max'] = number_format(100, 3) + 0.001;
  }
  else {
    $ngrams['comp_max'] = number_format(100 / ($length - $fuzzy['nlength'] + 1 - $fuzzy['extra_letters']), 3) + 0.001;
  }

  // Split word into ngrams.
  for ($i = 0; $i < $length - $fuzzy['nlength'] + 1; $i++) {
    $ngrams['ngrams'][] = drupal_substr($word, $i, $fuzzy['nlength']);
  }
  return $ngrams;
}