2009-04-05 18:02:44 -04:00
|
|
|
module RailsGuides
|
|
|
|
module Levenshtein
|
2011-12-19 14:53:33 -05:00
|
|
|
# Based on the pseudocode in http://en.wikipedia.org/wiki/Levenshtein_distance
|
2009-04-05 18:02:44 -04:00
|
|
|
def self.distance(s1, s2)
|
|
|
|
s = s1.unpack('U*')
|
|
|
|
t = s2.unpack('U*')
|
|
|
|
m = s.length
|
|
|
|
n = t.length
|
2009-03-21 19:40:35 -04:00
|
|
|
|
2009-04-05 18:02:44 -04:00
|
|
|
# matrix initialization
|
|
|
|
d = []
|
|
|
|
0.upto(m) { |i| d << [i] }
|
|
|
|
0.upto(n) { |j| d[0][j] = j }
|
2009-03-21 19:40:35 -04:00
|
|
|
|
2009-04-05 18:02:44 -04:00
|
|
|
# distance computation
|
|
|
|
1.upto(m) do |i|
|
|
|
|
1.upto(n) do |j|
|
|
|
|
cost = s[i] == t[j] ? 0 : 1
|
|
|
|
d[i][j] = [
|
|
|
|
d[i-1][j] + 1, # deletion
|
|
|
|
d[i][j-1] + 1, # insertion
|
|
|
|
d[i-1][j-1] + cost, # substitution
|
|
|
|
].min
|
|
|
|
end
|
2009-03-21 19:40:35 -04:00
|
|
|
end
|
2009-03-15 15:03:37 -04:00
|
|
|
|
2009-04-05 18:02:44 -04:00
|
|
|
# all done
|
|
|
|
return d[m][n]
|
|
|
|
end
|
2009-03-21 19:40:35 -04:00
|
|
|
end
|
2009-03-15 15:03:37 -04:00
|
|
|
end
|