Skip to content
class

CharTokenizerFast

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

C++-backed character tokenizer; bit-identical to CharTokenizer.

Same vocab format + semantics as CharTokenizer; the per-codepoint encode loop runs in C++ via lucid._C.engine.utils.tokenizer.CharTokenizer.

Parameters

vocabdict[str, int]= None
Pre-built codepoint → id map. If omitted the tokenizer is constructed empty (call train before encoding).
special_tokensSpecialTokens= None
Special-token registry; configure unk for OOV fallback.

Examples

>>> from lucid.utils.tokenizer import CharTokenizerFast
>>> tok = CharTokenizerFast()
>>> tok.train(["the quick brown fox"], vocab_size=64)
>>> tok.decode(tok.encode("the quick"))
'the quick'

Used by 1

Constructors

1
dunder

__init__

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

Construct a C++-backed character tokenizer.

Parameters

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

Notes

Caches the Python-side vocab so get_vocab and id_to_token avoid a binding round-trip; both halves are kept in sync by train.

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 = 1000)

Train in C++; mirror the resulting vocab back into Python state.

Parameters

corpusiterable of str
Materialised into a list before hand-off to C++.
vocab_sizeint= 1000
Maximum number of distinct codepoints to retain.