Class: Ruigi::Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(corpus) ⇒ Model

Returns a new instance of Model.

Raises:

  • (TypeError)


5
6
7
8
9
10
11
12
13
14
# File 'lib/ruigi/model.rb', line 5

def initialize(corpus)
  raise TypeError unless corpus.is_a?(Array)
  raise TypeError unless corpus.all? { |e| e.is_a?(Ruigi::Document) }

  self.corpus = corpus

  corpus.each do |document|
    document.model = self
  end
end

Instance Attribute Details

#corpusObject

Returns the value of attribute corpus.



3
4
5
# File 'lib/ruigi/model.rb', line 3

def corpus
  @corpus
end

Instance Method Details

#feature_vector_of(index) ⇒ Object



20
21
22
# File 'lib/ruigi/model.rb', line 20

def feature_vector_of(index)
  corpus[index].feature_vector.to_nm
end

#inner_product_matrixObject



28
29
30
# File 'lib/ruigi/model.rb', line 28

def inner_product_matrix
  matrix.dot(matrix.transpose)
end

#matrixObject



16
17
18
# File 'lib/ruigi/model.rb', line 16

def matrix
  @matrix ||= corpus.map(&:feature_vector).to_nm
end

#wordlistObject



24
25
26
# File 'lib/ruigi/model.rb', line 24

def wordlist
  @wordlist ||= corpus.map { |document| document.words.keys }.flatten.uniq.sort
end