Use FromWordPieceVocabulary to turn text into BERT token IDs from a bare vocab.txt.
dotnet add package RecursiveAI.OneNlpRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
Save this sample as vocab.txt in your project directory if you aren’t using the zip.
using RecursiveAI.OneNlp.Tokenizers; // vocab.txt is in the example zip. var tokenizer = Tokenizer .FromWordPieceVocabulary("vocab.txt", new() { LowerCase = true }); var encoded = tokenizer.Encode("Playing"); Console.WriteLine(string.Join(" ", encoded.Tokens)); Console.WriteLine(string.Join(" ", encoded.Ids));
The snippet prints:
[CLS] play ##ing [SEP] 2 5 6 3
[CLS] and [SEP] frame the sequence. ##ing continues the same word as play. The vocabulary must contain [UNK]; if a word can’t be split into known pieces, the tokenizer uses that ID for the whole word.
Unzip the example, then dotnet run.
[PAD] [UNK] [CLS] [SEP] [MASK] play ##ing
Variations
Get just the text IDs. Replace the Encode call and both print lines with:
var ids = tokenizer.EncodeToIds("Playing", addSpecialTokens: false); Console.WriteLine(string.Join(" ", ids));
5 6Related recipes
The short answer
Call Tokenizer.FromWordPieceVocabulary with the path to the BERT vocabulary and set LowerCase to match your model. Then call Encode to inspect the pieces and their IDs, or EncodeToIds if you only need the integers. Use the vocab.txt distributed for your checkpoint; the file in this recipe is only an example.
How does BERT’s vocab.txt assign token IDs?
There’s one entry per line. IDs start at zero: play is on the sixth line of the sample, so its ID is 5. Moving it to another line changes the number your model receives, even though the string still reads play. Don’t sort the file. Blank lines also occupy an ID, so removing one can shift the IDs below it. Read the file as UTF-8 without a byte order mark; the reader doesn’t strip one.
WordPiece tries the longest piece that fits at the current position. At the start of a word it looks for plain entries, such as play. For the rest of that word it looks for ## entries, such as ##ing. Those two hash characters mark a continuation in the vocabulary; they aren’t part of the input text. If the tokenizer can’t finish the word, it discards the partial result rather than keeping a known prefix beside an unknown suffix.
Should I load a cased or uncased BERT vocabulary?
Use the casing that the model expects. In the sample, LowerCase = true lets Playing find the entry play. The same setting also strips accents by default. When your model’s settings call for a different accent policy, set StripAccents independently on the options. If you use Tokenizer.FromFile on a bare .txt instead, OneNLP infers casing from uppercase letters with lowercase forms outside the bracketed special tokens. That is a guess from the entries, not a setting stored in the file.
Do I need tokenizer.json to encode text for BERT?
Not for the BERT-style pipeline shown here. The vocabulary loader supplies BERT normalization and pre-tokenization, and it builds the sequence template when both [CLS] and [SEP] are present. By default that means [CLS] A [SEP] for one input and [CLS] A [SEP] B [SEP] for a pair. If your model supplies a tokenizer configuration with different processing rules, use that configuration rather than assuming the vocabulary alone describes those rules. The OneNLP tokenizer docs describe what the bare-file loader constructs.