Repository navigation
fix(tokenizer): keep Unicode combining marks and joiners inside words in WhitespaceTokenSplitter - #411
Open
mlubbad wants to merge 1 commit into
Open
fix(tokenizer): keep Unicode combining marks and joiners inside words in WhitespaceTokenSplitter#411mlubbad wants to merge 1 commit into
mlubbad wants to merge 1 commit into
Conversation
Signed-off-by: Dr. Mohammed Lubbad <engmlubbad@gmail.com>
This branch has not been deployed
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 word-boundary fragmentation in
WhitespaceTokenSplitteron scripts with Unicode combining marks (Mn/Mc/Me) and zero-width joiners (U+200C/U+200D), and guardsCamelArabicSplitteragainst unmatched normalized tokens.Root Cause
Python's
re\wclass excludes Unicode Mark categories (M*) andZWNJ/ZWJ. As a result,re.compile(r"\w+(?:[-_]\w+)*|\S")splits diacritized Arabic, Indic scripts, PersianZWNJcompounds, and NFD-decomposed Latin words at every combining mark:"ذَهَبَ مُحَمَّدٌ"was split into 15 single-character tokens (['ذ', 'َ', 'ه', 'َ', 'ب', 'َ', ...]) instead of['ذَهَبَ', 'مُحَمَّدٌ'], causing multi-word diacritized entities to exceedmax_widthand inflatingconfig.max_lensplitter token counts by 5-8x."प्रधानमंत्री नरेंद्र मोदी"and NFD"Cafe\u0301"suffered the same per-diacritic fragmentation.CamelArabicSplitter.__call__, ifsimple_word_tokenizeemits a normalized token not found verbatim viatext.find(token, last_idx),start_idxbecame-1and rewoundlast_idxnear0.Changes
_WHITESPACE_PATTERNat module import with[_WORD_CHAR = [\w<Unicode M*>\u200c\u200d]](matching the combining-mark handling inGLiNER2), preserving exact character offsets into the original input while keeping plain ASCII/NFC splitting unchanged.start_idx == -1) inCamelArabicSplitter.__call__(matchingHindiSplitter).tests/test_tokenizer_unicode.pycovering Arabic tashkeel, Devanagari/Bengali/Tamil/Telugu vowel signs, Persian/IndicZWNJ/ZWJ, NFD accents, plain-text parity, andCamelArabicSplitteroffset safety.Verification
pytest tests/test_tokenizer_unicode.py tests/test_word_alignment.py tests/test_data_processing.py -q # 168 passed