Skip to content

Commit

Permalink
fix wrong result when allowing only deletions
Browse files Browse the repository at this point in the history
Also fix several buggy tests which this fix surfaced.
  • Loading branch information
taleinat committed Apr 6, 2020
1 parent 9dab06d commit 8d13f0a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
22 changes: 11 additions & 11 deletions src/fuzzysearch/_generic_search.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/fuzzysearch/_generic_search.pyx
Expand Up @@ -178,7 +178,7 @@ cdef _c_find_near_matches_generic_linear_programming(
# if skipping n_dels sub-sequence chars reaches the end
# of the sub-sequence, yield a match
if cand.subseq_index + n_skipped == subseq_len:
add_match(cand.start, index + 1, cand.l_dist + n_skipped)
add_match(cand.start, index, cand.l_dist + n_skipped)
break
# otherwise, if skipping n_skipped sub-sequence chars
# reaches a sub-sequence char identical to this sequence
Expand All @@ -187,7 +187,7 @@ cdef _c_find_near_matches_generic_linear_programming(
# if this is the last char of the sub-sequence, yield
# a match
if cand.subseq_index + n_skipped + 1 == subseq_len:
add_match(cand.start, index + 1, cand.l_dist + n_skipped)
add_match(cand.start, index, cand.l_dist + n_skipped)
# otherwise add a candidate skipping n_skipped
# subsequence chars
else:
Expand Down
4 changes: 2 additions & 2 deletions src/fuzzysearch/generic_search.py
Expand Up @@ -142,7 +142,7 @@ def make_match(start, end, dist):
# if skipping n_dels sub-sequence chars reaches the end
# of the sub-sequence, yield a match
if cand.subseq_index + n_skipped == subseq_len:
yield make_match(cand.start, index + 1,
yield make_match(cand.start, index,
cand.l_dist + n_skipped)
break
# otherwise, if skipping n_skipped sub-sequence chars
Expand All @@ -152,7 +152,7 @@ def make_match(start, end, dist):
# if this is the last char of the sub-sequence, yield
# a match
if cand.subseq_index + n_skipped + 1 == subseq_len:
yield make_match(cand.start, index + 1,
yield make_match(cand.start, index,
cand.l_dist + n_skipped)
# otherwise add a candidate skipping n_skipped
# subsequence chars
Expand Down
16 changes: 13 additions & 3 deletions tests/test_generic_search.py
Expand Up @@ -257,6 +257,16 @@ def test_no_deletion(self):
[]
)

def test_only_deletions(self):
self.expectedOutcomes(
self.search(b('TESTabc'), b('TEST123'), 0, 0, 5, None),
[
Match(start=0, end=4, dist=3, matched=b('TEST')),
Match(start=1, end=4, dist=4, matched=b('EST')),
Match(start=2, end=4, dist=5, matched=b('ST')),
],
)

def test_invalid_none_arguments(self):
# check that an exception is raised when max_l_dist is None as well as
# at least one other limitation
Expand Down Expand Up @@ -306,8 +316,8 @@ def test_non_string_sequences(self):
[Match(start=0, end=3, dist=1,
matched=klass([1, 2, 4]))])
self.expectedOutcomes(self.search(klass([1, 2, 3]), klass([1, 2, 4]), 0, 0, 1, 1),
[Match(start=0, end=3, dist=1,
matched=klass([1, 2, 4]))])
[Match(start=0, end=2, dist=1,
matched=klass([1, 2]))])

def test_list_of_words_one_missing(self):
subsequence = "jumped over the a lazy dog".split()
Expand Down Expand Up @@ -427,7 +437,7 @@ def test_missing_second_item_complex(self):
Match(start=1, end=5, dist=1, matched=b('bcde')),
Match(start=2, end=5, dist=1, matched=b('cde')),
Match(start=3, end=5, dist=1, matched=b('de')),
Match(start=2, end=5, dist=3, matched=b('bcd')),
Match(start=2, end=5, dist=2, matched=b('bcd')),
}.issubset(set(
self.search(b('bde'), b('abcdefg'), 1, 1, 1, 3),
))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_generic_search_cython.py
Expand Up @@ -125,7 +125,7 @@ def test_missing_second_item_complex(self):
Match(start=1, end=5, dist=1, matched=b('bcde')),
Match(start=2, end=5, dist=1, matched=b('cde')),
Match(start=3, end=5, dist=1, matched=b('de')),
Match(start=2, end=5, dist=3, matched=b('cde')),
Match(start=2, end=5, dist=2, matched=b('bcd')),
}.issubset(set(
self.search(b('bde'), b('abcdefg'), 1, 1, 1, 3),
))
Expand Down

0 comments on commit 8d13f0a

Please sign in to comment.