Class: Ruigi::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruigi/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words) ⇒ Document

Returns a new instance of Document.

Raises:

  • (TypeError)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruigi/document.rb', line 9

def initialize(words)
  raise TypeError unless words.is_a?(Array)
  raise TypeError unless words.all? { |e| e.is_a?(String) }

  self.words = Hash.new
  words.group_by { |e| e }.each do |k, v|
    word = Ruigi::Word.new(k, v.length)
    word.document = self
    self.words[k] = word
  end
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/ruigi/document.rb', line 5

def model
  @model
end

#wordsObject

Returns the value of attribute words.



5
6
7
# File 'lib/ruigi/document.rb', line 5

def words
  @words
end

Instance Method Details

#feature_vectorObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruigi/document.rb', line 21

def feature_vector
  @feature_vector ||=
    begin
      vector = all_words.map { |word| words.keys.include?(word) ? words[word].tfidf : 0.0 }
      norm = Math.sqrt(vector.inject(0) { |sum, e| sum + e ** 2 })
      vector.map do |e|
        quot = e / norm
        quot.nan? ? 0.0 : quot
      end
    end
end