Ruby compare two strings similarity percentage

前端 未结 3 1533
醉酒成梦
醉酒成梦 2020-12-31 09:05

Id like to compare two strings in Ruby and find their similarity

I\'ve had a look at the Levenshtein gem but it seems this was last updated in 2008 and

3条回答
  •  不思量自难忘°
    2020-12-31 09:34

    I think your question could do with some clarifications, but here's something quick and dirty (calculating as percentage of the longer string as per your clarification above):

    def string_difference_percent(a, b)
      longer = [a.size, b.size].max
      same = a.each_char.zip(b.each_char).select { |a,b| a == b }.size
      (longer - same) / a.size.to_f
    end
    

    I'm still not sure how much sense this percent difference you are looking for makes, but this should get you started at least.

    It's a bit like Levensthein distance, in that it compares the strings character by character. So if two names differ only by the middle name, they'll actually be very different.

提交回复
热议问题