Skip to main content

Engine

Struct Engine 

Source
pub struct Engine { /* private fields */ }
Expand description

The engine: a registry plus the common workflows.

Implementations§

Source§

impl Engine

Source

pub fn new() -> Self

An engine with every built-in algorithm and adapter registered.

Source

pub fn with_registry(registry: Registry) -> Self

An engine around a caller-built registry.

Source

pub fn registry(&self) -> &Registry

Source

pub fn registry_mut(&mut self) -> &mut Registry

Add algorithms or adapters without touching KUTE’s own crates.

Source

pub fn train( &self, algorithm: &str, adapter: &str, adapter_config: &AdapterConfig, documents: &[Vec<u8>], config: &TrainingConfig, ) -> Result<Vocabulary>

Train algorithm on raw documents read through adapter.

The adapter and its options are recorded in the vocabulary, so encoding and decoding later go through the same adapter without being told.

Source

pub fn train_stream<'a>( &self, algorithm: &str, adapter: &str, adapter_config: &AdapterConfig, config: &TrainingConfig, documents: &mut dyn FnMut() -> Result<Option<Cow<'a, Vec<u8>>>>, ) -> Result<Vocabulary>

Train on documents that are produced one at a time. Only the document being read is held, so a corpus larger than memory can be trained on as long as each document fits.

Source

pub fn train_paths( &self, algorithm: &str, adapter: &str, adapter_config: &AdapterConfig, paths: &[PathBuf], config: &TrainingConfig, ) -> Result<Vocabulary>

Train on files. A directory is read recursively, in sorted path order.

Source

pub fn tokenizer(&self, vocabulary: Vocabulary) -> Result<Box<dyn Tokenizer>>

A tokenizer with vocabulary installed.

Source

pub fn encode_raw( &self, tokenizer: &dyn Tokenizer, raw: &[u8], ) -> Result<TokenSequence>

Raw bytes to tokens, through the vocabulary’s adapter.

Source

pub fn encode_raw_open( &self, tokenizer: &dyn Tokenizer, raw: &[u8], policy: UnknownPolicy, ) -> Result<TokenSequence>

Raw bytes to tokens under an explicit policy for unknown symbols.

Source

pub fn decode_raw_bounded( &self, tokenizer: &dyn Tokenizer, tokens: &TokenSequence, max_units: u64, ) -> Result<Vec<u8>>

Like Engine::decode_raw, but refuses before decoding when the sequence would expand to more than max_units bytes or symbols.

For a symbol vocabulary this bounds the number of symbols and not their size: a symbol can be a kilobyte long, and an escaped one is chosen by whoever made the sequence. For a sequence from a party you do not trust, use Engine::decode_raw_within.

Source

pub fn decode_raw_within( &self, tokenizer: &dyn Tokenizer, tokens: &TokenSequence, max_units: u64, max_bytes: u64, ) -> Result<Vec<u8>>

Like Engine::decode_raw_bounded, and also refuses before decoding when the decoded content comes to more than max_bytes: the bytes of a byte vocabulary, or the names of the symbols of a symbol vocabulary. What an adapter writes around each name (a few bytes of JSON, and escapes inside it) cannot be known before it is written, so the result is checked again afterwards: nothing longer than max_bytes is returned. While decoding, memory is bounded by the two limits together (at most six bytes written per byte of a name, plus the framing of max_units symbols), not by max_bytes alone.

Source

pub fn decode_raw( &self, tokenizer: &dyn Tokenizer, tokens: &TokenSequence, ) -> Result<Vec<u8>>

Tokens back to raw bytes, through the vocabulary’s adapter.

Source

pub fn encode_batch( &self, tokenizer: &dyn Tokenizer, documents: &[Vec<u8>], ) -> Result<Vec<TokenSequence>>

Encode many documents in parallel. Output order matches input order.

Source

pub fn verify(&self, vocabulary: &Vocabulary) -> Result<VerifyReport>

Run the vocabulary’s embedded test vectors: each input must encode to the recorded ids and decode back to the input.

Trait Implementations§

Source§

impl Clone for Engine

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Engine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Engine

§

impl !UnwindSafe for Engine

§

impl Forget for Engine

§

impl Freeze for Engine

§

impl Move for Engine

§

impl Send for Engine

§

impl Sync for Engine

§

impl Unpin for Engine

§

impl UnsafeUnpin for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.