Repository navigation
fix: word_tokenize keeps combining marks inside the word - #24
Conversation
_WORD_REGEX matched words with [\p{L}\p{N}'-]+, which leaves out
Unicode category M. Thai, Devanagari, Bengali, Tamil and other abugidas
write vowel signs, viramas and tone marks as Mn/Mc code points, so each
mark ended the token and was dropped: "เพิ่มความสว่างไฟ" tokenized as
['เพ', 'มความสว', 'างไฟ'] and ovos-utterance-normalizer, which joins the
tokens back with spaces, turned it into "เพ มความสว างไฟ".
Add \p{M} next to \p{L} in the word class and in the abbreviation
alternative. Punctuation is still split off as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Reopening: this was closed by mistake. A PR in one of our own repositories referenced it with a closing keyword, and merging that PR closed this one. The fix here (keeping combining marks inside words in |
The bug
word_tokenizedrops Unicode combining marks (categoryMn/Mc) and splits the word at each one. Thai, Hindi, Bengali, Tamil and every other abugida write their vowel signs, viramas and tone marks this way, so their words come back in pieces.The cause is the word alternative of
_WORD_REGEXinquebra_frases/tokenization.py(line 5 ondev):[\p{L}\p{N}'-]+has no\p{M}, so U+0E34 THAI CHARACTER SARA I, U+0E48 MAI EK, U+093F DEVANAGARI VOWEL SIGN I, U+094D VIRAMA and the like match none of the alternatives.finditerskips them and the word ends there.It shows up in ovos-core because the default
ovos-utterance-normalizertransformer tokenizes withquebra_frases.word_tokenizeand joins the tokens back with spaces:word_tokenizebeforeเพิ่มความสว่างไฟ['เพ', 'มความสว', 'างไฟ']เพ มความสว างไฟเพิ่มความสว่างไฟनमस्ते['नमस', 'त']नमस तनमस्तेबत्ती की चमक बढ़ाओ['बत', 'त', 'क', 'चमक', 'बढ', 'ओ']बत त क चमक बढ ओबत्ती की चमक बढ़ाओআলো বাড়াও['আল', 'ব', 'ড', 'ও']আল ব ড ওআলো বাড়াওவிளக்கை அணை['வ', 'ளக', 'க', 'அண']வ ளக க அணவிளக்கை அணைIntent matching on these languages fails because of this.
The fix
Add
\p{M}next to\p{L}in the word class, and in the abbreviation alternative so the two stay consistent. Punctuation is still split off:เพิ่มความสว่างไฟ!becomes['เพิ่มความสว่างไฟ', '!']. Latin text written in NFD (cafefollowed by U+0301) also keeps its accent on the letter now.Tests
New
test_word_combining_marksintest/test_chunking.pycovers Thai (with and without!,,,?), Hindi, Bengali, Tamil, an NFD Latin accent, andspan_indexed_word_tokenizeoffsets on Devanagari. It fails ondevwithout the fix (['เพ', 'มความสว', 'างไฟ'] != ['เพิ่มความสว่างไฟ']).pytest test/passes with it: 23 passed.I also ran it end to end with
ovos-utterance-normalizer0.2.5a2 against this branch. The five sentences above come out unchanged, andturn on the lights, please!still normalises toturn on the lights , please.🤖 Generated with Claude Code