partialRatio function
fuzzywuzzy-style partial ratio: how well the shorter string matches the
best-aligning slice of the longer one, in [0, 1].
Slides a window the length of the shorter string across the longer string
and returns the best LevenshteinUtils.ratio of any window. This scores a
substring match high even when the surrounding text differs — e.g.
'New York' against 'New York City' — where the plain whole-string ratio
would be penalized for the extra words. Comparison is case-insensitive.
Two empty strings score 1.0; one empty against a non-empty scores 0.0.
Audited: 2026-06-12 11:26 EDT
Implementation
double partialRatio(String a, String b) {
final String shorter = (a.length <= b.length ? a : b).toLowerCase();
final String longer = (a.length <= b.length ? b : a).toLowerCase();
if (shorter.isEmpty) {
return longer.isEmpty ? 1.0 : 0.0;
}
double best = 0.0;
// Each window is the shorter string's length; the best-aligned slice wins.
for (int start = 0; start + shorter.length <= longer.length; start++) {
final double r = LevenshteinUtils.ratio(
shorter,
longer.substring(start, start + shorter.length),
);
if (r > best) {
best = r;
}
}
return best;
}