Created
June 21, 2011 19:27
-
-
Save ryanong/1038659 to your computer and use it in GitHub Desktop.
Compare Strings By Character Pairs
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
def compare_strings(str1,str2) | |
str1.downcase! | |
pairs1 = (0..str1.length-2).collect {|i| str1[i,2]}.reject { |pair| pair.include? " "} | |
str2.downcase! | |
pairs2 = (0..str2.length-2).collect {|i| str2[i,2]}.reject { |pair| pair.include? " "} | |
union = pairs1.size + pairs2.size | |
intersection = 0 | |
pairs1.each do |p1| | |
0.upto(pairs2.size-1) do |i| | |
if p1 == pairs2[i] | |
intersection += 1 | |
pairs2.slice!(i) | |
break | |
end | |
end | |
end | |
(2.0 * intersection) / union | |
end | |
# http://www.catalysoft.com/articles/StrikeAMatch.html | |
# http://stackoverflow.com/questions/653157/a-better-similarity-ranking-algorithm-for-variable-length-strings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment