fix: snap chunk start to a word boundary in CharacterBasedTextChunker - #2226
Merged
omri374 merged 3 commits intoAug 9, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves CharacterBasedTextChunker to avoid starting overlapped chunks in the middle of words by snapping the computed start forward to a configured boundary character (mirroring the existing logic which snaps end forward), reducing the chance of NER false positives on partial tokens.
Changes:
- Adjust
CharacterBasedTextChunker.chunk()to advancestartto the next boundary character after applyingchunk_overlap. - Add a unit test asserting that every chunk after the first begins on (or immediately after) a boundary.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| presidio-analyzer/presidio_analyzer/chunkers/character_based_text_chunker.py | Advances start to a boundary char after overlap to prevent mid-word chunk starts. |
| presidio-analyzer/tests/test_character_based_text_chunker.py | Adds coverage to ensure chunk starts (after the first) land on a word boundary. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Change Description
CharacterBasedTextChunkersnapsendforward to the next boundary char so a chunk never ends mid-word. Butstart, computed asend - chunk_overlap, gets no equivalent treatment — the subtraction discards the alignment established two lines earlier.With
chunk_size=10, chunk_overlap=3:text = "This is a test string for chunking purposes"
before: ['This is a test', 'st string for', 'or chunking', 'ing purposes']
after: ['This is a test', ' string for', ' chunking purposes']
Three of the four chunks begin mid-word. An NER recognizer receives
'st','or','ing'as standalone tokens and can confidently mislabel them as entities, producing a false positive that redacts the wrong span.Fix: advance
startto the next boundary char, mirroring the loopendalready has. Three lines.Present since #1805, which introduced this chunker.
Issue reference
No existing issue. Found while using the chunker via
GLiNERRecognizeron long transcripts.Checklist