fix: replace words wrapped in leading punctuation#6322
Open
NishchayMahor wants to merge 1 commit into
Open
Conversation
replace_words only stripped trailing punctuation (word.rstrip), so a word wrapped in leading punctuation -- "(world)", '"world"', "-world" -- never matched the replacements dict, while the same word with trailing punctuation did. Strip punctuation on both sides and preserve whatever surrounded the word. Reduces to the existing behavior when there is no leading punctuation. Adds a test; fixes both the sync and async paths (shared _process_words).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
tokenize.utils.replace_wordshandles a word's trailing punctuation but not its leading punctuation, so a word wrapped in quotes/parens/brackets or with a leading dash is never replaced — while the same word with trailing punctuation is:split_words(ignore_punctuation=False)returns tokens with their surrounding punctuation ("(world)",'"world"',"-world"), but_process_wordscomputesno_punctuation = word.rstrip(PUNCTUATIONS)— trailing only — so the dict lookup misses whenever there's leading punctuation. Asymmetric and surprising for TTS pronunciation substitution.Fix
In
_process_words(shared by both the sync and async paths), strip punctuation on both sides for the lookup and preserve whatever punctuation surrounded the word:This reduces exactly to the previous logic when
leading_off == 0, so existing behavior (trailing punctuation, theA.B.C→A.B.C.Dinternal-dot case) is unchanged.Testing
Added
test_replace_words_leading_punctuationtotests/test_tokenizer.pycovering(world),"world",-world,hello (world)., trailing-only, and a no-false-positive case (worldly). The existingtest_replace_wordsstill passes (verified). Pure string functions — offline.