Skip to content
class

WordTokenizerFast

extends_FastVocabTokenizer
WordTokenizerFast(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]= None
Pre-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.
special_tokensSpecialTokens= None
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

1
dunder

__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= None
Pre-built word → id map. None or empty constructs the C++ backend with an empty vocab.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
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

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)

Re-train in C++; refresh Python-side caches afterwards.

Parameters

corpusiterable of str
Materialised into a list before being handed to the C++ binding.
vocab_sizeint= 30 000
Hard cap on the number of distinct tokens kept.