我有以下cython实现计算基于this Wikipedia article的2个字符串的Damerau-Levenshtein距离,但目前它对我的需求来说太慢了。我有一个大约600000个字符串的列表,我必须在该列表中找到拼写错误。
如果有人能提出任何算法改进或某些可能减少脚本运行时的python / cython魔法,我会很高兴。我真的不在乎它只使用了多少空间来计算。
根据使用大约2000个字符串对脚本进行分析,它在damerauLevenshteinDistance
函数中花费了完整运行时的80%(30秒中的24个),并且我完全没有想法如何使其更快。< / p>
def damerauLevenshteinDistance(a, b, h):
"""
a = source sequence
b = comparing sequence
h = matrix to store the metrics (currently nested list)
"""
cdef int inf,lena,lenb,i,j,x,i1,j1,d,db
alphabet = getAlphabet((a,b))
lena = len(a)
lenb = len(b)
inf = lena + lenb + 1
da = [0 for x in xrange(0, len(alphabet))]
for i in xrange(1, lena+1):
db = 0
for j in xrange(1, lenb+1):
i1 = da[alphabet[b[j-1]]]
j1 = db
d = 1
if (a[i-1] == b[j-1]):
d = 0
db = j
h[i+1][j+1] = min(
h[i][j]+d,
h[i+1][j]+1,
h[i][j+1]+1,
h[i1][j1]+(i-i1-1)+1+(j-j1-1)
)
da[alphabet[a[i-1]]] = i
return h[lena+1][lenb+1]
cdef getAlphabet(words):
"""
construct an alphabet out of the lists found in the tuple words with a
sequential identifier for each word
"""
cdef int i
alphabet = {}
i = 0
for wordList in words:
for letter in wordList:
if letter not in alphabet:
alphabet[letter] = i
i += 1
return alphabet
答案 0 :(得分:1)
如果您的搜索中出现了多个单词(如果您需要为输入字符串的相同值多次计算Damerau Levenshtein距离),则可以考虑使用Dictionary(或hashmap)来缓存结果。这是C#中的一个实现:
private static Dictionary<int, Dictionary<int, int>> DamerauLevenshteinDictionary = new Dictionary<int, Dictionary<int, int>>();
public static int DamerauLevenshteinDistanceWithDictionaryCaching(string word1, string word2)
{
Dictionary<int, int> word1Dictionary;
if (DamerauLevenshteinDictionary.TryGetValue(word1.GetHashCode(), out word1Dictionary))
{
int distance;
if (word1Dictionary.TryGetValue(word2.GetHashCode(), out distance))
{
// The distance is already in the dictionary
return distance;
}
else
{
// The word1 has been found in the dictionary, but the matching with word2 hasn't been found.
distance = DamerauLevenshteinDistance(word1, word2);
DamerauLevenshteinDictionary[word1.GetHashCode()].Add(word2.GetHashCode(), distance);
return distance;
}
}
else
{
// The word1 hasn't been found in the dictionary, we must add an entry to the dictionary with that match.
int distance = DamerauLevenshteinDistance(word1, word2);
Dictionary<int, int> dictionaryToAdd = new Dictionary<int,int>();
dictionaryToAdd.Add(word2.GetHashCode(), distance);
DamerauLevenshteinDictionary.Add(word1.GetHashCode(), dictionaryToAdd);
return distance;
}
}
答案 1 :(得分:0)
至少对于较长的字符串,您应该通过使用不必计算lena⋅lenb矩阵中的所有值的不同算法来获得更好的性能。例如,可能通常没有必要计算矩阵[lena][0]
角的确切成本,这表示通过删除a
中的所有字符开始的成本。
更好的算法可能是始终查看到目前为止计算出的最低权重的点,然后从那里向所有方向前进一步。这样,您可以在不检查矩阵中的所有位置的情况下到达目标位置:
此算法的实现可以使用优先级队列,如下所示:
from heapq import heappop, heappush
def distance(a, b):
pq = [(0,0,0)]
lena = len(a)
lenb = len(b)
while True:
(wgh, i, j) = heappop(pq)
if i == lena and j == lenb:
return wgh
if i < lena:
# deleted
heappush(pq, (wgh+1, i+1, j))
if j < lenb:
# inserted
heappush(pq, (wgh+1, i, j+1))
if i < lena and j < lenb:
if a[i] == b[i]:
# unchanged
heappush(pq, (wgh, i+1, j+1))
else:
# changed
heappush(pq, (wgh+1, i+1, j+1))
# ... more possibilities for changes, like your "+(i-i1-1)+1+(j-j1-1)"
这只是一个粗略的实现,可以大大改进:
heapq
模块答案 2 :(得分:0)
您似乎可以静态输入比当前更多的代码,这样可以提高速度。
您也可以在Cython中查看Levenshtein距离的实现作为示例: http://hackmap.blogspot.com/2008/04/levenshtein-in-cython.html
答案 3 :(得分:0)
我的猜测是,当前代码的最大改进将来自使用C数组而不是h
矩阵的列表列表。
答案 4 :(得分:0)
通过“cython -a”运行它,这将为您提供带有黄色注释行的HTML注释源版本。颜色越深,该行中发生的Python操作就越多。这通常有助于找到耗时的对象转换等。
但是,我很确定最终的问题是您的数据结构。考虑使用NumPy数组而不是嵌套列表,或者只使用动态分配的C内存块。
答案 5 :(得分:0)
我最近刚开源了Damerau-Levenshtein算法的Cython实现。我包括pyx和C源。