class
WordTokenizerFast
extends
_FastVocabTokenizerWordTokenizerFast(vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)C++-backed word tokenizer with UNK fallback.
Identical surface API and OOV-error semantics to
WordTokenizer; the whitespace scan + dict lookup run
in C++ via lucid._C.engine.utils.tokenizer.WordTokenizer.
Parameters
vocabdict[str, int]= NonePre-built token-string → id map. Forwarded to the C++
backend at construction; if omitted, the C++ tokenizer is
constructed with an empty vocab and
train must be
called before encoding.Must include a non-None
unk to allow OOV input; the id
is mirrored into the C++ registry so the C++ OOV fall-back
path agrees with the Python-side OOV check.Notes
The C++ LookupTokenizer.encode silently substitutes UNK for
OOV words when one is configured, or drops them otherwise. To
match the Python reference's explicit error for unconfigured
UNK, _encode_one performs an OOV pre-check before
delegating.
Examples
>>> from lucid.utils.tokenizer import WordTokenizerFast
>>> tok = WordTokenizerFast()
>>> tok.train(["the quick brown fox", "the lazy dog sleeps"],
... vocab_size=64)
>>> tok.decode(tok.encode("the quick dog"))
'the quick dog'See Also
WordTokenizer—Pure-Python reference sibling.
Used by 1
Constructors
1dunder
__init__
→None__init__(vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)Construct a C++-backed word tokenizer.
Parameters
vocabdict[str, int] or None= NonePre-built word → id map.
None or empty constructs
the C++ backend with an empty vocab.Special-token registry; mirrored into the C++ backend
via
_sync_specials_to_cpp so the C++ encode
fall-back path sees the same UNK id.Properties
2Class methods
1Instance methods
4Return a shallow copy of the token → id map.
Return the surface string for token_id or None.
Persist as vocab.txt + unified tokenizer.json.
Re-train in C++; refresh Python-side caches afterwards.
Parameters
corpusiterable of strMaterialised into a
list before being handed to the
C++ binding.vocab_ sizeint= 30 000Hard cap on the number of distinct tokens kept.