-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Migrate editdistance metrics to rapidfuzz with empty-reference guard #3114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import importlib.util | ||
| import sys | ||
| import types | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _install_fake_rapidfuzz(): | ||
| rapidfuzz = types.ModuleType("rapidfuzz") | ||
| distance = types.ModuleType("rapidfuzz.distance") | ||
|
|
||
| class Levenshtein: | ||
| @staticmethod | ||
| def distance(left, right): | ||
| left = list(left) | ||
| right = list(right) | ||
| dp = list(range(len(right) + 1)) | ||
| for i, lval in enumerate(left, 1): | ||
| prev = dp[0] | ||
| dp[0] = i | ||
| for j, rval in enumerate(right, 1): | ||
| old = dp[j] | ||
| dp[j] = min( | ||
| dp[j] + 1, | ||
| dp[j - 1] + 1, | ||
| prev + (0 if lval == rval else 1), | ||
| ) | ||
| prev = old | ||
| return dp[-1] | ||
|
|
||
| distance.Levenshtein = Levenshtein | ||
| rapidfuzz.distance = distance | ||
| sys.modules.setdefault("rapidfuzz", rapidfuzz) | ||
| sys.modules.setdefault("rapidfuzz.distance", distance) | ||
|
|
||
|
|
||
| def _load_metrics_common(): | ||
| _install_fake_rapidfuzz() | ||
| module_path = Path(__file__).resolve().parents[1] / "funasr" / "metrics" / "common.py" | ||
| spec = importlib.util.spec_from_file_location("metrics_common_probe", module_path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| sys.modules["metrics_common_probe"] = module | ||
| assert spec.loader is not None | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def test_cer_and_wer_return_none_when_reference_length_is_zero(): | ||
| common = _load_metrics_common() | ||
| calc = common.ErrorCalculator( | ||
| char_list=["<blank>", "a", "b", "c", "h", "e", "l", "o", "w", "r", "d", " "], | ||
| sym_space=" ", | ||
| sym_blank="<blank>", | ||
| report_cer=True, | ||
| report_wer=True, | ||
| ) | ||
|
|
||
| assert calc.calculate_cer(["abc"], [" "]) is None | ||
| assert calc.calculate_wer(["hello world"], [" "]) is None | ||
|
|
||
|
|
||
| def test_cer_and_wer_keep_normal_levenshtein_semantics(): | ||
| common = _load_metrics_common() | ||
| calc = common.ErrorCalculator( | ||
| char_list=["<blank>", "a", "b", "c", "x", "y", " "], | ||
| sym_space=" ", | ||
| sym_blank="<blank>", | ||
| report_cer=True, | ||
| report_wer=True, | ||
| ) | ||
|
|
||
| assert calc.calculate_cer(["abc"], ["axc"]) == 1 / 3 | ||
| assert calc.calculate_wer(["foo bar baz"], ["foo qux baz"]) == 1 / 3 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocking
rapidfuzzwith a custom pure-Python Levenshtein implementation and using dynamic module loading (importlib.util) is an anti-pattern here. Sincerapidfuzzis a required dependency insetup.py, it is guaranteed to be installed in any standard test environment. Mocking it defeats the purpose of testing the actual integration with the library, hides potential import/compatibility issues, and adds unnecessary complexity to the test suite. Simplifying the test file to use standard imports and the realrapidfuzzlibrary makes the tests cleaner, more maintainable, and more reliable.