Class: TfIdf::Ja

Inherits:
Object
  • Object
show all
Defined in:
lib/tfidf_ja.rb

Overview

日本語辞書を使用してTFIDFを算出するクラス

Instance Method Summary collapse

Constructor Details

#initializeJa

コンストラクタ



17
18
19
20
# File 'lib/tfidf_ja.rb', line 17

def initialize
  @idfs = load_dic
  reset
end

Instance Method Details

#idf(word) ⇒ Object

IDFを取得する

word

形態素

return

IDF



42
43
44
45
46
47
48
# File 'lib/tfidf_ja.rb', line 42

def idf(word)
  idf = @idfs.get(word)
  if(idf.nil?)
    idf = @idfs.average
  end
  return idf
end

#resetObject

インスタンスのリセット



23
24
25
# File 'lib/tfidf_ja.rb', line 23

def reset
  @tfs = {}
end

#tfidf(words) ⇒ Object

TF-IDFを算出する

words

形態素配列

return

key = 形態素、value = TF-IDF値のハッシュテーブル



30
31
32
33
34
35
36
37
# File 'lib/tfidf_ja.rb', line 30

def tfidf(words)
  tfidfs = {}
  set_tf_map(words)
  @tfs.each_pair { |word, tf|
    tfidfs[word] = tf * idf(word)
  }
  return tfidfs
end