Skip to content

Fix partial_ratio ignoring score_cutoff > 100 for two empty strings in the Python fallback - #512

Open
ines-besrour wants to merge 1 commit into
rapidfuzz:mainfrom
ines-besrour:fix-partial-ratio-score-cutoff
Open

ines-besrour wants to merge 1 commit into
rapidfuzz:mainfrom
ines-besrour:fix-partial-ratio-score-cutoff

Conversation

@ines-besrour

Copy link
Copy Markdown

The pure-Python partial_ratio_alignment returns a perfect score for two empty strings before it looks at score_cutoff. With a cutoff above 100, the fallback therefore disagrees with the C++ implementation:

fuzz_cpp.partial_ratio("", "", score_cutoff=101)             # 0.0
fuzz_py.partial_ratio("", "", score_cutoff=101)              # 100.0
fuzz_py.partial_token_sort_ratio("", "", score_cutoff=101)   # 100.0
fuzz_cpp.partial_ratio_alignment("", "", score_cutoff=101)   # None
fuzz_py.partial_ratio_alignment("", "", score_cutoff=101)    # ScoreAlignment(score=100.0, ...)

This adds the score_cutoff > 100 guard that the C++ code and the other Python scorers already use (token_sort_ratio, and the token scorers since #488), placed before the empty-string shortcut. partial_token_sort_ratio goes through partial_ratio, so it is fixed as well.

Tests. test_score_cutoff_above_100 now also checks two empty strings for every scorer in both implementations, and a new test checks partial_ratio_alignment. Without the fix they fail for partial_ratio, partial_token_sort_ratio and partial_ratio_alignment; with it, tests/test_fuzz.py passes (built from source on main, CPython 3.12, Linux).

Note: tests/distance/test_distance.py::test_score_cutoff_equal_to_score[scorer9] fails on main both before and after this change, so it is unrelated to this PR.

Fixes #510

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python fallback: partial_ratio ignores score_cutoff > 100 for two empty strings

1 participant