danielharan (owner)

Revisions

gist: 37343 Download_button fork
public
Public Clone URL: git://gist.github.com/37343.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Euclidean
  class << self
    def similarity(item,other)
      new.sim(item,other)
    end
    
   #inline(:Ruby) do |builder|
   # builder.optimize :similarity
   #end
  end
  
  def sim(item,other)
    common_keys = item.keys & other.keys
    return 0 if common_keys.empty?
    
    sum_of_squares = 0.0
    common_keys.each do |key|
      sum_of_squares += (item[key] - other[key]) ** 2
    end
  
    1.0 / (1 + sum_of_squares)
  end
end