Module Brot.Pre_tokenizer
Pre-tokenization.
Pre-tokenizers split raw text into pretokens before vocabulary-based tokenization (BPE, WordPiece, etc.) is applied. Each pretoken carries byte offsets into the original text.
type tThe type for pre-tokenizers.
Constructors
whitespace splits on whitespace using pattern \w+|[^\w\s]+.
Groups word characters (letters, digits, underscore) together and groups non-word, non-space characters together. Whitespace is used as delimiter but not included in output.
whitespace_split splits on any whitespace characters.
Removes whitespace from output. Simplest and fastest pre-tokenizer.
bert applies BERT-style pre-tokenization.
Splits on whitespace, isolates punctuation, and separates CJK characters individually.
byte_level () is a byte-level pre-tokenizer. Used by GPT-2, GPT-3, RoBERTa.
Converts text to byte representation and applies GPT-2's regex pattern for splitting.
add_prefix_space: add space at beginning if text does not start with whitespace. Default:true.use_regex: use GPT-2's regex pattern for splitting. Default:true.trim_offsets: adjust offsets for byte-level encoding. Default:true.
type behavior = [ - #
| `Isolated(*Keep each delimiter as a pretoken of its own
*) - #
| `Removed(*Drop the delimiters
*) - #
| `Merged_with_previous(*Keep a delimiter with what precedes it
*) - #
| `Merged_with_next(*Keep a delimiter with what follows it
*) - #
| `Contiguous(*Keep neighbours that are both delimiters, or both not, as one pretoken
*)
]Delimiter handling behavior for splitting operations.
`Merged_with_previous and `Merged_with_next merge a delimiter into the text beside it only: a delimiter that follows another one is a pretoken of its own.
punctuation () separates punctuation characters from the text around them, whitespace included. Every punctuation character is a delimiter.
behavior defaults to `Isolated.
split ~pattern () splits on a literal string pattern. See split_regex to split on a regular expression.
behavior defaults to `Removed. When invert is true the delimiters are the runs of text between the occurrences of pattern, and those occurrences are what they separate; defaults to false.
An empty pattern matches at every position, so the pretokens are the characters — and none of them when invert is true and behavior is `Removed.
split_regex ~pattern () is split on the matches of the regular expression pattern rather than on the occurrences of a string. Matches are taken leftmost first without overlap, each the one a backtracking engine would find: the first alternative that matches, with greedy repetition.
pattern is written in the dialect of tokenizer files, as for Normalizer.replace_regex, except that anchors are not supported. A lookahead that ends the pattern or one of its alternatives is how \s+(?!\S) leaves the last space of a run to the word after it.
An empty match cuts the text where it stands, so a pattern that matches the empty string everywhere gives the characters.
Raises Invalid_argument if pattern is not valid or uses a construct that is not supported; the message says which.
char_delimiter c splits on the character c, removing it from the output. Equivalent to split ~pattern:c ~behavior:`Removed ().
Raises Invalid_argument if c is not exactly one character.
digits () splits on digit boundaries.
When individual_digits is true, each digit is a separate pretoken; when false (default), consecutive digits are grouped.
type prepend_scheme = [ - #
| `First(*Prepend to the pretoken that opens the document only
*) - #
| `Never(*Never prepend
*) - #
| `Always(*Prepend to every pretoken not starting with a space
*)
]Controls when metaspace prepends the replacement character. In a sequence the pretokens are those of the member before it, so `First marks the first of them and `Always each — and the encode pipeline counts a pretoken as opening the document only when nothing, an added token included, comes before it.
val metaspace :
?replacement:string ->
?prepend_scheme:prepend_scheme ->
?split:bool ->
unit ->
tmetaspace () replaces spaces with a visible marker. Used by SentencePiece models.
replacement: the marker spaces are replaced with. Default:"▁"(U+2581). It must be exactly one character.prepend_scheme: when to prepend the marker, which happens only if the marked text does not start with one already. Default:`Always.split: whether to split before each marker. Default:true.
Pretokens are those of the marked text; their offsets are the bytes of the text they were made from. A marker that replaced a space stands at that space, and a prepended one at the character it opens.
Raises Invalid_argument if replacement is not exactly one character.
unicode_scripts splits on Unicode script boundaries.
A pretoken opens where the writing system changes (e.g. Latin to Cyrillic, Latin to Han) and runs to the next change. The writing system is the Script property of Unicode 17.0.0. Hiragana and Katakana count as Han, as does the prolonged sound mark "ー" (U+30FC).
Spaces and characters of no known script join the pretoken they follow, so a leading run of them belongs to no pretoken: unlike the other pre-tokenizers, the pretokens need not cover the input.
fixed_length n cuts the text into pretokens of n characters.
The last pretoken may be shorter than n.
sequence ts chains multiple pre-tokenizers left-to-right.
Each pre-tokenizer processes the pretokens from the previous one. Offsets are composed correctly through the chain.
Operations
pre_tokenize t text splits text into pretokens with byte offsets.
Returns a list of (pretoken, (start, end_)) where start and end_ are byte positions in text, ascending and within it. A pretoken is the bytes of its span unless t rewrote or encoded them, in which case the span is where the bytes it was made from lie.
Two spans can cover the same bytes. A span is widened to whole characters, so pretokens of a text that is not valid UTF-8 can share one; and the pretokens a member of a sequence cuts from one that was rewritten more than once, cut by a fixed-length member, or encoded all report that pretoken's span, nothing in it being placeable more finely than the whole of it.
Formatting
Byte-level decoding
val byte_level_decode : string -> stringbyte_level_decode s is the bytes the byte-level alphabet spells s from: one byte per character. A single character outside the alphabet, an invalidly encoded one included, leaves s as it is — the fallback is over the whole string, not the character.
The result needs not be valid UTF-8: it is bytes, and turning them into text is the caller's step.
Serialization
of_json json is a pre-tokenizer from HuggingFace JSON format. Errors if json is not an object, has a missing or unknown "type" field, has invalid parameters, or is a "Split" whose regular expression ({"Regex": ...}) uses a construct split_regex does not support.