fix(tokenize): include the last character in the final sentence span - #6866
Open
uuzzrm wants to merge 1 commit into
Open
fix(tokenize): include the last character in the final sentence span#6866uuzzrm wants to merge 1 commit into
uuzzrm wants to merge 1 commit into
Conversation
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.
What this does
The basic (rule-based) sentence splitter returned
len(text) - 1as the end index of the final sentence, so the last character of the input was always excluded from its span. That is inconsistent with the blingfire implementation (which useslen(text)for the tail) and with the documented return contract ("start and end indices of the original text").It is observable in the xml-aware tokenizer wrapper used for expressive TTS: with the span stopping one character short, the final period of the last sentence was remapped into its own sentence token, e.g.
"<expr type='expression' label='happy'/>Hello world."became"<expr .../>Hello world"+".".Fix: use
len(text)for the trailing sentence's end index, matching the in-loop spans.How to test
pytest tests/test_tokenize_sentence_spans.py- added three regression tests: the final span covers the whole text, the final span of a multi-sentence input ends atlen(text), and the xml-aware wrapper no longer splits the final period into its own token.test_tokenizer.pyexpectations (token text) are unaffected; verified against the same fixture with the publicbasic.SentenceTokenizer.ruff checkandruff format --checkpass on the changed files.