Class: Clusterer::DocumentsCentroidVector

Inherits:
Object
  • Object
show all
Defined in:
lib/clusterer/lsi/documents_centroid_vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(docs = []) ⇒ DocumentsCentroidVector

Returns a new instance of DocumentsCentroidVector.



28
29
30
31
32
33
34
# File 'lib/clusterer/lsi/documents_centroid_vector.rb', line 28

def initialize(docs = [])
  @no_of_documents = docs.size
  return if @no_of_documents == 0
  @centroid = docs[0]
  docs.slice(1..docs.length).each {|d| @centroid = @centroid + d}
  @centroid = @centroid / @no_of_documents
end

Instance Attribute Details

#centroidObject (readonly)

Returns the value of attribute centroid.



26
27
28
# File 'lib/clusterer/lsi/documents_centroid_vector.rb', line 26

def centroid
  @centroid
end

#no_of_documentsObject (readonly)

Returns the value of attribute no_of_documents.



25
26
27
# File 'lib/clusterer/lsi/documents_centroid_vector.rb', line 25

def no_of_documents
  @no_of_documents
end

Instance Method Details

#merge!(centroid) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/clusterer/lsi/documents_centroid_vector.rb', line 40

def merge!(centroid)
  return unless centroid.centroid
  unless @centroid
    @centroid = centroid.centroid
    @no_of_documents = centroid.no_of_documents
  else
    @centroid = (@centroid * @no_of_documents) + (centroid.centroid * centroid.no_of_documents)
    @centroid = @centroid / (@no_of_documents += centroid.no_of_documents)
  end
end

#to_dmatrixObject



36
37
38
# File 'lib/clusterer/lsi/documents_centroid_vector.rb', line 36

def to_dmatrix
  @centroid
end