Skip to main content

Embeddings

Struct Embeddings 

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

Trained embeddings with vocabulary — the primary inference interface.

Implementations§

Source§

impl Embeddings

Source

pub fn vocab_size(&self) -> usize

Number of words in the vocabulary.

assert!(emb.vocab_size() >= 2);
Source

pub fn embedding_dim(&self) -> usize

Embedding dimension.

Source

pub fn get_vector(&self, word: &str) -> Option<&[f32]>

Get the raw embedding vector for a word.

Returns None if the word is not in the vocabulary.

assert!(emb.get_vector("hello").is_some());
assert!(emb.get_vector("nonexistent").is_none());
Source

pub fn similarity(&self, word_a: &str, word_b: &str) -> Result<f32>

Cosine similarity between two words.

Returns a value in [-1.0, 1.0], or an error if either word is not in the vocabulary.

let sim = emb.similarity("cat", "mat").unwrap();
assert!(sim >= -1.0 && sim <= 1.0);
Source

pub fn most_similar(&self, query: &str, top_k: usize) -> Vec<(String, f32)>

Find the top_k most similar words to query.

Returns (word, cosine_similarity) pairs sorted descending. The query word itself is excluded from results.

let nearest = emb.most_similar("cat", 3);
assert!(nearest.len() <= 3);
Source

pub fn analogy( &self, pos_a: &str, neg_a: &str, pos_b: &str, top_k: usize, ) -> Result<Vec<(String, f32)>>

Solve an analogy: pos_a - neg_a + pos_b ≈ result.

Classic example: king - man + woman ≈ queen.

Excludes pos_a, neg_a, and pos_b from the candidate list.

Returns (word, score) pairs sorted by cosine similarity to the query vector.

Source

pub fn words(&self) -> Vec<&str>

Return all words in vocabulary sorted alphabetically.

Source

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

Save embeddings to JSON.

The file contains both the model weights and vocabulary so it can be loaded independently.

Source

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

Load embeddings from a JSON file produced by save.

Source

pub fn save_text_format(&self, path: impl AsRef<Path>) -> Result<()>

Export just the word vectors as a plain-text file (word2vec format).

Format: first line is <vocab_size> <dim>, then one word per line followed by space-separated floats.

Source

pub fn vocab(&self) -> &Vocabulary

Get a reference to the vocabulary.

Trait Implementations§

Source§

impl Debug for Embeddings

Source§

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

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

impl<'de> Deserialize<'de> for Embeddings

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Embeddings

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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

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

§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,