jaroSimilarity function
Calculates the Jaro similarity between two strings
Implementation
double jaroSimilarity(String s1, String s2) {
if (s1 == s2) return 1.0;
if (s1.isEmpty || s2.isEmpty) return 0.0;
if (s1.length == 1 && s2.length == 1) return s1 == s2 ? 1.0 : 0.0;
final matchWindow = (max(s1.length, s2.length) ~/ 2) - 1;
final matches1 = List<bool>.filled(s1.length, false);
final matches2 = List<bool>.filled(s2.length, false);
var matches = 0;
var transpositions = 0;
// Find matches
for (var i = 0; i < s1.length; i++) {
final start = max(0, i - matchWindow);
final end = min(i + matchWindow + 1, s2.length);
for (var j = start; j < end; j++) {
if (!matches2[j] && s1[i] == s2[j]) {
matches1[i] = true;
matches2[j] = true;
matches++;
break;
}
}
}
if (matches == 0) return 0.0;
// Count transpositions
var k = 0;
for (var i = 0; i < s1.length; i++) {
if (matches1[i]) {
while (!matches2[k]) {
k++;
}
if (s1[i] != s2[k]) transpositions++;
k++;
}
}
transpositions = transpositions ~/ 2;
return (matches / s1.length +
matches / s2.length +
(matches - transpositions) / matches) /
3.0;
}