tokenSetRatio function
fuzzywuzzy-style token-set ratio: the best ratio over the shared tokens
and each side's remainder, in [0, 1].
Splits both strings into token sets, then compares the sorted intersection
against the intersection-plus-each-remainder. A string that is a superset of
the other's tokens ('apple pie' vs 'apple pie with cinnamon') scores
high because the shared core aligns exactly. More forgiving of extra words
than tokenSortRatio. Comparison is case-insensitive.
Audited: 2026-06-12 11:26 EDT
Implementation
double tokenSetRatio(String a, String b) {
final Set<String> sa = _fuzzyTokens(a).toSet();
final Set<String> sb = _fuzzyTokens(b).toSet();
final List<String> intersection = sa.intersection(sb).toList()..sort();
final List<String> diffAb = sa.difference(sb).toList()..sort();
final List<String> diffBa = sb.difference(sa).toList()..sort();
// The shared core, and the core extended by each side's leftovers.
final String core = intersection.join(' ');
final String combinedA = <String>[...intersection, ...diffAb].join(' ').trim();
final String combinedB = <String>[...intersection, ...diffBa].join(' ').trim();
// The best of the three pairings is the token-set score. fold (not reduce)
// with a 0.0 seed — ratios are always >= 0, so the seed never wins spuriously
// and there is no empty-collection crash risk.
final double r1 = LevenshteinUtils.ratio(core, combinedA);
final double r2 = LevenshteinUtils.ratio(core, combinedB);
final double r3 = LevenshteinUtils.ratio(combinedA, combinedB);
return <double>[r1, r2, r3].fold<double>(0.0, (double m, double v) => v > m ? v : m);
}