pub struct Bpe { /* private fields */ }Expand description
Byte-Pair Encoding model.
§Example
use std::collections::HashMap;
use morpheme::models::Bpe;
use morpheme::Model;
let vocab: HashMap<String, u32> =
[("a", 0), ("b", 1), ("c", 2), ("ab", 3), ("abc", 4)].map(|(t, i)| (t.to_string(), i)).into();
let merges = vec![("a".into(), "b".into()), ("ab".into(), "c".into())];
let bpe = Bpe::builder().vocab_and_merges(vocab, merges).build()?;
let tokens: Vec<String> = bpe.tokenize("abcab")?.into_iter().map(|t| t.value).collect();
assert_eq!(tokens, ["abc", "ab"]);Implementations§
Source§impl Bpe
impl Bpe
Sourcepub fn builder() -> BpeBuilder
pub fn builder() -> BpeBuilder
Start building a model.
Sourcepub fn new(vocab: Vocab, merges: Merges) -> Result<Self>
pub fn new(vocab: Vocab, merges: Merges) -> Result<Self>
Build from a vocabulary and merges with default options.
Sourcepub fn continuing_subword_prefix(&self) -> Option<&str>
pub fn continuing_subword_prefix(&self) -> Option<&str>
The continuing-subword prefix, if any.
Sourcepub fn end_of_word_suffix(&self) -> Option<&str>
pub fn end_of_word_suffix(&self) -> Option<&str>
The end-of-word suffix, if any.
Sourcepub fn set_dropout(&mut self, dropout: Option<f32>) -> Result<()>
pub fn set_dropout(&mut self, dropout: Option<f32>) -> Result<()>
Set the BPE-dropout probability at runtime, with the same
validation as the builder: None or a value in [0, 1], where
Some(0.0) is normalized to None (it never skips a merge).
Anything else, including NaN, is a config error and leaves the
model unchanged. The word cache is cleared, since cached results
assume the previous merge behaviour.
Sourcepub fn fuse_unk(&self) -> bool
pub fn fuse_unk(&self) -> bool
Whether consecutive unknown chars are fused into one unknown token.
Sourcepub fn byte_fallback(&self) -> bool
pub fn byte_fallback(&self) -> bool
Whether unknown chars fall back to <0xNN> byte tokens.
Sourcepub fn ignore_merges(&self) -> bool
pub fn ignore_merges(&self) -> bool
Whether words already in the vocabulary skip merging.
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Drop all cached words.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Bpe
impl<'de> Deserialize<'de> for Bpe
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
Source§impl From<Bpe> for ModelWrapper
impl From<Bpe> for ModelWrapper
Source§impl Model for Bpe
impl Model for Bpe
Auto Trait Implementations§
impl !Freeze for Bpe
impl RefUnwindSafe for Bpe
impl Send for Bpe
impl Sync for Bpe
impl Unpin for Bpe
impl UnsafeUnpin for Bpe
impl UnwindSafe for Bpe
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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