def computeLevenshteinAmbiguous(pattern, edit, ambiguous)
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 = [self, pattern].map(&prepare)
n = s.length
m = t.length
return m if (0 == n)
return n if (0 == m)
d = (0..m).to_a
x = nil
(0...n).each do |i|
e = i+1
(0...m).each do |j|
cost = (isAmbiguousEqual(s[i],t[j],ambiguous)) ? 0 : 1
x = [
d[j+1] + 1,
e + 1,
d[j] + cost
].min
d[j] = e
e = x
end
d[m] = x
end
if(x>edit)
return -1
end
return x
end