closeMatches method

Iterable<Iterable<T>> closeMatches(
  1. Iterable<T> target, {
  2. int count = 3,
  3. double cutoff = 0.6,
})

Returns a list of the best "good enough" matches.

count specifies the number of matches to return. cutoff specifies the required similarity of candidates.

Implementation

Iterable<Iterable<T>> closeMatches(Iterable<T> target,
    {int count = 3, double cutoff = 0.6}) {
  if (count <= 0) throw ArgumentError.value(count, 'count');
  if (cutoff < 0 || cutoff > 1) throw ArgumentError.value(cutoff, 'cutoff');
  final matcher = SequenceMatcher<T>(target: target);
  final candidates = <({Iterable<T> candidate, double ratio})>[];
  for (final candidate in this) {
    matcher.source = candidate;
    if (matcher.realQuickRatio >= cutoff && matcher.quickRatio >= cutoff) {
      final ratio = matcher.ratio;
      if (ratio >= cutoff) {
        candidates.add((candidate: candidate, ratio: ratio));
      }
    }
  }
  return candidates
      .largest(count,
          comparator: naturalComparable<num>.onResultOf((each) => each.ratio))
      .map((each) => each.candidate);
}