Class: TfIdf

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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TfIdf

n the n-grams of the data en.wikipedia.org/wiki/N-gram



4
5
6
# File 'lib/tf_idf.rb', line 4

def initialize(data)
  @data = data
end

Instance Method Details

#idfObject



12
13
14
# File 'lib/tf_idf.rb', line 12

def idf
  @idf ||= calculate_inverse_document_frequency
end

#tfObject



8
9
10
# File 'lib/tf_idf.rb', line 8

def tf
  @tf ||= calculate_term_frequencies
end

#tf_idfObject

This is basically calculated by multiplying tf by idf



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tf_idf.rb', line 17

def tf_idf
  tf_idf = tf.clone
  
  tf.each_with_index do |document, index|
    document.each_pair do |term, tf_score|
      tf_idf[index][term] = tf_score * idf[term]
    end
  end
  
  tf_idf
end