Module: Canivete::Similarity
- Defined in:
- lib/canivete/similarity.rb
Instance Method Summary collapse
-
#similarity_rate(target_string, options = {:ignore_case => true}) ⇒ Object
return the similarity rate between two strings, ranging from 0.0 to 1.0.
Instance Method Details
#similarity_rate(target_string, options = {:ignore_case => true}) ⇒ Object
return the similarity rate between two strings, ranging from 0.0 to 1.0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/canivete/similarity.rb', line 5 def similarity_rate(target_string, = {:ignore_case => true}) src = self.dup tgt = target_string.dup if [:ignore_case] src.downcase! tgt.downcase! end return 0 if (src.length == 0 && tgt.length != 0) return 1 if (src.length == 0 && tgt.length == 0) distance = Text::Levenshtein.distance(src, tgt) rate = 1 - (distance/src.length.to_f) unless src.length == 0 rate < 0.0 ? 0.0 : rate end |