use super::super::types::Tokenizer;
use crate::core::added::AddedTokens;
use crate::core::batch;
use crate::core::policy::{PolicyError, SpecialMode};
impl Tokenizer {
pub fn encode(&self, text: &str) -> Vec<u32> {
if self.match_added_tokens {
self.encode_with_special(text)
} else {
self.encode_ordinary(text)
}
}
pub fn encode_ordinary(&self, text: &str) -> Vec<u32> {
self.encode_content(text, false)
}
pub fn encode_rayon(&self, text: &str) -> Vec<u32> {
if self.match_added_tokens {
AddedTokens::dispatch(&self.special_matcher, text, |gap, out| {
self.encode_content_into(gap, true, opens_input(text, gap), out)
})
} else {
self.encode_content(text, true)
}
}
pub fn encode_with_special(&self, text: &str) -> Vec<u32> {
AddedTokens::dispatch(&self.special_matcher, text, |gap, out| {
self.encode_content_into(gap, false, opens_input(text, gap), out)
})
}
pub fn encode_with(&self, text: &str, mode: &SpecialMode<'_>) -> Result<Vec<u32>, PolicyError> {
if matches!(mode, SpecialMode::All) && !self.match_added_tokens {
return Ok(self.encode_ordinary(text));
}
AddedTokens::dispatch_with_mode(&self.special_matcher, text, mode, |gap, out| {
self.encode_content_into(gap, false, opens_input(text, gap), out)
})
}
pub fn encode_batch(&self, texts: &[String]) -> Vec<Vec<u32>> {
batch::map(texts, String::len, |text| self.encode(text))
}
pub fn encode_batch_with_special(&self, texts: &[String]) -> Vec<Vec<u32>> {
batch::map(texts, String::len, |text| self.encode_with_special(text))
}
}
#[inline]
fn opens_input(text: &str, gap: &str) -> bool {
std::ptr::eq(text.as_ptr(), gap.as_ptr())
}