Skip to content
class

WhitespaceTokenizerFast

extends_FastVocabTokenizer
WhitespaceTokenizerFast(vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)

C++-backed whitespace tokenizer; bit-identical to WhitespaceTokenizer.

Same vocab format + OOV-dropping contract as WhitespaceTokenizer; the encode loop runs in C++ via the engine WhitespaceTokenizer binding.

Parameters

vocabdict[str, int]= None
Pre-built word → id map. Empty = call train first.
special_tokensSpecialTokens= None
Special-token registry.

Examples

>>> from lucid.utils.tokenizer import WhitespaceTokenizerFast
>>> tok = WhitespaceTokenizerFast()
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
...           vocab_size=64)
>>> tok.encode("the quick dog")
[0, 1, 5]

Used by 1

Constructors

1
dunder

__init__

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

Construct a C++-backed whitespace tokenizer.

Parameters

vocabdict[str, int] or None= None
Pre-built word → id map. None or empty constructs an empty C++ backend; call train to populate.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry.

Properties

2
prop

algo

→strfrom _VocabTokenizer
algo: str

Algorithm identifier.

prop

vocab_size

→intfrom _FastVocabTokenizer
vocab_size: int

Total number of tokens in the live C++ 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 whitespace-split words via the C++ trainer.

Parameters

corpusiterable of str
Documents to scan. Materialised into a list before crossing into C++.
vocab_sizeint= 30 000
Maximum number of distinct words to retain.

Notes

Mutates _vocab / _id_to_token in place after the C++ side finishes; re-resolves special-token ids against the new vocab.