class Hash

Public Instance Methods

similarity_to(other) click to toggle source

Returns a rating f between 0 and 1 that indicates the similarity rating of self to other

Calculates the number of keys with the same values then divide the result by the number of keys

# File lib/observance.rb, line 49
def similarity_to(other)
  smaller_one, bigger_one = [self, other].sort_by(&:size)
  diff_size = bigger_one.size - smaller_one.size

  ratio = bigger_one.size + diff_size

  sum = 0
  bigger_one.each_pair do |k, v|
    (sum = sum + 1) if v == smaller_one[k]
  end
  r1 = sum.fdiv(bigger_one.size)

  sum2 = 0
  smaller_one.each_pair do |k, v|
    (sum2= sum2 + 1) if v == bigger_one[k]
  end
  r2 = (sum2).fdiv(ratio)
  (r1 + r2).fdiv(2)
end