Class: Clusterer::Cluster

Inherits:
Object
  • Object
show all
Includes:
ClusterSimilarity
Defined in:
lib/clusterer/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClusterSimilarity

#centroid_similarity, #intra_cluster_similarity, #upgma

Constructor Details

#initialize(docs = []) ⇒ Cluster

Returns a new instance of Cluster.



28
29
30
# File 'lib/clusterer/cluster.rb', line 28

def initialize(docs = [])
  @documents = docs
end

Instance Attribute Details

#centroidObject (readonly)

Returns the value of attribute centroid.



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

def centroid
  @centroid
end

#documentsObject (readonly)

Returns the value of attribute documents.



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

def documents
  @documents
end

Instance Method Details

#+(cluster) ⇒ Object



42
43
44
45
46
# File 'lib/clusterer/cluster.rb', line 42

def + (cluster)
  c = Cluster.new(self.documents.clone)
  c.merge!(cluster)
  return c
end

#==(cluster) ⇒ Object



48
49
50
# File 'lib/clusterer/cluster.rb', line 48

def ==(cluster)
  cluster && self.documents == cluster.documents
end

#intra_cluster_cosine_similarityObject



52
53
54
# File 'lib/clusterer/cluster.rb', line 52

def intra_cluster_cosine_similarity
  @intra_cluster_similarity ||= documents.inject(0) {|n,d| n + d.cosine_similarity(centroid) }
end

#merge!(cluster) ⇒ Object



36
37
38
39
40
# File 'lib/clusterer/cluster.rb', line 36

def merge!(cluster)
  documents.concat(cluster.documents)
  @centroid ? centroid.merge!(cluster.centroid) : @centroid = cluster.centroid
  @intra_cluster_similarity = nil
end