Skip to content

Commit

Permalink
fix partial_ratio implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbachmann committed Sep 11, 2022
1 parent 99fcdaa commit 9f2caf4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
16 changes: 8 additions & 8 deletions extras/rapidfuzz_amalgamated.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// RapidFuzz v1.0.2
// Generated: 2022-09-11 17:50:38.322175
// Generated: 2022-09-11 18:18:03.779400
// ----------------------------------------------------------
// This file is an amalgamation of multiple different files.
// You probably shouldn't edit it directly.
Expand Down Expand Up @@ -5902,9 +5902,6 @@ partial_ratio_short_needle(rapidfuzz::detail::Range<InputIt1> s1, rapidfuzz::det

while (!windows.empty()) {
for (const auto& window : windows) {
size_t cell_diff = window.second - window.first;
if (cell_diff == 1) continue;

auto subseq1 = s2.subseq(static_cast<ptrdiff_t>(window.first), static_cast<ptrdiff_t>(len1));
auto subseq2 = s2.subseq(static_cast<ptrdiff_t>(window.second), static_cast<ptrdiff_t>(len1));
if (scores[window.first] == -1) {
Expand All @@ -5914,8 +5911,8 @@ partial_ratio_short_needle(rapidfuzz::detail::Range<InputIt1> s1, rapidfuzz::det
res.dest_start = window.first;
res.dest_end = window.first + len1;
if (best_dist == 0) {
new_windows.clear();
break;
res.score = 100;
return res;
}
}
}
Expand All @@ -5926,12 +5923,15 @@ partial_ratio_short_needle(rapidfuzz::detail::Range<InputIt1> s1, rapidfuzz::det
res.dest_start = window.second;
res.dest_end = window.second + len1;
if (best_dist == 0) {
new_windows.clear();
break;
res.score = 100;
return res;
}
}
}

size_t cell_diff = window.second - window.first;
if (cell_diff == 1) continue;

/* find the minimum score possible in the range first <-> last */
int64_t known_edits = std::abs(scores[window.first] - scores[window.second]);
/* half of the cells that are not needed for known_edits can lead to a better score */
Expand Down
14 changes: 7 additions & 7 deletions rapidfuzz/fuzz.impl
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ partial_ratio_short_needle(rapidfuzz::detail::Range<InputIt1> s1, rapidfuzz::det

while (!windows.empty()) {
for (const auto& window : windows) {
size_t cell_diff = window.second - window.first;
if (cell_diff == 1) continue;

auto subseq1 = s2.subseq(static_cast<ptrdiff_t>(window.first), static_cast<ptrdiff_t>(len1));
auto subseq2 = s2.subseq(static_cast<ptrdiff_t>(window.second), static_cast<ptrdiff_t>(len1));
if (scores[window.first] == -1) {
Expand All @@ -88,8 +85,8 @@ partial_ratio_short_needle(rapidfuzz::detail::Range<InputIt1> s1, rapidfuzz::det
res.dest_start = window.first;
res.dest_end = window.first + len1;
if (best_dist == 0) {
new_windows.clear();
break;
res.score = 100;
return res;
}
}
}
Expand All @@ -100,12 +97,15 @@ partial_ratio_short_needle(rapidfuzz::detail::Range<InputIt1> s1, rapidfuzz::det
res.dest_start = window.second;
res.dest_end = window.second + len1;
if (best_dist == 0) {
new_windows.clear();
break;
res.score = 100;
return res;
}
}
}

size_t cell_diff = window.second - window.first;
if (cell_diff == 1) continue;

/* find the minimum score possible in the range first <-> last */
int64_t known_edits = std::abs(scores[window.first] - scores[window.second]);
/* half of the cells that are not needed for known_edits can lead to a better score */
Expand Down
1 change: 1 addition & 0 deletions test/tests-fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ TEST_CASE("RatioTest")
SECTION("testPartialRatioShortNeedle")
{
REQUIRE(Approx(33.3333333) == fuzz::partial_ratio("001", "220222"));
REQUIRE(Approx(100) == fuzz::partial_ratio("physics 2 vid", "study physics physics 2 video"));
}

SECTION("testIssue206") /* test for https://github.com/maxbachmann/RapidFuzz/issues/206 */
Expand Down

0 comments on commit 9f2caf4

Please sign in to comment.