Skip to content
class

RegexTokenizerFast

extends_RegexPersistenceMixin_FastVocabTokenizer
RegexTokenizerFast(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

patternstr
Regex pattern. Interpreted as ECMAScript (std::regex default), not the Python re dialect — see Notes.
vocabdict[str, int]= None
Pre-built token-string → id map.
special_tokensSpecialTokens= None
See lucid.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
dunder

__init__

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

Construct a C++-backed regex tokenizer.

Parameters

patternstr
Match pattern handed to the C++ regex engine; see class docstring for the cross-engine compatibility caveats.
vocabdict[str, int] or None= None
Pre-built token → id map.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry; mirrored into the C++ backend.

Properties

3
prop

algo

→strfrom _VocabTokenizer
algo: str

Algorithm identifier.

prop

pattern

→strfrom _RegexPersistenceMixin
pattern: str

The original regex source — useful for serialisation.

prop

vocab_size

→intfrom _VocabTokenizer
vocab_size: int

Total number of tokens currently registered in the vocab.

Class methods

1
cls

from_file

→Selffrom _RegexPersistenceMixin
from_file(directory: str, special_tokens: SpecialTokens | None = None)

Load from a directory previously written by save.

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 _RegexPersistenceMixin
save(directory: str)

Persist vocab + regex pattern + 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.