use alloc::string::String;
use alloc::vec::Vec;
use crate::error::TokenizerError;
pub type TokenId = u32;
pub trait Tokenizer {
fn encode(&self, text: &str) -> Result<Vec<TokenId>, TokenizerError>;
fn decode(&self, ids: &[TokenId]) -> Result<String, TokenizerError>;
}
pub(crate) fn split_words(text: &str) -> impl Iterator<Item = &str> {
text.split_whitespace().filter(|w| !w.is_empty())
}
#[cfg(feature = "std")]
pub(crate) fn parse_vocab_lines(text: &str) -> alloc::collections::BTreeMap<String, TokenId> {
let mut vocab = alloc::collections::BTreeMap::new();
for (i, line) in text.lines().enumerate() {
let token = line.trim_end();
if token.is_empty() {
continue;
}
vocab.insert(token.to_string(), i as TokenId);
}
vocab
}