Commit a86d128
* Strip Japanese voiced-kana marks in BasicTokenizer (#352, Bug 2)
`BasicTokenizer.maybeStripAccents` used `.folding(options: .diacriticInsensitive,
locale: nil)`, which strips Latin diacritics but not the U+3099/U+309A combining
sound marks that Japanese voiced kana carry. Precomposed `ザ` (U+30B6) and `で`
(U+3067) therefore reached WordPiece intact, but BERT-family vocabularies only
contain the dakuten-stripped forms (`##サ`, `##て`) — `##ザ`/`##で` are missing.
A single missing continuation forced WordPiece's greedy match to fall back to
`[UNK]` for the entire word, so long Japanese inputs containing any voiced kana
returned essentially nothing useful.
Switch to NFD-decompose-then-Mn-filter, matching HF Python's `_run_strip_accents`.
This strips every nonspacing-mark scalar regardless of block, so Latin diacritics
and Japanese dakuten/handakuten are handled identically.
Adds a regression test using BAAI/bge-small-en-v1.5 over a multi-script Japanese
input that exercises both hiragana and katakana including voiced kana.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Address review: widen BertNormalizer.stripAccents to all Mn; add direct-tokenizer test
Per @pcuenca's review on #354:
- Widen `BertNormalizer.stripAccents` in Normalizer.swift from filtering only
U+0300..U+036F to filtering all nonspacing marks (Unicode general category Mn).
This is the architecturally correct location, matching the Rust HF tokenizers
`normalizers/bert.rs#strip_accents` path used by the fast Python tokenizer.
Keeps `BasicTokenizer.maybeStripAccents` as defensive coverage for callers
using `BertTokenizer` directly without `AutoTokenizer.from(pretrained:)`.
- Add `bertTokenizerStripsDakuten` to BertTokenizerTests exercising the direct
path (no `from(pretrained:)`), per @pcuenca's suggested test.
- Add a single-character `#expect(tokenizer.encode(text: "ザ") == [101, 1705, 102])`
to the existing `bertJapaneseDakuten` test for clarity, per @pcuenca's request.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Apply suggestions from code review
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* lint
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
1 parent 03e7404 commit a86d128
4 files changed
Lines changed: 64 additions & 5 deletions
File tree
- Sources/Tokenizers
- Tests/TokenizersTests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
244 | 256 | | |
245 | 257 | | |
246 | 258 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
240 | 244 | | |
241 | | - | |
242 | | - | |
243 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
244 | 251 | | |
245 | 252 | | |
246 | 253 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
222 | 242 | | |
223 | 243 | | |
224 | 244 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
379 | 399 | | |
380 | 400 | | |
381 | 401 | | |
| |||
0 commit comments