# File lib/text/levenshtein.rb, line 32 def distance(str1, str2) prepare = if "ruby".respond_to?(:encoding) lambda { |str| str.encode(Encoding::UTF_8).unpack("U*") } else rule = $KCODE.match(/^U/i) ? "U*" : "C*" lambda { |str| str.unpack(rule) } end s, t = [str1, str2].map(&prepare) n = s.length m = t.length return m if n.zero? return n if m.zero? d = (0..m).to_a x = nil n.times do |i| e = i + 1 m.times do |j| cost = (s[i] == t[j]) ? 0 : 1 x = [ d[j+1] + 1, # insertion e + 1, # deletion d[j] + cost # substitution ].min d[j] = e e = x end d[m] = x end return x end