Struct tokenizers::tokenizer::Tokenizer
source · pub struct Tokenizer(_);Implementations§
source§impl Tokenizer
impl Tokenizer
sourcepub fn new(model: impl Into<ModelWrapper>) -> Self
pub fn new(model: impl Into<ModelWrapper>) -> Self
Construct a new Tokenizer based on the model.
sourcepub fn into_inner(
self
) -> TokenizerImpl<ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper>
pub fn into_inner( self ) -> TokenizerImpl<ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper>
Unwrap the TokenizerImpl.
pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self>
pub fn from_bytes<P: AsRef<[u8]>>(bytes: P) -> Result<Self>
pub fn from_pretrained<S: AsRef<str>>( identifier: S, params: Option<FromPretrainedParameters> ) -> Result<Self>
Methods from Deref<Target = TokenizerImpl<ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper>>§
sourcepub fn with_normalizer(&mut self, normalizer: impl Into<N>) -> &mut Self
pub fn with_normalizer(&mut self, normalizer: impl Into<N>) -> &mut Self
Set the normalizer
sourcepub fn get_normalizer(&self) -> Option<&N>
pub fn get_normalizer(&self) -> Option<&N>
Get the normalizer
sourcepub fn with_pre_tokenizer(&mut self, pre_tokenizer: impl Into<PT>) -> &mut Self
pub fn with_pre_tokenizer(&mut self, pre_tokenizer: impl Into<PT>) -> &mut Self
Set the pre tokenizer
sourcepub fn get_pre_tokenizer(&self) -> Option<&PT>
pub fn get_pre_tokenizer(&self) -> Option<&PT>
Get the pre tokenizer
sourcepub fn with_post_processor(
&mut self,
post_processor: impl Into<PP>
) -> &mut Self
pub fn with_post_processor( &mut self, post_processor: impl Into<PP> ) -> &mut Self
Set the post processor
sourcepub fn get_post_processor(&self) -> Option<&PP>
pub fn get_post_processor(&self) -> Option<&PP>
Get the post processor
sourcepub fn with_decoder(&mut self, decoder: impl Into<D>) -> &mut Self
pub fn with_decoder(&mut self, decoder: impl Into<D>) -> &mut Self
Set the decoder
sourcepub fn get_decoder(&self) -> Option<&D>
pub fn get_decoder(&self) -> Option<&D>
Get the decoder
sourcepub fn with_model(&mut self, model: impl Into<M>) -> &mut Self
pub fn with_model(&mut self, model: impl Into<M>) -> &mut Self
Set the model
sourcepub fn with_truncation(&mut self, trunc: Option<TruncationParams>) -> &mut Self
pub fn with_truncation(&mut self, trunc: Option<TruncationParams>) -> &mut Self
Set the truncation parameters
sourcepub fn get_truncation(&self) -> Option<&TruncationParams>
pub fn get_truncation(&self) -> Option<&TruncationParams>
Get the currently set truncation parameters
sourcepub fn get_truncation_mut(&mut self) -> Option<&mut TruncationParams>
pub fn get_truncation_mut(&mut self) -> Option<&mut TruncationParams>
Get a mutable reference to the currently set truncation parameters
sourcepub fn with_padding(&mut self, padding: Option<PaddingParams>) -> &mut Self
pub fn with_padding(&mut self, padding: Option<PaddingParams>) -> &mut Self
Set the padding parameters
sourcepub fn get_padding(&self) -> Option<&PaddingParams>
pub fn get_padding(&self) -> Option<&PaddingParams>
Get the currently set padding parameters
sourcepub fn get_padding_mut(&mut self) -> Option<&mut PaddingParams>
pub fn get_padding_mut(&mut self) -> Option<&mut PaddingParams>
Get a mutable reference to the currently set padding parameters
sourcepub fn get_vocab_size(&self, with_added_tokens: bool) -> usize
pub fn get_vocab_size(&self, with_added_tokens: bool) -> usize
Get the size of the vocabulary
sourcepub fn token_to_id(&self, token: &str) -> Option<u32>
pub fn token_to_id(&self, token: &str) -> Option<u32>
Converts a token in the corresponding id.
sourcepub fn id_to_token(&self, id: u32) -> Option<String>
pub fn id_to_token(&self, id: u32) -> Option<String>
Converts an id to the corresponding token.
sourcepub fn encode<'s, E>(
&self,
input: E,
add_special_tokens: bool
) -> Result<Encoding>where
E: Into<EncodeInput<'s>>,
pub fn encode<'s, E>( &self, input: E, add_special_tokens: bool ) -> Result<Encoding>where E: Into<EncodeInput<'s>>,
Encode the given input. This method accepts both single sequences, as well as pair sequences. Also, a sequence can be a string, or already pre-tokenized input directly:
// Sequences:
tokenizer.encode("Single sequence", false);
tokenizer.encode(("Sequence A", "Sequence B"), false);
// Pre-tokenized sequences:
tokenizer.encode(&["Single", "sequence"][..], false);
tokenizer.encode((
&["Sequence", "A"][..],
&["Sequence", "B"][..]
), false);
// or even both types together:
tokenizer.encode(("A complete sequence", &["And", "a", "tokenized"][..]), false);sourcepub fn encode_char_offsets<'s, E>(
&self,
input: E,
add_special_tokens: bool
) -> Result<Encoding>where
E: Into<EncodeInput<'s>>,
pub fn encode_char_offsets<'s, E>( &self, input: E, add_special_tokens: bool ) -> Result<Encoding>where E: Into<EncodeInput<'s>>,
Encode the given input, using offsets relative to chars instead of bytes. This method accepts both single sequences, as well as pair sequences. Also, a sequence can be a string, or already pre-tokenized input directly:
// Sequences:
tokenizer.encode("Single sequence", false);
tokenizer.encode(("Sequence A", "Sequence B"), false);
// Pre-tokenized sequences:
tokenizer.encode(&["Single", "sequence"][..], false);
tokenizer.encode((
&["Sequence", "A"][..],
&["Sequence", "B"][..]
), false);
// or even both types together:
tokenizer.encode(("A complete sequence", &["And", "a", "tokenized"][..]), false);sourcepub fn add_special_tokens(&mut self, tokens: &[AddedToken]) -> usize
pub fn add_special_tokens(&mut self, tokens: &[AddedToken]) -> usize
Register the given tokens as special tokens. This is especially useful for removing these special tokens while decoding
sourcepub fn add_tokens(&mut self, tokens: &[AddedToken]) -> usize
pub fn add_tokens(&mut self, tokens: &[AddedToken]) -> usize
Add the given tokens to the added vocabulary
sourcepub fn encode_batch<'s, E>(
&self,
inputs: Vec<E>,
add_special_tokens: bool
) -> Result<Vec<Encoding>>where
E: Into<EncodeInput<'s>> + Send,
pub fn encode_batch<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool ) -> Result<Vec<Encoding>>where E: Into<EncodeInput<'s>> + Send,
Encode all the sentences in parallel, using multiple threads
sourcepub fn encode_batch_char_offsets<'s, E>(
&self,
inputs: Vec<E>,
add_special_tokens: bool
) -> Result<Vec<Encoding>>where
E: Into<EncodeInput<'s>> + Send,
pub fn encode_batch_char_offsets<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool ) -> Result<Vec<Encoding>>where E: Into<EncodeInput<'s>> + Send,
Encode all the sentences in parallel, using multiple threads.
The offsets on each Encoding will be relative to chars instead of bytes.
sourcepub fn decode_batch(
&self,
sentences: Vec<Vec<u32>>,
skip_special_tokens: bool
) -> Result<Vec<String>>where
M: Send + Sync,
pub fn decode_batch( &self, sentences: Vec<Vec<u32>>, skip_special_tokens: bool ) -> Result<Vec<String>>where M: Send + Sync,
Decode all sentences in parallel