Fix StaccatoTokenizer Handling of Zero-Width Characters and BOM #3657
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.
This pull request addresses issues in the
StaccatoTokenizer
related to the handling of certain non-printing Unicode characters, specifically zero-width characters like variation selectors (e.g., U+FE0F) and the Byte Order Mark (BOM, U+FEFF).Problem:
The previous implementation could incorrectly split tokens or generate spurious empty/single-character tokens when encountering:
❤
+️
U+FE0F).This resulted in unexpected tokenization outputs like
['norm', '❤', '️', 'enjoyed']
instead of['norm', '❤', 'enjoyed']
or adding an empty token at the end of a sentence if it contained a BOM.Solution:
tokenize
method was changed from usingre.split
followed by whitespace splitting to usingre.findall
. A comprehensive regex pattern (self.token_pattern
) was constructed to directly find and extract valid tokens based on defined categories: sequences of letters (across various scripts), sequences of digits, individual Kanji characters, and individual punctuation/symbol characters.self.punctuation
) was modified to explicitly exclude common zero-width characters (\uFE00-\uFE0F, \u200B-\u200D, \u2060-\u206F) and the BOM (\uFEFF) by adding them to the negated character set[^...]
.Impact:
StaccatoTokenizer
now correctly ignores these non-printing characters during tokenization, preventing spurious splits and unwanted tokens.Closes #3652