RegexTokenizerFast
_RegexPersistenceMixin_FastVocabTokenizerRegexTokenizerFast(pattern: str, vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)C++-backed regex tokenizer.
Identical surface API to RegexTokenizer; the per-chunk
regex scan runs in C++ via
lucid._C.engine.utils.tokenizer.RegexTokenizer. Use
this in production training / inference where encode throughput
matters.
Parameters
patternstrstd::regex
default), not the Python re dialect — see Notes.vocabdict[str, int]= Nonelucid.utils.tokenizer.SpecialTokens. The unk
id is mirrored into the C++ side so the OOV fall-back path
is consistent across both flavours.Notes
The Fast flavour uses std::regex (ECMAScript dialect) while
the Python flavour uses re — patterns that lean on
Python-specific features (named-group syntax with ?P<name>,
lookbehind variability, etc.) may diverge between the two.
Stick to plain character classes + quantifiers for portable
patterns.
Examples
>>> from lucid.utils.tokenizer import RegexTokenizerFast
>>> tok = RegexTokenizerFast(r"\w+|\S")
>>> 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
RegexTokenizer—Pure-Python reference sibling.
Used by 1
Constructors
1__init__
→None__init__(pattern: str, vocab: dict[str, int] | None = None, special_tokens: SpecialTokens | None = None)Construct a C++-backed regex tokenizer.
Parameters
patternstrvocabdict[str, int] or None= NoneProperties
3Algorithm identifier.
The original regex source — useful for serialisation.
Total number of tokens currently registered in the vocab.
Class methods
1Instance methods
4Return a shallow copy of the token → id map.
Return the surface string for token_id or None.
Persist vocab + regex pattern + unified tokenizer.json.
Re-train in C++; refresh Python-side caches afterwards.
Parameters
corpusiterable of strlist before being handed to the
C++ binding.vocab_ sizeint= 30 000