Skip to content
class

WordTokenizer

extends_VocabTokenizer
WordTokenizer(vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)

Reference (pure-Python) word-level tokenizer with UNK fallback.

Splits the input on ASCII whitespace runs, looks each word up in the vocab, and either emits the configured unk token or raises ValueError on out-of-vocab inputs.

For production / latency-sensitive use, prefer the matching WordTokenizerFast — same algorithm, same vocab format, same OOV-error semantics, but the hot loop runs in C++.

Parameters

vocabdict[str, int]= None
Pre-built token-string → id map. If omitted, the tokenizer starts empty and train must be called before encoding.
special_tokensSpecialTokens= None
Must include a non-None unk if the caller plans to encode text containing OOV words. Without it, OOV at encode time raises ValueError.

Notes

train does not pre-seed an UNK token. The caller is responsible for adding it to both the vocab and the SpecialTokens registry before encoding OOV text.

Examples

>>> tok = WordTokenizer(special_tokens=SpecialTokens(unk="<unk>"))
>>> tok.train(["the quick brown fox", "<unk>"])
>>> tok.encode("the slow fox")  # 'slow' → unk
[0, 4, 3]

See Also

  • WordTokenizerFast—C++-backed sibling with the same surface API.

Used by 1

Constructors

1
dunder

__init__

→None
__init__(vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)

Construct a pure-Python word tokenizer.

Parameters

vocabdict[str, int] or None= None
Pre-built word → id map; None or empty leaves the tokenizer empty (call train to populate).
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry; configure unk for OOV fallback at encode time.

Properties

2
prop

algo

→strfrom _VocabTokenizer
algo: str

Algorithm identifier.

prop

vocab_size

→intfrom _VocabTokenizer
vocab_size: int

Total number of tokens currently registered in the vocab.

Class methods

1
cls

from_file

→Selffrom _VocabTokenizer
from_file(directory: str, special_tokens: SpecialTokens | None = None)

Load from a directory containing vocab.txt.

Instance methods

4
fn

get_vocab

→dict[str, int]from _VocabTokenizer
get_vocab()

Return a shallow copy of the token → id map.

fn

id_to_token

→str | Nonefrom _VocabTokenizer
id_to_token(token_id: int)

Return the surface string for token_id or None.

fn

save

→Nonefrom _VocabTokenizer
save(directory: str)

Persist as vocab.txt + unified tokenizer.json.

fn

train

→None
train(corpus: Iterable[str], vocab_size: int = 30000)

Collect unique words in insertion order until vocab_size.

Same algorithm as WhitespaceTokenizer.train — pure insertion-order vocab build with a hard cap. No frequency cut-off is applied.

Training does not pre-seed an UNK token; the caller is responsible for adding it to the vocab + the special-token registry before encoding OOV text.

Parameters

corpusiterable of str
Each item is one document. Generators are consumed exactly once.
vocab_sizeint= 30 000
Hard cap on the number of distinct tokens kept.