#[non_exhaustive]pub struct Tokenizer<T: TokenType = Token> { /* private fields */ }Expand description
Construct via Tokenizer::train or Tokenizer::load. Use by calling encode
/ decode or inspect the vocabulary with
Tokenizer::vocab_size, Tokenizer::token_to_string, Tokenizer::str_to_token, and Tokenizer::tokens.
§Token representation
Tokens are T values where T: TokenType. By default, T = Token.
Tokens 0..256 correspond to single bytes (with printable
ASCII mapped to their character representation and control/high bytes to <hex> notation). 257th token is the word-start symbol ▁.
Tokens 257.. are special tokens, plus Unicode codepoints extracted from the training data, plus those learned by BPE merges (if any).
Implementations§
Source§impl<T: TokenType> Tokenizer<T>
impl<T: TokenType> Tokenizer<T>
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Returns the total vocabulary size (byte tokens + codepoint + BPE merge tokens).
Sourcepub fn token_to_string(&self, tok: T) -> Option<&str>
pub fn token_to_string(&self, tok: T) -> Option<&str>
Maps a token ID to its string representation.
Returns None if the token ID exceeds the vocabulary range.
For byte tokens 0..256, this returns the byte’s display form
(printable ASCII as-is, others as <hex>).
Sourcepub fn str_to_token(&self, s: &str) -> Option<T>
pub fn str_to_token(&self, s: &str) -> Option<T>
Maps a string to its token ID, if the string corresponds to a single token.
Returns None if the string is not present as an atomic vocabulary entry.
Note that multi-token sequences (e.g. "hello world") will not match —
this only succeeds when the entire input maps to exactly one token.
Sourcepub fn tokens(&self) -> impl Iterator<Item = (T, &str)>
pub fn tokens(&self) -> impl Iterator<Item = (T, &str)>
Iterates over all (token_id, string) pairs in the vocabulary.
Byte tokens come first (0..256), followed by sorted Unicode codepoints and BPE merge tokens in merge order.
Sourcepub fn load(path: &Path) -> Result<Self, TokenizerError>
pub fn load(path: &Path) -> Result<Self, TokenizerError>
Loads a tokenizer from a JSON file previously saved with Tokenizer::save.
Sourcepub fn save(&self, path: &Path) -> Result<(), TokenizerError>
pub fn save(&self, path: &Path) -> Result<(), TokenizerError>
Saves the tokenizer vocabulary to a JSON file.
Only the learned vocabulary (257..) is stored; the first 256 byte-tokens plus word-start ▁
are implicit. The file also records the crate version for forward compatibility.
Sourcepub fn train(
s: &str,
special_tokens: &[&str],
max_extra_tokens: Option<usize>,
) -> Self
pub fn train( s: &str, special_tokens: &[&str], max_extra_tokens: Option<usize>, ) -> Self
Trains a new BPE tokenizer on the given text.
The initial vocabulary always contains 257 initial tokens (256 single bytes plus the word start symbol ▁),
in addition to the provided special tokens.
In case max_extra_tokens is Some(n), other unicode codepoints found in s are appended
to the vocabulary first, and then BPE merge operations are applied until the vocabulary
reaches 257 + special_tokens.len() + n entries.
If n is less than the number of unicode codepoints, then only the top-n are appended
from those in s, sorted longest-first, then lexicographically.
Sourcepub fn encode(&self, s: &str) -> Vec<T>
pub fn encode(&self, s: &str) -> Vec<T>
Encodes a string into a sequence of token IDs.
The input is first normalized, then chunked by a GPT-style regex pattern, and each chunk is matched against the vocabulary using leftmost-longest Aho-Corasick search. Characters not in the vocabulary fall back to their UTF-8 byte tokens.
Sourcepub fn decode(&self, v: &[T]) -> String
pub fn decode(&self, v: &[T]) -> String
Decodes a sequence of token IDs back into a string.
Consecutive byte tokens that form valid UTF-8 are reassembled into
characters. The result is then denormalized (removing or converting ▁ markers
back to spaces where appropriate).
Sourcepub fn token_size() -> usize
pub fn token_size() -> usize
Returns the size in bytes of a single token value.
Auto Trait Implementations§
impl<T> Freeze for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: Freeze,
impl<T> RefUnwindSafe for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: RefUnwindSafe,
impl<T> Send for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: Send,
impl<T> Sync for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: Sync,
impl<T> Unpin for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: Unpin,
impl<T> UnsafeUnpin for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: UnsafeUnpin,
impl<T> UnwindSafe for Tokenizer<T>where
DoubleArrayAhoCorasick<T>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more