【发布时间】:2012-04-12 10:56:04
【问题描述】:
这是算法(在 ruby 中)
#http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
def self.dameraulevenshtein(seq1, seq2)
oneago = nil
thisrow = (1..seq2.size).to_a + [0]
seq1.size.times do |x|
twoago, oneago, thisrow = oneago, thisrow, [0] * seq2.size + [x + 1]
seq2.size.times do |y|
delcost = oneago[y] + 1
addcost = thisrow[y - 1] + 1
subcost = oneago[y - 1] + ((seq1[x] != seq2[y]) ? 1 : 0)
thisrow[y] = [delcost, addcost, subcost].min
if (x > 0 and y > 0 and seq1[x] == seq2[y-1] and seq1[x-1] == seq2[y] and seq1[x] != seq2[y])
thisrow[y] = [thisrow[y], twoago[y-2] + 1].min
end
end
end
return thisrow[seq2.size - 1]
end
我的问题是,长度为 780 的 seq1 和长度为 7238 的 seq2,在 i7 笔记本电脑上运行大约需要 25 秒。理想情况下,我希望将其缩短到大约一秒钟,因为它是作为 web 应用程序的一部分运行的。
我发现there is a way to optimize the vanilla levenshtein distance 使得运行时间从 O(n*m) 下降到 O(n + d^2),其中 n 是较长字符串的长度,d 是编辑距离。那么,我的问题就变成了,可以将相同的优化应用到我拥有的 damerau 版本(上面)吗?
【问题讨论】:
-
你看过Levenshtein Automata吗?
-
您是否需要知道确切的距离,或者距离是否低于某个阈值?前者比后者难。
标签: ruby string algorithm optimization