Skip to content

Commit e204e54

Browse files
committed
fix potentially incorrect results of JaroWinkler when using high prefix weights
1 parent cb4b1c7 commit e204e54

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22

3+
## [3.0.3] - 2023-04-06
4+
### Fixed
5+
- fix potentially incorrect results of JaroWinkler when using high prefix weights
6+
37
## [3.0.2] - 2023-03-04
48
### Fixed
59
- fix assert leading to compilation failures

rapidfuzz/distance/JaroWinkler.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct MultiJaroWinkler : public detail::MultiSimilarityBase<MultiJaroWinkler<Ma
137137
if (static_cast<uint64_t>(s2[prefix]) != prefixes[i][prefix]) break;
138138

139139
scores[i] += static_cast<double>(prefix) * prefix_weight * (1.0 - scores[i]);
140+
scores[i] = std::min(scores[i], 1.0);
140141
}
141142

142143
if (scores[i] < score_cutoff) scores[i] = 0.0;

rapidfuzz/distance/JaroWinkler_impl.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ double jaro_winkler_similarity(const Range<InputIt1>& P, const Range<InputIt2>&
2929
}
3030

3131
double Sim = jaro_similarity(P, T, jaro_score_cutoff);
32-
if (Sim > 0.7) Sim += static_cast<double>(prefix) * prefix_weight * (1.0 - Sim);
32+
if (Sim > 0.7) {
33+
Sim += static_cast<double>(prefix) * prefix_weight * (1.0 - Sim);
34+
Sim = std::min(Sim, 1.0);
35+
}
3336

3437
return (Sim >= score_cutoff) ? Sim : 0;
3538
}
@@ -58,7 +61,10 @@ double jaro_winkler_similarity(const BlockPatternMatchVector& PM, const Range<In
5861
}
5962

6063
double Sim = jaro_similarity(PM, P, T, jaro_score_cutoff);
61-
if (Sim > 0.7) Sim += static_cast<double>(prefix) * prefix_weight * (1.0 - Sim);
64+
if (Sim > 0.7) {
65+
Sim += static_cast<double>(prefix) * prefix_weight * (1.0 - Sim);
66+
Sim = std::min(Sim, 1.0);
67+
}
6268

6369
return (Sim >= score_cutoff) ? Sim : 0;
6470
}

0 commit comments

Comments
 (0)