strictTokenSimilarity static method

double strictTokenSimilarity(
  1. String hay,
  2. String needle, {
  3. Set<String> drop = const {},
})

Fraction of needle's meaningful tokens that have a tokenEq partner in hay (each hay token consumed once).

Implementation

static double strictTokenSimilarity(String hay, String needle,
    {Set<String> drop = const {}}) {
  final n = allTokens(needle)
      .where((t) => !filler.contains(t) && !drop.contains(t))
      .toList(growable: false);
  if (n.isEmpty) return 0;
  final h = allTokens(hay)
      .where((t) => !filler.contains(t) && !drop.contains(t))
      .toList(growable: false);
  if (h.isEmpty) return 0;
  final used = List<bool>.filled(h.length, false);
  var matched = 0;
  for (final t in n) {
    for (var i = 0; i < h.length; i++) {
      if (used[i]) continue;
      if (tokenEq(h[i], t)) {
        used[i] = true;
        matched++;
        break;
      }
    }
  }
  return matched / n.length;
}