Fix stray space before punctuation andword in number_to_words#247
Open
gaoflow wants to merge 1 commit into
Open
Fix stray space before punctuation andword in number_to_words#247gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
When andword is punctuation such as ",", the COMMA_WORD substitution re-introduced a space before it, producing "one hundred , forty" instead of "one hundred, forty". Re-apply the existing WHITESPACES_COMMA cleanup after the substitution so the separator attaches to the preceding word. Fixes jaraco#229.
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.
Fixes #229.
number_to_words(541140, andword=",")producedfive hundred, forty-one thousand, one hundred , fortywith a stray space before the final comma. The same happens for any number whose last group has trailing tens or units, for examplenumber_to_words(140, andword=",")gaveone hundred , forty.Cause
In
_handle_chunk, the code first runsWHITESPACES_COMMA.sub(",", chunk)to drop any space before a comma. The laterCOMMA_WORD.sub(f" {andword} \\1", chunk)then re-introduces a leading space through its space-padded template. With a wordandword(the default"and") those surrounding spaces are correct, but with a punctuationandwordsuch as","the leading space is wrong.Fix
Re-apply the existing
WHITESPACES_COMMAcleanup immediately after theCOMMA_WORDsubstitution, scoped to the branch that performs it. The default"and"and other wordandwordvalues are unchanged.Verification
Note:
python -m ruff check inflect/__init__.py tests/test_pwd.pycurrently reports pre-existing UP006/UP007/UP035 modernization diagnostics acrossinflect/__init__.pyunrelated to this change.Disclosure: I prepared this fix with AI assistance under my direction; I reviewed and verified the change and tests myself.