Class: SameSame::RockClusters

Inherits:
Object
  • Object
show all
Defined in:
lib/same_same/rock_clusters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ RockClusters

Returns a new instance of RockClusters.



7
8
9
10
11
12
13
14
15
# File 'lib/same_same/rock_clusters.rb', line 7

def initialize( attrs = {} )
  self.link_matrix = attrs.fetch(:link_matrix)
  self.goodness_measure = attrs.fetch(:goodness_measure)
  self.cluster_map = {}
  @last_key = -1

  attrs[:clusters].each {|c| add_cluster(c) }
  calculate_closest_clusters
end

Instance Attribute Details

#closest_clustersObject

Returns the value of attribute closest_clusters.



5
6
7
# File 'lib/same_same/rock_clusters.rb', line 5

def closest_clusters
  @closest_clusters
end

#cluster_mapObject

Returns the value of attribute cluster_map.



5
6
7
# File 'lib/same_same/rock_clusters.rb', line 5

def cluster_map
  @cluster_map
end

#clustersObject

Returns the value of attribute clusters.



5
6
7
# File 'lib/same_same/rock_clusters.rb', line 5

def clusters
  @clusters
end

#goodness_measureObject

Returns the value of attribute goodness_measure.



5
6
7
# File 'lib/same_same/rock_clusters.rb', line 5

def goodness_measure
  @goodness_measure
end

Returns the value of attribute link_matrix.



5
6
7
# File 'lib/same_same/rock_clusters.rb', line 5

def link_matrix
  @link_matrix
end

Instance Method Details

#add_cluster(c) ⇒ Object



37
38
39
# File 'lib/same_same/rock_clusters.rb', line 37

def add_cluster( c )
  cluster_map[next_key] = c
end

#calculate_closest_clustersObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/same_same/rock_clusters.rb', line 45

def calculate_closest_clusters
  self.closest_clusters = {}
  cluster_map.each do |cluster_key, cluster|
    similarity = cluster_map.map do |other_key, other_cluster|
      if cluster_key != other_key
        number_of_links = link_matrix.count_links_between_clusters( cluster, other_cluster )
        if number_of_links > 0
          goodness = goodness_measure.g( number_of_links, cluster.size, other_cluster.size)
          ClusterSimilarity.new( other_key, goodness )
        end
      end
    end.compact.sort.first
    closest_clusters[cluster_key] = similarity if similarity
  end
end

#find_most_similar_pairObject



25
26
27
# File 'lib/same_same/rock_clusters.rb', line 25

def find_most_similar_pair
  closest_clusters.sort_by {|_, similarity| similarity.goodness}.first || []
end

#merge_best_candidatesObject



17
18
19
20
21
22
23
# File 'lib/same_same/rock_clusters.rb', line 17

def merge_best_candidates
  key1, similarity = find_most_similar_pair
  if key1
    merge_clusters key1, similarity.cluster_key
    similarity.goodness
  end
end

#merge_clusters(key1, key2) ⇒ Object



61
62
63
64
65
# File 'lib/same_same/rock_clusters.rb', line 61

def merge_clusters(key1, key2)
  merged_key = add_cluster( cluster_map.delete(key1) + cluster_map.delete(key2) )
  calculate_closest_clusters
  merged_key
end

#next_keyObject



41
42
43
# File 'lib/same_same/rock_clusters.rb', line 41

def next_key
  @last_key = @last_key + 1
end

#sizeObject



29
30
31
# File 'lib/same_same/rock_clusters.rb', line 29

def size
  cluster_map.size
end