Skip to content

fix(tokenizer): keep Unicode combining marks and joiners inside words in WhitespaceTokenSplitter - #411

Open
mlubbad wants to merge 1 commit into
urchade:mainfrom
mlubbad:fix/whitespace-splitter-combining-marks
Open

mlubbad wants to merge 1 commit into
urchade:mainfrom
mlubbad:fix/whitespace-splitter-combining-marks

Conversation

@mlubbad

@mlubbad mlubbad commented Nov 27, 2026

Copy link
Copy Markdown

Summary

Fixes word-boundary fragmentation in WhitespaceTokenSplitter on scripts with Unicode combining marks (Mn/Mc/Me) and zero-width joiners (U+200C/U+200D), and guards CamelArabicSplitter against unmatched normalized tokens.

Root Cause

Python's re \w class excludes Unicode Mark categories (M*) and ZWNJ/ZWJ. As a result, re.compile(r"\w+(?:[-_]\w+)*|\S") splits diacritized Arabic, Indic scripts, Persian ZWNJ compounds, and NFD-decomposed Latin words at every combining mark:

  • "ذَهَبَ مُحَمَّدٌ" was split into 15 single-character tokens (['ذ', 'َ', 'ه', 'َ', 'ب', 'َ', ...]) instead of ['ذَهَبَ', 'مُحَمَّدٌ'], causing multi-word diacritized entities to exceed max_width and inflating config.max_len splitter token counts by 5-8x.
  • "प्रधानमंत्री नरेंद्र मोदी" and NFD "Cafe\u0301" suffered the same per-diacritic fragmentation.
  • In CamelArabicSplitter.__call__, if simple_word_tokenize emits a normalized token not found verbatim via text.find(token, last_idx), start_idx became -1 and rewound last_idx near 0.

Changes

  • Precompile _WHITESPACE_PATTERN at module import with [_WORD_CHAR = [\w<Unicode M*>\u200c\u200d]] (matching the combining-mark handling in GLiNER2), preserving exact character offsets into the original input while keeping plain ASCII/NFC splitting unchanged.
  • Skip unmatched tokens (start_idx == -1) in CamelArabicSplitter.__call__ (matching HindiSplitter).
  • Add tests/test_tokenizer_unicode.py covering Arabic tashkeel, Devanagari/Bengali/Tamil/Telugu vowel signs, Persian/Indic ZWNJ/ZWJ, NFD accents, plain-text parity, and CamelArabicSplitter offset safety.

Verification

pytest tests/test_tokenizer_unicode.py tests/test_word_alignment.py tests/test_data_processing.py -q
# 168 passed

Signed-off-by: Dr. Mohammed Lubbad <engmlubbad@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant