Module Brot.Post_processor
Post-processing tokenization output with special tokens.
Post-processors add special tokens and type IDs to tokenized sequences after core tokenization. They handle model-specific requirements like [CLS] and [SEP] for BERT, sentence pair formatting, and byte-level offset adjustments.
type tThe type for post-processors.
type token = string * intA special token as (text, id).
Constructors
bert ~sep ~cls is a BERT-style post-processor.
Single: [CLS] A [SEP]. Pair: [CLS] A [SEP] B [SEP]. Type IDs: 0 for the first sequence, 1 for the second.
roberta ~sep ~cls () is a RoBERTa-style post-processor.
Single: <s> A </s>. Pair: <s> A </s> </s> B </s>. All type IDs are 0.
trim_offsets removes leading and trailing whitespace from the offsets of byte-level tokens; it defaults to true. add_prefix_space tells the trimming that a single leading space on a token that starts at offset 0 was added by the pre-tokenizer and must be kept; it defaults to true.
byte_level () is a byte-level post-processor that adjusts character offsets for byte-level encoding.
trim_offsets removes leading and trailing whitespace from offsets. Defaults to true. add_prefix_space tells the trimming that a single leading space on a token that starts at offset 0 was added by the pre-tokenizer and must be kept; it defaults to true.
template ~single () is a template-based post-processor.
Templates use $A and $B as sequence placeholders and literal special token names (e.g. [CLS]). Type IDs can be specified with a colon suffix: $A:0, [SEP]:1.
pair must reference both $A and $B; it defaults to "$A:0 $B:1". special_tokens defaults to [].
Processing
val process :
t ->
?pair:Encoding.t ->
Encoding.t ->
add_special_tokens:bool ->
Encoding.tprocess t enc ~add_special_tokens adds special tokens and sets type IDs on enc.
When ~pair is provided, both sequences are merged into a single encoding. pair enters as the second segment, with type ID 1, which t may override. Offsets are not shifted: each sequence keeps the offsets into its own text.
When ~add_special_tokens is false, no special token is inserted, but everything else still happens: the two sequences are still merged, type IDs are still assigned, and byte-level offsets are still trimmed.
added_tokens t ~is_pair is the number of special tokens t adds. Useful for calculating the truncation budget.
Formatting
Serialization
of_json json is a post-processor from HuggingFace tokenizer.json format. Errors if json is not an object, has a missing or unknown "type" field, or has invalid parameters.