Skip to content

Instantly share code, notes, and snippets.

@lhw
Created December 15, 2013 15:57
Show Gist options
  • Save lhw/7974647 to your computer and use it in GitHub Desktop.
Save lhw/7974647 to your computer and use it in GitHub Desktop.
class String
def similarity(other)
b = [self, other].map{|s| 0.upto(s.length-2).map{|i| s[i,2].downcase unless s[i,2].include? " "}.compact}
(2.0 * ((1.0/(b.first.size + b.last.size)) * b.first.inject(0){|r,p1|
b.last.each{|p2| b.last.delete(p2) and r = r+1 and break if p1 == p2}
r
})).round(2)
end
end
["Sealed", "Healthy", "Heard", "Herded", "Help", "Sold"].each{|s| p "Healed".similarity(s)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment