getSuggestions method
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);
}