Skip to main content

Tokenizer

Trait Tokenizer 

Source
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§

Source

fn train( &mut self, corpus: &mut dyn CorpusSource, config: &TrainingConfig, ) -> Result<Vocabulary, KuteError>

Learn a vocabulary from corpus, install it, and return a copy.

Source

fn load(&mut self, vocabulary: Vocabulary) -> Result<(), KuteError>

Install a vocabulary produced earlier. Implementations validate it.

Source

fn encode(&self, input: &CanonicalInput) -> Result<TokenSequence, KuteError>

Source

fn decode(&self, tokens: &TokenSequence) -> Result<CanonicalInput, KuteError>

Source

fn vocabulary(&self) -> Option<&Vocabulary>

Source

fn capabilities(&self) -> TokenizerCapabilities

Provided Methods§

Source

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".

Implementations on Foreign Types§

Source§

impl Tokenizer for BpeTokenizer

Source§

impl Tokenizer for ByteTokenizer

Source§

impl Tokenizer for CharTokenizer

Source§

impl Tokenizer for DictionaryTokenizer

Source§

impl Tokenizer for SequenceBpeTokenizer

Implementors§