PRO-824: Use Unicode-aware regex in ambiguous text check - #1730
PRO-824: Use Unicode-aware regex in ambiguous text check#1730SteveJonesDev wants to merge 1 commit into
Conversation
…languages
Fixes detection of translated ambiguous phrases (e.g. Danish "Læs mere") by
replacing the ASCII-only /[^a-z]+/g regex with the Unicode-aware /[^\p{L}]+/gu
variant. Also normalizes the phrases array on both sides of the comparison so
translator punctuation variations don't cause missed matches.
Closes #84
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe ambiguous text detection in ChangesAmbiguous Text Detection Normalization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Code Review
This pull request improves the ambiguous text check by pre-normalizing phrases and utilizing a Unicode-aware regular expression to strip non-letter characters. The reviewer noted that using only \p{L} will strip combining diacritical marks (\p{M}) used in many languages, potentially breaking words and causing matching to fail. They recommended including \p{M} in the regex and applying .normalize() to both the predefined phrases and the input text to ensure consistent and robust matching.
| const normalizedPhrases = ambiguousPhrases.map( | ||
| ( p ) => p.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim() | ||
| ); |
There was a problem hiding this comment.
Using only \p{L} (Letters) in the Unicode regex will strip combining diacritical marks (\p{M}), which are used in many languages (e.g., Hindi, Arabic, Thai, or even Latin languages when strings are in NFD/decomposed form, such as text copied from macOS). Stripping these marks will break words into fragments and cause matching to fail.
To prevent this, we should:
- Include
\p{M}in the character class:/[^\p{L}\p{M}]+/gu. - Use
.normalize()to ensure consistent Unicode normalization (NFC) before processing.
| const normalizedPhrases = ambiguousPhrases.map( | |
| ( p ) => p.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim() | |
| ); | |
| const normalizedPhrases = ambiguousPhrases.map( | |
| ( p ) => p.normalize().toLowerCase().replace( /[^\p{L}\p{M}]+/gu, ' ' ).trim() | |
| ); |
| text = text.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim(); | ||
| return normalizedPhrases.includes( text ); |
There was a problem hiding this comment.
Apply the same Unicode-aware normalization (including \p{M} and .normalize()) to the input text to ensure consistent matching with the normalized phrases.
| text = text.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim(); | |
| return normalizedPhrases.includes( text ); | |
| text = text.normalize().toLowerCase().replace( /[^\p{L}\p{M}]+/gu, ' ' ).trim(); | |
| return normalizedPhrases.includes( text ); |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 42abe667f7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| text = text.toLowerCase().replace( /[^a-z]+/g, ' ' ).trim(); | ||
| return ambiguousPhrases.includes( text ); | ||
| text = text.toLowerCase().replace( /[^\p{L}]+/gu, ' ' ).trim(); |
There was a problem hiding this comment.
Preserve combining marks when normalizing Unicode text
For locales with accented translations, this drops combining marks instead of treating canonically equivalent text the same. For example, the Vietnamese translation for “continue” is tiếp tục; if the DOM contains the same visible text in decomposed form from copy/paste, textContent normalizes to something like tie p tu c while the precomposed translated phrase normalizes to tiếp tục, so the ambiguous link is missed. Include marks in the character class or normalize both strings to the same Unicode form before stripping punctuation.
Useful? React with 👍 / 👎.
Summary
/[^a-z]+/gregex with the Unicode-aware/[^\p{L}]+/guvariant so that non-Latin characters (e.g.æ,ø,å) are preserved during normalization rather than strippedCloses #84
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit