Created
December 15, 2013 15:57
-
-
Save lhw/7974647 to your computer and use it in GitHub Desktop.
String similarity based on http://www.catalysoft.com/articles/StrikeAMatch.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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