Skip to content

Embeddings wordpiecetokenizer encode

github-actions[bot] edited this page Aug 26, 2026 · 24 revisions

Development build. This page describes main, not a released package. The latest published Lodestar.Embeddings is 0.4.0 — read its documentation.

WordPieceTokenizer.Encode

Tokens and ids for one string.

public TokenizationResult Encode(string text)

Parameterstext is the string to encode.

ReturnsTokenizationResult, tokens and ids of the same length.

Example — a word outside the vocabulary becomes one unknown token.

using Lodestar.Embeddings.Tokenization;

var vocab = new Dictionary<string, int>(StringComparer.Ordinal)
{
    ["[UNK]"] = 0, ["token"] = 1, ["##ize"] = 2, ["text"] = 3,
};
var tokenizer = new WordPieceTokenizer(
    vocab, unkToken: "[UNK]", continuationPrefix: "##", maxCharsPerWord: 100, lowercase: true);

TokenizationResult known = tokenizer.Encode("tokenize");
TokenizationResult unknown = tokenizer.Encode("zzz");

int pieces = known.Tokens.Count;  // => 2
string missing = unknown.Tokens[0];  // => [UNK]

Remarkszzz produces a single [UNK], not three. That is WordPiece's rule and it matters when reading a tokenization: a burst of unknown tokens means several unmatched words, never one long one.

Lowercasing, when enabled, happens before matching, so the vocabulary only needs lowercase entries.

Applies to — net10.0, netstandard2.0.

See alsoWordPieceTokenizer.EncodeToIds, TokenizationResult.

Lodestar

Project

Clone this wiki locally