Repository navigation
hftokenizer: fix BertNormalizer control/format character filtering and parity - #92
Merged
Merged
Conversation
…d parity - Drop Unicode General Category Other (Cc, Cf, Co) except \t/\n/\r via isUnicodeOther/isBertControl, matching Hugging Face tokenizers' BertNormalizer (unicode_categories::is_other) so format characters such as U+00AD, U+200B, and U+200E are stripped instead of producing [UNK]. - Respect n.CleanText in applyBertNormalizerWithOffsets. - Remove unconditional CJK space padding from cleanText (handled by tokenizeChineseChars when handle_chinese_chars is enabled). - Expand isWhitespace from unicode.Zs to unicode.Z so U+2028 (Zl) and U+2029 (Zp) normalize to ASCII space. - Add unit tests covering all cases and update docs/CHANGELOG.md. Fixes #90
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.
Summary
Fixes #90 and resolves three adjacent
BertNormalizerparity bugs discovered while verifying against Hugging Facetokenizers:Cc,Cf,Co(except\t,\n,\r) inBertNormalizerclean_text(hftokenizer: BertNormalizer clean_text keeps format characters (Cf) that Hugging Face tokenizers removes #90):isControl(which only matchedCcviaunicode.IsControl) withisUnicodeOther(unicode.In(r, unicode.Cc, unicode.Cf, unicode.Co)) andisBertControl(exempting\t,\n,\r).tokenizers'bert.rs(unicode_categories::is_other()), so format characters like soft hyphen (U+00AD), zero-width space (U+200B), left-to-right mark (U+200E), and BOM (U+FEFF) are stripped instead of turning words into[UNK], while unassigned code points (Cn) are preserved.n.CleanTextinapplyBertNormalizerWithOffsets:n.CleanText, matchingapplyBertNormalizerand Hugging FaceBertNormalizer(clean_text=False).cleanText:cleanTextpreviously wrapped CJK characters in spaces regardless ofhandle_chinese_chars, and caused double spacing when bothclean_textandhandle_chinese_charswere enabled inapplyBertNormalizer. CJK spacing is now handled solely bytokenizeChineseChars/n.HandleChineseChars.Zl(U+2028) andZp(U+2029) to space inisWhitespace:isWhitespacefromunicode.Zstounicode.Z(Zs | Zl | Zp) to match Hugging FaceBertNormalizer/BertPreTokenizer.Testing
TestCleanTextto coverCc,Cf,Co,Cn,U+FFFD,Zs/Zl/Zp, and CJK pass-through.TestBertNormalizer_CleanTextAndChineseCharsParityverifying all 4 combinations ofclean_text×handle_chinese_charsacross bothapplyNormalizerandapplyNormalizerWithOffsets(including byte-offset coverage).TestIssue90_BertNormalizerFormatCharsreproducing issue hftokenizer: BertNormalizer clean_text keeps format characters (Cf) that Hugging Face tokenizers removes #90 end-to-end and verifying token IDs and byte spans.go test -short ./...passes across the repository.docs/CHANGELOG.md.