Skip to content

Instantly share code, notes, and snippets.

@ryanong
Created June 21, 2011 19:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanong/1038659 to your computer and use it in GitHub Desktop.
Save ryanong/1038659 to your computer and use it in GitHub Desktop.
Compare Strings By Character Pairs
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