我正在尝试使用levenshtein距离通过编辑距离对列表进行排序
def suggest(dic, word, distance, maxSugestions=5):
list = []
for i in range(1, 200):
for word1 in sorted(dic):
if distance(word1, word) == i:
list.append(word1)
if len(list) == maxSugestions:
return list
这是我当前的函数,它接收一个字符串列表(这个列表大约有43000个字符串),一个我想要比较的字,一个返回两个字符串之间的编辑距离和列表应该具有的maxSugestions整数的函数
这是当前距离函数:
def levDistance(str1, str2):
matrix = [[0 for x in range(len(str2) + 1)] for x in range(len(str1) + 1)]
for i in range(len(str1) + 1):
for j in range(len(str2) + 1):
if i == 0:
matrix[i][j] = j
elif j == 0:
matrix[i][j] = i
elif str1[i-1] == str2[j-1]:
matrix[i][j] = matrix[i-1][j-1]
else:
matrix[i][j] = 1 + min(matrix[i][j-1], matrix[i-1][j], matrix[i-1][j-1])
return matrix[len(str1)][len(str2)]
当前的suggest()函数可以工作,但是我需要对它进行优化,因为它花费的时间太长了,我不知道该怎么做。任何帮助都是感激的。多谢各位
我试过这个方法,希望对你有用
您在每次迭代中计算相同的距离,这是一个很大的问题。请尝试只计算一次,然后获得maxSuggestion定义的建议数:
然后是你的实现!如果您仍然希望更快,最好使用editdistance库。(或任何其他基于C的实现,如果有必要的话)而不是基于python的实现For me it went 20x faster than python implementation.根据原始答案:
顺便说一句,我不是该软件包的作者,而是通过谷歌搜索“基于C的levenshtein距离实现”找到该软件包的
相关问题 更多 >
编程相关推荐