You are here

public static function SuggestionHelper::ngrams in Autocomplete Search Suggestions 7

Build a set of ngrams from the set of atoms.

Parameters

array $atoms: An array of strings.

Return value

array An array of ngrams keys.

1 call to SuggestionHelper::ngrams()
SuggestionHelper::insert in src/SuggestionHelper.php
Add a suggestion.

File

src/SuggestionHelper.php, line 199
Helper methods for the suggestion module.

Class

SuggestionHelper
Provides helper methods for suggestions.

Code

public static function ngrams(array $atoms = array()) {
  $max = variable_get('suggestion_atoms_max', 6);
  $min = variable_get('suggestion_atoms_min', 6);
  $ngrams = array();
  $count = count($atoms) - $min;
  for ($i = 0; $i <= $count; $i++) {
    for ($j = $min; $j <= $max; $j++) {
      $ngrams[implode(' ', array_slice($atoms, $i, $j))] = 1;
    }
  }
  $atoms = array_reverse($atoms);
  for ($i = 0; $i <= $count; $i++) {
    for ($j = $min; $j <= $max; $j++) {
      $ngrams[implode(' ', array_slice($atoms, $i, $j))] = 1;
    }
  }
  return $ngrams;
}