lib/text/white_similarity.rb in text-1.0.2 vs lib/text/white_similarity.rb in text-1.0.3

- old
+ new

@@ -32,27 +32,30 @@ def initialize @word_letter_pairs = {} end def similarity(str1, str2) - pairs1, length1 = word_letter_pairs(str1) - pairs2, length2 = word_letter_pairs(str2) + pairs1 = word_letter_pairs(str1) + pairs2 = word_letter_pairs(str2).dup - intersection = pairs1.inject(0) { |acc, pair| - pairs2.include?(pair) ? acc + 1 : acc - } - union = length1 + length2 + union = pairs1.count + pairs2.count + intersection = 0 + pairs1.each_with_index do |pair1| + if index = pairs2.index(pair1) + intersection += 1 + pairs2.delete_at(index) + end + end + (2.0 * intersection) / union end private def word_letter_pairs(str) - @word_letter_pairs[str] ||= ( - pairs = str.upcase.split(/\s+/).map{ |word| + @word_letter_pairs[str] ||= + str.upcase.split(/\s+/).map{ |word| (0 ... (word.length - 1)).map { |i| word[i, 2] } - }.flatten - [Set.new(pairs), pairs.length] - ) + }.flatten.freeze end end end