Skip to main content

Vocabulary

Struct Vocabulary 

Source
pub struct Vocabulary {
    pub word2idx: HashMap<String, usize>,
    pub idx2word: Vec<String>,
    pub counts: Vec<u64>,
    pub noise_table: Vec<u32>,
    pub total_tokens: u64,
}
Expand description

Maps tokens ↔ integer indices and stores frequency statistics.

Fields§

§word2idx: HashMap<String, usize>

word → index

§idx2word: Vec<String>

index → word

§counts: Vec<u64>

Raw corpus frequency per index

§noise_table: Vec<u32>

Flat noise table for O(1) negative sampling

§total_tokens: u64

Total token count (after min_count filter)

Implementations§

Source§

impl Vocabulary

Source

pub fn build(sentences: &[Vec<String>], config: &Config) -> Result<Self>

Build vocabulary from a tokenised corpus.

Steps:

  1. Count every token
  2. Drop tokens below config.min_count
  3. Sort by descending frequency (stable index order)
  4. Build unigram noise table
use word2vec::{Config, vocab::Vocabulary};

let corpus = vec!["the cat sat on the mat".to_string()];
let tokens: Vec<Vec<String>> = corpus.iter()
    .map(|s| s.split_whitespace().map(str::to_string).collect())
    .collect();

let vocab = Vocabulary::build(&tokens, &Config::default()).unwrap();
assert!(vocab.word2idx.contains_key("the"));
assert_eq!(vocab.count("the"), 2);
Source

pub fn len(&self) -> usize

Number of unique tokens in vocabulary.

Source

pub fn is_empty(&self) -> bool

Returns true if the vocabulary contains no words.

Source

pub fn count(&self, word: &str) -> u64

Frequency of a word (0 if not in vocab).

use word2vec::{Config, vocab::Vocabulary};
let corpus = vec!["a a b".to_string()];
let tokens: Vec<Vec<String>> = corpus.iter()
    .map(|s| s.split_whitespace().map(str::to_string).collect())
    .collect();
let vocab = Vocabulary::build(&tokens, &Config::default()).unwrap();
assert_eq!(vocab.count("a"), 2);
assert_eq!(vocab.count("z"), 0);
Source

pub fn should_subsample(&self, idx: usize, threshold: f64, dice: f64) -> bool

Returns true if this word should be subsampled (discarded) given a uniformly random dice in [0, 1).

Uses Mikolov’s formula: P(keep) = min(1, sqrt(t/f) + t/f).

Source

pub fn negative_sample(&self, rng: &mut SmallRng) -> usize

Draw a negative sample index from the noise distribution.

Uses the precomputed unigram table for O(1) lookup.

Source

pub fn tokenise_and_subsample( &self, sentence: &[String], threshold: f64, rng: &mut SmallRng, ) -> Vec<usize>

Tokenise and subsample a sentence, returning word indices.

Trait Implementations§

Source§

impl Clone for Vocabulary

Source§

fn clone(&self) -> Vocabulary

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 Vocabulary

Source§

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

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

impl<'de> Deserialize<'de> for Vocabulary

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 Vocabulary

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

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