pub struct Tokenizer { /* private fields */ }Expand description
Thread-safe tokenizer with persistent single-document and Rayon-worker
caches. Token IDs remain those of the source tokenizer.json.
Implementations§
Source§impl Tokenizer
impl Tokenizer
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self>
Load a byte-level BPE tokenizer from a Hugging Face tokenizer.json.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Load a byte-level BPE tokenizer from in-memory tokenizer.json bytes.
Sourcepub fn encode(&self, text: &str, add_special_tokens: bool) -> Result<Vec<u32>>
pub fn encode(&self, text: &str, add_special_tokens: bool) -> Result<Vec<u32>>
Encode one UTF-8 string, optionally applying the tokenizer’s supported single-sequence post-processor.
Sourcepub fn encode_batch(
&self,
texts: Vec<String>,
add_special_tokens: bool,
) -> Result<Vec<Vec<u32>>>
pub fn encode_batch( &self, texts: Vec<String>, add_special_tokens: bool, ) -> Result<Vec<Vec<u32>>>
Encode documents in input order. Small batches stay on the caller; larger batches use persistent per-Rayon-thread tokenizer forks.
Sourcepub fn decode(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String>
pub fn decode(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String>
Decode IDs through the byte-level vocabulary. Invalid UTF-8 fragments use the replacement character, matching the display behavior of the previous tokenizer backend.
Sourcepub fn id_to_token(&self, id: u32) -> Option<String>
pub fn id_to_token(&self, id: u32) -> Option<String>
Return the exact vocabulary spelling stored in tokenizer.json.
Sourcepub fn token_to_id(&self, token: &str) -> Option<u32>
pub fn token_to_id(&self, token: &str) -> Option<u32>
Resolve an exact model or added-token spelling to its ID.
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Vocabulary extent including added tokens.
Auto Trait Implementations§
impl !Freeze for Tokenizer
impl !RefUnwindSafe for Tokenizer
impl Send for Tokenizer
impl Sync for Tokenizer
impl Unpin for Tokenizer
impl UnsafeUnpin for Tokenizer
impl UnwindSafe for Tokenizer
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