tokenSetPartialRatio function
Calculates partial token set ratio using the partial ratio algorithm
Implementation
double tokenSetPartialRatio(String s1, String s2, {double scoreCutoff = 0.0}) {
if (scoreCutoff > 100) return 0.0;
// RapidFuzz handles empty strings specially
if (s1.isEmpty || s2.isEmpty) {
return 0.0;
}
// Split the strings into tokens and deduplicate them
var tokens1 = s1.split(RegExp(r'\s+')).toSet();
var tokens2 = s2.split(RegExp(r'\s+')).toSet();
// Handle empty token sets
if (tokens1.isEmpty || tokens2.isEmpty) {
return 0.0;
}
// Calculate intersection and differences
var intersection = tokens1.intersection(tokens2);
var diff1to2 = tokens1.difference(tokens2);
var diff2to1 = tokens2.difference(tokens1);
// Exit early when there is a common word in both sequences
if (intersection.isNotEmpty) {
return 100.0;
}
// Create joined strings for comparison
var diffAB = (diff1to2.toList()..sort()).join(' ');
var diffBA = (diff2to1.toList()..sort()).join(' ');
// Calculate partial ratio between the differences
return core.partialRatio(diffAB, diffBA, scoreCutoff: scoreCutoff);
}