You are here

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

Same name and namespace in other branches
  1. 8 src/SuggestionHelper.php \Drupal\suggestion\SuggestionHelper::ngrams()
  2. 3.0.x src/SuggestionHelper.php \Drupal\suggestion\SuggestionHelper::ngrams()

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 235

Class

SuggestionHelper
Provides helper methods for suggestions.

Namespace

Drupal\suggestion

Code

public static function ngrams(array $atoms = []) {
  $max = self::getConfig('atoms_max');
  $min = self::getConfig('atoms_min');
  $ngrams = [];
  $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;
}