Ruby compare two strings similarity percentage

前端 未结 3 1520
醉酒成梦
醉酒成梦 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.

    0 讨论(0)
  • 2020-12-31 09:40

    There is now a ruby gem for similar_text. https://rubygems.org/gems/similar_text It provides a similar method that compares two strings and returns a number representing the percent similarity between the two strings.

    0 讨论(0)
  • 2020-12-31 09:54

    I can recommend the fuzzy-string-match gem.

    You can use it like this (taken from the docs):

    require "fuzzystringmatch"
    jarow = FuzzyStringMatch::JaroWinkler.create(:native)
    p jarow.getDistance("jones", "johnson")
    

    It will return a score ~0.832 which tells how good those strings match.

    0 讨论(0)
提交回复
热议问题