pub struct Embeddings { /* private fields */ }Expand description
Trained embeddings with vocabulary — the primary inference interface.
Implementations§
Source§impl Embeddings
impl Embeddings
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Number of words in the vocabulary.
assert!(emb.vocab_size() >= 2);Sourcepub fn embedding_dim(&self) -> usize
pub fn embedding_dim(&self) -> usize
Embedding dimension.
Sourcepub fn get_vector(&self, word: &str) -> Option<&[f32]>
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());Sourcepub fn similarity(&self, word_a: &str, word_b: &str) -> Result<f32>
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);Sourcepub fn most_similar(&self, query: &str, top_k: usize) -> Vec<(String, f32)>
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);Sourcepub fn analogy(
&self,
pos_a: &str,
neg_a: &str,
pos_b: &str,
top_k: usize,
) -> Result<Vec<(String, f32)>>
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.
Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<()>
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.
Sourcepub fn load(path: impl AsRef<Path>) -> Result<Self>
pub fn load(path: impl AsRef<Path>) -> Result<Self>
Load embeddings from a JSON file produced by save.
Sourcepub fn save_text_format(&self, path: impl AsRef<Path>) -> Result<()>
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.
Sourcepub fn vocab(&self) -> &Vocabulary
pub fn vocab(&self) -> &Vocabulary
Get a reference to the vocabulary.