Commit 723c1ccb authored by Thanh Nguyen's avatar Thanh Nguyen Committed by Commit Bot

[fuzzy-app-search] Fix bug regarding EditDistance ratio

If the edit distance is big enough, the ratio could be less than 0.
This CL fixes the bug.

Bug: 990684
Change-Id: I61e536aff49cef3cd5e844b816f8d3e6662f0e88
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2068228Reviewed-by: default avatarRachel Wong <wrong@chromium.org>
Commit-Queue: Thanh Nguyen <thanhdng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743878}
parent d2954597
...@@ -16,7 +16,7 @@ bool CompareMatches(const Match& m1, const Match& m2) { ...@@ -16,7 +16,7 @@ bool CompareMatches(const Match& m1, const Match& m2) {
} }
} // namespace } // namespace
SequenceMatcher::Match::Match() {} SequenceMatcher::Match::Match() = default;
SequenceMatcher::Match::Match(int pos_first, int pos_second, int len) SequenceMatcher::Match::Match(int pos_first, int pos_second, int len)
: pos_first_string(pos_first), pos_second_string(pos_second), length(len) { : pos_first_string(pos_first), pos_second_string(pos_second), length(len) {
DCHECK_GE(pos_first_string, 0); DCHECK_GE(pos_first_string, 0);
...@@ -175,9 +175,9 @@ double SequenceMatcher::Ratio() { ...@@ -175,9 +175,9 @@ double SequenceMatcher::Ratio() {
if (use_edit_distance_) { if (use_edit_distance_) {
if (edit_distance_ratio_ < 0) { if (edit_distance_ratio_ < 0) {
const int edit_distance = EditDistance(); const int edit_distance = EditDistance();
edit_distance_ratio_ = edit_distance_ratio_ = std::max(
1.0 - static_cast<double>(edit_distance) * 2 / 0.0, 1.0 - static_cast<double>(edit_distance) * 2 /
(first_string_.size() + second_string_.size()); (first_string_.size() + second_string_.size()));
} }
return edit_distance_ratio_; return edit_distance_ratio_;
} }
......
...@@ -127,5 +127,11 @@ TEST_F(SequenceMatcherTest, TestEditDistanceRatio) { ...@@ -127,5 +127,11 @@ TEST_F(SequenceMatcherTest, TestEditDistanceRatio) {
base::UTF8ToUTF16("cats white"), true) base::UTF8ToUTF16("cats white"), true)
.Ratio(), .Ratio(),
0.2, 0.01); 0.2, 0.01);
// Totally different
EXPECT_NEAR(SequenceMatcher(base::UTF8ToUTF16("dog"),
base::UTF8ToUTF16("elphant"), true)
.Ratio(),
0.0, 0.01);
} }
} // namespace } // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment