Skip to content
class

RegexTokenizer

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

Reference (pure-Python) regex tokenizer.

Splits the input by repeatedly applying a compiled regular expression; every match is treated as one chunk and looked up in the vocab. Characters between matches are dropped — there is no canonical reconstruction during decode.

For production / latency-sensitive use, prefer the matching RegexTokenizerFast — same matching semantics on portable patterns, but the hot loop runs in C++.

Parameters

patternstr
Regex pattern matched against the input via re.finditer. Every match's text becomes a chunk; unmatched spans are dropped.
vocabdict[str, int]= None
Pre-built token-string → id map. If omitted, the tokenizer starts empty and train must be called before encoding.
special_tokensSpecialTokens= None
Special-token registry — see lucid.utils.tokenizer.SpecialTokens. If a non-None unk is configured, OOV matches map to it; otherwise OOV matches are silently dropped.

Notes

The pattern is compiled once at construction time. Re-assigning the pattern after construction is not supported — build a new instance instead.

Examples

>>> tok = RegexTokenizer(r"\w+")
>>> tok.train(["hello world hello"])
>>> tok.encode("hello world")
[0, 1]

See Also

  • RegexTokenizerFast—C++-backed sibling with the same surface API.

Used by 1

Constructors

1
dunder

__init__

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

Construct a pure-Python regex tokenizer.

Parameters

patternstr
Match pattern compiled once via re.compile. Each re.findall hit becomes one token candidate.
vocabdict[str, int] or None= None
Pre-built token → id map.
special_tokens(SpecialTokens or None, optional, keyword - only)= None
Special-token registry; configure unk for OOV handling (OOV matches are dropped otherwise).

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)

Build the vocab in insertion order from corpus matches.

Iterates over corpus, applies the regex to each document, and assigns each first-seen match a fresh id starting at 0. Stops as soon as vocab_size distinct tokens have been collected (no frequency cut-off — the order is purely insertion order).

Parameters

corpusiterable of str
Each item is one document. Generators are consumed exactly once.
vocab_sizeint= 30 000
Hard cap on the number of distinct tokens kept.