---
title: "Load a BERT vocab.txt tokenizer in C#"
url: https://recursiveai.org/onenlp/cookbook/load-bert-wordpiece-vocabulary/
description: "Load a bare BERT vocab.txt file with OneNLP in C#, choose casing explicitly, and turn text into WordPiece tokens and IDs without tokenizer.json."
version: "1.0.0"
---

# Load a BERT vocab.txt tokenizer in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/load-bert-wordpiece-vocabulary/load-bert-wordpiece-vocabulary.zip)

Use `FromWordPieceVocabulary` to turn text into BERT token IDs from a bare `vocab.txt`.

```shell
dotnet add package RecursiveAI.OneNlp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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.

```csharp
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:

```text
[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](https://recursiveai.org/onenlp/cookbook/load-bert-wordpiece-vocabulary/load-bert-wordpiece-vocabulary.zip), then `dotnet run`.

```text
[PAD]
[UNK]
[CLS]
[SEP]
[MASK]
play
##ing
```

## Variations

**Get just the text IDs.** Replace the `Encode` call and both print lines with:

```csharp
var ids = tokenizer.EncodeToIds("Playing",
    addSpecialTokens: false);
Console.WriteLine(string.Join(" ", ids));
```

```text
5 6
```

## Related recipes

- [Load WordPiece and SentencePiece tokenizers](https://recursiveai.org/onenlp/cookbook/load-transformer-tokenizer-files/)
- [Batch encode and pad token IDs](https://recursiveai.org/onenlp/cookbook/batch-encode-and-pad-token-ids/)

## 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](https://recursiveai.org/onenlp/docs/tokenizers-wordpiece-unigram/#vocabtxt-reader) describe what the bare-file loader constructs.
