getSuggestions method

List<SimilarityIndex> getSuggestions(
  1. Iterable<String> terms, {
  2. int limit = 10,
  3. int k = 2,
  4. double greaterThan = 0.10,
  5. int roundTo = 3,
})

Returns a collection of SimilarityIndexs for this String from terms.

Similarity is calculated for each term in terms by starting with 1.0 and iteratively multiplying by lengthSimilarity, characterSimilarity, jaccardSimilarity and editSimilarity, terminating the iteration as soon as the greaterThan threshold is reached.

The default greaterThan is 0.10. A higher value for greaterThan can improve performance as candidates with different lengths to this term are not evaluated, avoiding costly computation of edit distance and or Jaccard similarity.

Suggestions are returned in descending order of similarity.

The top returned matches will be limited to limit if more than limit matches are found.

Not case-sensitive.

Implementation

List<SimilarityIndex> getSuggestions(Iterable<String> terms,
    {int limit = 10, int k = 2, double greaterThan = 0.10, int roundTo = 3}) {
  final retVal = <SimilarityIndex>[];
  for (final other in terms.toSet()) {
    var similarity = lengthSimilarity(other);
    similarity = similarity <= greaterThan
        ? similarity
        : similarity * characterSimilarity(other);
    similarity = similarity <= greaterThan
        ? similarity
        : similarity * startsWithSimilarity(other);
    similarity = similarity <= greaterThan
        ? similarity
        : similarity * jaccardSimilarity(other);
    similarity = similarity <= greaterThan
        ? similarity
        : similarity * editSimilarity(other);
    final f = pow(10, roundTo);
    similarity = (similarity * f).roundToDouble() / f;
    if (similarity > greaterThan) {
      retVal.add(SimilarityIndex(other, similarity));
    }
  }
  // sort in descending order of similarity and return only the first [limit]
  // results
  return retVal.sortBySimilarity().limit(limit);
}