TokenFrequency

Struct TokenFrequency 

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

TokenFrequency 構造体 tokenの出現頻度を管理するための構造体です tokenの出現回数をカウントします。

§Examples

use vectorizer::token::TokenFrequency;
let mut token_freq = TokenFrequency::new();
token_freq.add_token("token1");
token_freq.add_token("token2");
token_freq.add_token("token1");
 

Implementations§

Source§

impl TokenFrequency

Tokenの追加、削除の実装

Source

pub fn new() -> Self

新しいTokenFrequencyを作成するメソッド

Examples found in repository?
examples/basic.rs (line 5)
3fn main() {
4    let corpus = Corpus::new();
5    let mut freq1 = TokenFrequency::new();
6    freq1.add_tokens(&["rust", "高速", "並列", "rust"]);
7    let mut freq2 = TokenFrequency::new();
8    freq2.add_tokens(&["rust", "柔軟", "安全", "rust"]);
9
10    let mut vectorizer: TFIDFVectorizer<u16> = TFIDFVectorizer::new(&corpus);    
11    vectorizer.add_doc("doc1".to_string(), &freq1);
12    vectorizer.add_doc("doc2".to_string(), &freq2);
13
14    let mut query_tokens = TokenFrequency::new();
15    query_tokens.add_tokens(&["rust", "高速"]);
16    let query = SimilarityQuery::CosineSimilarity(query_tokens);
17    let mut result = vectorizer.similarity(query);
18    result.sort_by_score();
19
20    result.list.iter().for_each(|(k, s)| {
21        println!("doc: {}, score: {}", k, s);
22    });    
23}
Source

pub fn add_token(&mut self, token: &str) -> &mut Self

tokenを追加する

§Arguments
  • token - 追加するトークン
Source

pub fn add_tokens<T>(&mut self, tokens: &[T]) -> &mut Self
where T: AsRef<str>,

複数のtokenを追加する

§Arguments
  • tokens - 追加するトークンのスライス
Examples found in repository?
examples/basic.rs (line 6)
3fn main() {
4    let corpus = Corpus::new();
5    let mut freq1 = TokenFrequency::new();
6    freq1.add_tokens(&["rust", "高速", "並列", "rust"]);
7    let mut freq2 = TokenFrequency::new();
8    freq2.add_tokens(&["rust", "柔軟", "安全", "rust"]);
9
10    let mut vectorizer: TFIDFVectorizer<u16> = TFIDFVectorizer::new(&corpus);    
11    vectorizer.add_doc("doc1".to_string(), &freq1);
12    vectorizer.add_doc("doc2".to_string(), &freq2);
13
14    let mut query_tokens = TokenFrequency::new();
15    query_tokens.add_tokens(&["rust", "高速"]);
16    let query = SimilarityQuery::CosineSimilarity(query_tokens);
17    let mut result = vectorizer.similarity(query);
18    result.sort_by_score();
19
20    result.list.iter().for_each(|(k, s)| {
21        println!("doc: {}, score: {}", k, s);
22    });    
23}
Source

pub fn sub_token(&mut self, token: &str) -> &mut Self

tokenを引く

§Arguments
  • token - 引くトークン
Source

pub fn sub_tokens<T>(&mut self, tokens: &[T]) -> &mut Self
where T: AsRef<str>,

複数のtokenを引く

§Arguments
  • tokens - 引くトークンのスライス
Source

pub fn set_token_count(&mut self, token: &str, count: u64) -> &mut Self

tokenの出現回数を指定する

§Arguments
  • token - トークン
  • count - 出現回数
Source§

impl TokenFrequency

TokenFrequencyの情報を取得するための実装

Source

pub fn token_count_vector(&self) -> Vec<(String, u64)>

すべてのtokenの出現回数を取得します

§Returns
  • Vec<(String, u32)> - トークンとその出現回数のベクタ
Source

pub fn token_count_vector_ref_str(&self) -> Vec<(&str, u64)>

すべてのtokenの出現回数を取得します 文字列はこれの参照を返します

§Returns
  • Vec<(&str, u32)> - トークンとその出現回数のベクタ
Source

pub fn token_count_hashmap_ref_str(&self) -> HashMap<&str, u64, RandomState>

すべてのtokenの出現回数を取得します 文字列はこれの参照を返します

§Returns
  • HashMap<&str, u32> - トークンとその出現回数のハッシュマップ
Source

pub fn token_sum(&self) -> u64

全tokenのカウントの合計を取得します

§Returns
  • u64 - tokenのカウントの合計
Source

pub fn token_count(&self, token: &str) -> u64

あるtokenの出現回数を取得します

§Arguments
  • token - トークン
§Returns
  • u32 - トークンの出現回数
Source

pub fn most_frequent_tokens_vector(&self) -> Vec<(String, u64)>

もっとも多く出現したtokenを取得します 同じ出現回数のtokenが複数ある場合は、すべてのtokenを取得します

§Returns
  • Vec<(String, u32)> - トークンとその出現回数のベクタ
Source

pub fn most_frequent_token_count(&self) -> u64

もっとも多く出現したtokenの出現回数取得します

§Returns
  • u32 - 再頻出tokenの出現回数
Source

pub fn contains_token(&self, token: &str) -> bool

tokenが存在するかどうかを確認します

§Arguments
  • token - トークン
§Returns
  • bool - tokenが存在する場合はtrue、存在しない場合はfalse
Source

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

tokenのsetを取得します

§Returns
  • Vec<String> - tokenのset
Source

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

tokenのsetを取得します 文字列はこれの参照を返します

§Returns
  • Vec<&str> - tokenのset
Source

pub fn token_hashset(&self) -> HashSet<String, RandomState>

tokenのsetを取得します

§Returns
  • HashSet<String> - tokenのset
Source

pub fn token_hashset_ref_str(&self) -> HashSet<&str, RandomState>

tokenのsetを取得します 文字列はこれの参照を返します

§Returns
  • HashSet<&str> - tokenのset
Source

pub fn token_num(&self) -> usize

出現した単語数を取得します

§Returns
  • usize - 出現した単語数
Source

pub fn remove_stop_tokens(&mut self, stop_tokens: &[&str]) -> u64

stop_tokenを削除します

§Arguments
  • stop_tokens - 削除するトークンのスライス
§Returns
  • u64 - 削除されたtokenの合計数
Source

pub fn remove_tokens_by<F>(&mut self, condition: F) -> u64
where F: Fn(&str, &u64) -> bool,

条件に基づいてtokenを削除します

§Arguments
  • condition - 条件を満たすtokenを削除するクロージャ
§Returns
  • u64 - 削除されたtokenの合計数
Source

pub fn sorted_frequency_vector(&self) -> Vec<(String, u64)>

頻度でソートされたトークンのベクタを取得(降順)

§Returns
  • Vec<(String, u32)> - 頻度でソートされたトークンのベクタ
Source

pub fn sorted_dict_order_vector(&self) -> Vec<(String, u64)>

辞書順でソートされたトークンのベクタを取得(昇順)

§Returns
  • Vec<(String, u32)> - 辞書順でソートされたトークンのベクタ
Source

pub fn unique_token_ratio(&self) -> f64

tokenの多様性を計算します 1.0は完全な多様性を示し、0.0は完全な非多様性を示します

§Returns
  • f64 - tokenの多様性
Source

pub fn clear(&mut self)

カウントを全リセットします

Source

pub fn shrink_to_fit(&mut self)

shrink internal storage to fit current size

Trait Implementations§

Source§

impl Clone for TokenFrequency

Source§

fn clone(&self) -> TokenFrequency

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 TokenFrequency

Source§

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

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

impl<'de> Deserialize<'de> for TokenFrequency

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 TokenFrequency

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

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