Skip to main content

FastText

Struct FastText 

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

FastText model for learning word representations with character n-grams

Decomposes each word into character n-grams (subwords) and learns embeddings for both whole words and their constituent n-grams. This enables:

  • Out-of-vocabulary word handling (any word can be represented via its n-grams)
  • Morphological awareness (similar prefixes/suffixes produce similar vectors)
  • Robustness to misspellings and rare word forms

Implementations§

Source§

impl FastText

Source

pub fn new() -> Self

Create a new FastText model with default configuration

Source

pub fn with_config(config: FastTextConfig) -> Self

Create a new FastText model with custom configuration

Source

pub fn with_tokenizer(self, tokenizer: Box<dyn Tokenizer + Send + Sync>) -> Self

Set a custom tokenizer

Source

pub fn extract_ngrams(&self, word: &str) -> Vec<String>

Extract character n-grams from a word

Wraps the word with boundary markers < and > before extracting. For example, “fox” with min_n=3, max_n=4 produces: 3-grams: “<fo”, “fox”, “ox>” 4-grams: “<fox”, “fox>”, “<fox>”(if len allows)

Source

pub fn build_vocabulary(&mut self, texts: &[&str]) -> Result<()>

Build vocabulary from texts

Source

pub fn train(&mut self, texts: &[&str]) -> Result<()>

Train the FastText model

Source

pub fn get_word_vector(&self, word: &str) -> Result<Array1<f64>>

Get the embedding vector for a word (handles OOV words via subwords)

For in-vocabulary words, returns the average of the word vector and its n-gram vectors. For OOV words, returns the average of matching n-gram vectors.

Source

pub fn most_similar( &self, word: &str, top_n: usize, ) -> Result<Vec<(String, f64)>>

Find most similar words to a given word

Source

pub fn most_similar_by_vector( &self, vector: &Array1<f64>, top_n: usize, exclude_words: &[&str], ) -> Result<Vec<(String, f64)>>

Find most similar words to a given vector

Source

pub fn analogy( &self, a: &str, b: &str, c: &str, top_n: usize, ) -> Result<Vec<(String, f64)>>

Compute word analogy: a is to b as c is to ?

Uses vector arithmetic: result = b - a + c, then finds most similar words. Works with OOV words since FastText can compute vectors for any word.

Source

pub fn word_similarity(&self, word1: &str, word2: &str) -> Result<f64>

Compute cosine similarity between two words

Both words can be OOV.

Source

pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()>

Save the model to a file

Saves in a format that includes word vectors, n-gram info, and config. Uses a custom header format: Line 1: FASTTEXT <vocab_size> <vector_size> <min_n> <max_n> <bucket_size> Lines 2+: word vector_components…

Source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Self>

Load a FastText model from a file

Source

pub fn vocabulary_size(&self) -> usize

Get the vocabulary size

Source

pub fn vector_size(&self) -> usize

Get the vector size

Source

pub fn ngram_range(&self) -> (usize, usize)

Get the n-gram configuration (min_n, max_n)

Source

pub fn ngram_count(&self) -> usize

Get the number of unique n-grams discovered

Source

pub fn contains(&self, word: &str) -> bool

Check if a word is in the vocabulary

Source

pub fn can_represent(&self, word: &str) -> bool

Check if a word can be represented (either in vocab or has matching n-grams)

Source

pub fn get_vocabulary_words(&self) -> Vec<String>

Get all words in the vocabulary

Trait Implementations§

Source§

impl Clone for FastText

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FastText

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FastText

Source§

fn default() -> Self

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

impl WordEmbedding for FastText

Source§

fn embedding(&self, word: &str) -> Result<Array1<f64>>

Get the embedding vector for a word
Source§

fn dimension(&self) -> usize

Get the dimensionality of the embedding vectors
Source§

fn find_similar(&self, word: &str, top_n: usize) -> Result<Vec<(String, f64)>>

Find the top-N most similar words to the query word
Source§

fn solve_analogy( &self, a: &str, b: &str, c: &str, top_n: usize, ) -> Result<Vec<(String, f64)>>

Solve the analogy: a is to b as c is to ?
Source§

fn vocab_size(&self) -> usize

Get the number of words in the vocabulary
Source§

fn similarity(&self, word1: &str, word2: &str) -> Result<f64>

Compute cosine similarity between two words

Auto Trait Implementations§

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<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V