toKGramsMap method

Map<String, Set<String>> toKGramsMap([
  1. int k = 2
])

Returns a hashmap of k-grams to terms from the collection of tokens.

Implementation

Map<String, Set<String>> toKGramsMap([int k = 2]) {
  final terms = this;
  // print the terms
  final Map<String, Set<String>> kGramIndex = {};
  for (final term in terms) {
    final kGrams = term.kGrams(k);
    for (final kGram in kGrams) {
      final set = kGramIndex[kGram] ?? {};
      set.add(term);
      kGramIndex[kGram] = set;
    }
  }
  return kGramIndex;
}