8000 hftokenizer: fix BertNormalizer control/format character filtering and parity by janpfeifer · Pull Request #92 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content

hftokenizer: fix BertNormalizer control/format character filtering and parity - #92

Merged
janpfeifer merged 1 commit into
mainfrom
fix-90
Nov 30, 2026
Merged

janpfeifer merged 1 commit into
mainfrom
fix-90

Conversation

@janpfeifer
Copy link
Copy Markdown
Contributor

Summary

Fixes #90 and resolves three adjacent BertNormalizer parity bugs discovered while verifying against Hugging Face tokenizers:

  1. Drop Unicode Cc, Cf, Co (except \t, \n, \r) in BertNormalizer clean_text (hftokenizer: BertNormalizer clean_text keeps format characters (Cf) that Hugging Face tokenizers removes #90):
    • Replaced isControl (which only matched Cc via unicode.IsControl) with isUnicodeOther (unicode.In(r, unicode.Cc, unicode.Cf, unicode.Co)) and isBertControl (exempting \t, \n, \r).
    • Matches Hugging Face 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.
  2. Respect n.CleanText in applyBertNormalizerWithOffsets:
    • Guarded control-character stripping and whitespace normalization on n.CleanText, matching applyBertNormalizer and Hugging Face BertNormalizer(clean_text=False).
  3. Remove unconditional CJK space padding from cleanText:
    • cleanText previously wrapped CJK characters in spaces regardless of handle_chinese_chars, and caused double spacing when both clean_text and handle_chinese_chars were enabled in applyBertNormalizer. CJK spacing is now handled solely by tokenizeChineseChars / n.HandleChineseChars.
  4. Normalize Zl (U+2028) and Zp (U+2029) to space in isWhitespace:
    • Expanded isWhitespace from unicode.Zs to unicode.Z (Zs | Zl | Zp) to match Hugging Face BertNormalizer / BertPreTokenizer.

Testing

  • Expanded TestCleanText to cover Cc, Cf, Co, Cn, U+FFFD, Zs/Zl/Zp, and CJK pass-through.
  • Added TestBertNormalizer_CleanTextAndChineseCharsParity verifying all 4 combinations of clean_text × handle_chinese_chars across both applyNormalizer and applyNormalizerWithOffsets (including byte-offset coverage).
  • Added TestIssue90_BertNormalizerFormatChars reproducing 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.
  • Verified go test -short ./... passes across the repository.
  • Updated docs/CHANGELOG.md.

…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
@janpfeifer
janpfeifer merged commit 6be46d3 into main Nov 30, 2026
5 checks passed
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.

hftokenizer: BertNormalizer clean_text keeps format characters (Cf) that Hugging Face tokenizers removes

1 participant

0