fix: guard against empty regex matches in WordAug (IndexError #20)#21
Open
temrjan wants to merge 1 commit intoai-forever:mainfrom
Open
fix: guard against empty regex matches in WordAug (IndexError #20)#21temrjan wants to merge 1 commit intoai-forever:mainfrom
temrjan wants to merge 1 commit intoai-forever:mainfrom
Conversation
__replace() and __text2emoji() crash with IndexError when a token contains only characters not matched by the regex (e.g. parentheses, em-dashes). Return the original token unchanged when re.findall() produces an empty list. Added regression tests reproducing the exact scenario from issue ai-forever#20. Closes ai-forever#20 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
Fixes #20 —
WordAug.augment(action='replace')crashes withIndexError: list index out of rangewhen a token contains only characters not matched by the regex pattern (e.g. parentheses(, em-dashes—).Root cause:
re.findall()returns an empty list for unmatchable tokens, thenword[0]raisesIndexError.Fix: Check if the regex result is empty before indexing. If empty, return the original token unchanged. Applied to both affected methods:
__replace()(line 134)__text2emoji()(line 106) — same bug patternAlso renamed the shadowed
wordvariable totokensfor clarity — the original code reusedwordfor both the input string and the regex result list.Test plan
Added
tests/test_word_aug.pywith 5 regression tests:(that don't match the regex—text2emojiaction🤖 Generated with Claude Code