fix(dedup): preserve distinct texts shorter than the n-gram size - #526
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The fix is narrowly scoped, preserves existing behavior for other cases, and is backed by targeted regression tests that cover the reported failure modes.
Pull request overview
This PR fixes an edge case in MinHashDeduplicateFilter where non-empty texts shorter than the configured n-gram size produced an empty MinHash signature and were incorrectly deduplicated together. The change ensures short-but-distinct texts remain distinct while keeping empty-string and existing n-gram/character-mode behavior unchanged.
Changes:
- Update MinHash shingling so
0 < len(text) < ngramuses the whole text as a single shingle. - Add CPU-only regression tests covering short English/Chinese texts, custom n-gram sizes, empty strings, boundary/longer texts, character mode, and repeated operator runs.
File summaries
| File | Description |
|---|---|
dataflow/operators/general_text/filter/minhash_deduplicate_filter.py |
Adjusts MinHash signature construction for short texts to avoid empty signatures and incorrect deduplication. |
test/cpu_only/test_minhash_short_texts.py |
Adds regression tests validating correct behavior for short texts and ensuring existing behavior remains stable. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Contributor
|
LGTM, Sincerely, thanks! |
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.
With the default ngram=5, ["cat", "dog", "cat"] currently becomes ["cat"]: no n-grams are generated for either short text, so both receive the same empty MinHash signature.
Use each non-empty text shorter than the n-gram size as a single shingle. Distinct short texts now survive while actual duplicates are removed. Empty texts retain their existing behavior; normal n-gram generation, character mode, and the per-run index lifetime are unchanged.
Validation: 8 CPU regression cases pass, covering English and Chinese short texts, a custom n-gram size, empty strings, actual duplicates, boundary and longer texts, character mode, and repeated runs. The unmodified code fails 5 cases and passes 3; all 8 pass with the fix. Tested with datasketch 2.0.0.