Class: Clusterer::DocumentsCentroid

Inherits:
DocumentBase show all
Defined in:
lib/clusterer/documents_centroid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DocumentBase

#vector_length

Methods included from DocumentSimilarity

#cosine_similarity

Constructor Details

#initialize(docs = []) ⇒ DocumentsCentroid

Returns a new instance of DocumentsCentroid.



27
28
29
30
31
32
# File 'lib/clusterer/documents_centroid.rb', line 27

def initialize(docs = [])
  @no_of_documents = docs.size
  docs.each do |d|
    d.each {|w,f| self[w] = (self[w] || 0.0) + f/@no_of_documents}
  end
end

Instance Attribute Details

#no_of_documentsObject (readonly)

Returns the value of attribute no_of_documents.



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

def no_of_documents
  @no_of_documents
end

Instance Method Details

#merge!(centroid) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/clusterer/documents_centroid.rb', line 34

def merge!(centroid)
  @vector_length = nil
  temp = @no_of_documents/(@no_of_documents + centroid.no_of_documents)
  self.each {|w,v| self[w] = v*temp}
  @no_of_documents += centroid.no_of_documents

  temp = centroid.no_of_documents/@no_of_documents
  centroid.each {|w,v| self[w] = (self[w] || 0) + v*temp }
end