Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#similarity_to(other) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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