Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created December 18, 2008 02:18
Show Gist options
  • Save danielharan/37343 to your computer and use it in GitHub Desktop.
Save danielharan/37343 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment