Repository navigation
Conversation
…ls in BertTokenizer
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Supplementary-plane punctuation can merge adjacent known words into a single unknown token.
1 open issue
What changed in this PR
Aligns BERT basic tokenization with whitespace and symbol handling described in #7724 and #7725, without changing the public word-or-punctuation pre-tokenizer.
Changes:
- Preserves word boundaries around tabs and line breaks.
- Adds BERT-specific punctuation splitting and symbol retention.
- Adds token and offset regression tests.
| File | Description |
|---|---|
| test/Microsoft.ML.Tokenizers.Tests/BertTokenizerTests.cs | Adds whitespace, symbol, and offset tests. |
| src/Microsoft.ML.Tokenizers/PreTokenizer/PreTokenizer.cs | Adds an internal BERT pre-tokenizer. |
| src/Microsoft.ML.Tokenizers/Normalizer/BertNormalizer.cs | Updates whitespace and control-character cleaning. |
| src/Microsoft.ML.Tokenizers/Model/BertTokenizer.cs | Selects the new BERT pre-tokenizer by default. |
Review effort: Balanced
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7820 +/- ##
==========================================
+ Coverage 70.77% 70.79% +0.02%
==========================================
Files 1420 1421 +1
Lines 279877 280027 +150
Branches 27604 27614 +10
==========================================
+ Hits 198077 198259 +182
+ Misses 73620 73612 -8
+ Partials 8180 8156 -24
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.

BertTokenizernow follows the basic tokenization of BERT and Hugging Face in two places:BertNormalizerturns\t,\nand\rinto spaces instead of removing them, sonote\nbookisnote book, notnotebook. It also removes format and private-use characters, as the text cleaning of BERT does, and keeps the characters that are not assigned in Unicode, which become[UNK].BertTokenizeruses a new internal pre-tokenizer: every punctuation character is a token, with the definition of BERT (Unicode punctuation and every ASCII character that is not a letter, a digit or whitespace), and every other run of non-whitespace characters is a word.BertNormalizersets apart the punctuation outside the Basic Multilingual Plane, which the pre-tokenizer can't match on UTF-16 code units. So$ + < = > ^ ` | ~are tokens, and°,©and emoji reach WordPiece, which finds them in the vocabulary or gives[UNK].PreTokenizer.CreateWordOrPunctuationis unchanged.The cases of both issues, and a few more, are in
BertTokenizerTests, with the tokens Hugging Facetokenizersgives on the same vocabulary; they fail without the change. Compared with Hugging Facetokenizers0.23.3, texts encoded to the same ids:one§lineOptions:
LowerCaseBeforeTokenizationandRemoveNonSpacingMarkson for the uncased models, off for the cased one.No text that had the same ids as Hugging Face before has different ids now, except U+1734 alone with the uncased models: .NET classifies it as a spacing mark and the Unicode tables of
tokenizersas a non-spacing one, which accent stripping removes. The code points that still differ are mostly Hangul syllables and combining marks withRemoveNonSpacingMarks, and characters assigned in recent Unicode versions, as the punctuation added after Unicode 8.0, whichtokenizersdoes not treat as punctuation; they are outside these two issues.Fixes #7724
Fixes #7725