pub trait Tokenizer: Send + Sync {
// Required methods
fn train(
&mut self,
corpus: &mut dyn CorpusSource,
config: &TrainingConfig,
) -> Result<Vocabulary, KuteError>;
fn load(&mut self, vocabulary: Vocabulary) -> Result<(), KuteError>;
fn encode(&self, input: &CanonicalInput) -> Result<TokenSequence, KuteError>;
fn decode(
&self,
tokens: &TokenSequence,
) -> Result<CanonicalInput, KuteError>;
fn vocabulary(&self) -> Option<&Vocabulary>;
fn capabilities(&self) -> TokenizerCapabilities;
// Provided method
fn encode_open(
&self,
input: &CanonicalInput,
policy: UnknownPolicy,
) -> Result<TokenSequence, KuteError> { ... }
}Expand description
The generic tokenizer interface (PRD 9.3).
Two deliberate differences from the PRD sketch: vocabulary returns an
Option because a tokenizer exists before it is trained, and load installs
an existing vocabulary. Both are recorded in ADR-0003.
Required Methods§
Sourcefn train(
&mut self,
corpus: &mut dyn CorpusSource,
config: &TrainingConfig,
) -> Result<Vocabulary, KuteError>
fn train( &mut self, corpus: &mut dyn CorpusSource, config: &TrainingConfig, ) -> Result<Vocabulary, KuteError>
Learn a vocabulary from corpus, install it, and return a copy.
Sourcefn load(&mut self, vocabulary: Vocabulary) -> Result<(), KuteError>
fn load(&mut self, vocabulary: Vocabulary) -> Result<(), KuteError>
Install a vocabulary produced earlier. Implementations validate it.
fn encode(&self, input: &CanonicalInput) -> Result<TokenSequence, KuteError>
fn decode(&self, tokens: &TokenSequence) -> Result<CanonicalInput, KuteError>
fn vocabulary(&self) -> Option<&Vocabulary>
fn capabilities(&self) -> TokenizerCapabilities
Provided Methods§
Sourcefn encode_open(
&self,
input: &CanonicalInput,
policy: UnknownPolicy,
) -> Result<TokenSequence, KuteError>
fn encode_open( &self, input: &CanonicalInput, policy: UnknownPolicy, ) -> Result<TokenSequence, KuteError>
Encode under an explicit policy for symbols the vocabulary lacks.
Strict is plain Tokenizer::encode. Escape must return a
sequence that decodes to the input exactly, without changing the
vocabulary. Algorithms that can meet no unknown symbol (byte-level
ones) need not override this; algorithms that can, and have no
escape support, report that instead of guessing.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".