Skip to content

Fix UnicodeScripts pre-tokenizer dropping leading spaces - #2382

Open
Kayvan-Zahiri wants to merge 1 commit into
huggingface:mainfrom
Kayvan-Zahiri:fix/unicode-scripts-leading-space
Open

Kayvan-Zahiri wants to merge 1 commit into
huggingface:mainfrom
Kayvan-Zahiri:fix/unicode-scripts-leading-space

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

UnicodeScripts silently drops a leading run of spaces. fixed_script(' ') returns Script::Any, and the boundary filter_map only emits an offset when the script is not Any, so ranges starts past the spaces. PreTokenizedString::split does not check that splits tile the input, so those bytes just vanish.

On main today:

" Yes"  -> [("Yes", (1, 4))]     " 1 " -> [("1 ", (1, 3))]
"1 "    -> [("1 ", (0, 2))]      "   " -> []

The pre-tokenizer contradicts itself on the same character: a trailing space is absorbed, a leading one is destroyed, and its own test is named spaces_are_included_in_every_script.

Through a real Tokenizer (WordLevel vocab holding both "Yes" and " Yes"), " Yes" and "Yes" encode identically, so the " Yes" entry is unreachable and offsets cover 3 of 4 bytes.

Fix anchors the first boundary at 0, which merges the leading spaces into the following run rather than emitting a bare space token. cargo test --lib: 201 pass + 1 fail before, 202 pass after. cargo fmt --check and cargo clippy --all-targets -D warnings are clean.

馃 Generated with Claude Code

`fixed_script(' ')` returns `Script::Any`, and the boundary `filter_map`
only emits an offset for a non-Any script. A leading run of spaces
therefore emits no boundary, `ranges` does not start at 0, and those
bytes never land in a split. `PreTokenizedString::split` does not check
that splits tile the input, so the spaces are dropped silently.

Anchor the first boundary at 0, and seed `ranges` when the input is
entirely `Script::Any` (previously `"   "` produced zero splits).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DQughgr95y4B9H1jfaQH8o
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