Class: Clusterer::DocumentsCentroid
- Inherits:
-
DocumentBase
- Object
- Hash
- DocumentBase
- Clusterer::DocumentsCentroid
- Defined in:
- lib/clusterer/documents_centroid.rb
Instance Attribute Summary collapse
-
#no_of_documents ⇒ Object
readonly
Returns the value of attribute no_of_documents.
Instance Method Summary collapse
-
#initialize(docs = []) ⇒ DocumentsCentroid
constructor
A new instance of DocumentsCentroid.
- #merge!(centroid) ⇒ Object
Methods inherited from DocumentBase
Methods included from DocumentSimilarity
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_documents ⇒ Object (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 |