我有以下python函数:
import Levenshtein
SENTINEL = -1
def findClosestMatch(sampleElemList, convertElem, error):
matchIndividualElem = False
lowestLevDistance = SENTINEL
closestMatchingString = ''
for sampleElem in sampleElemList:
if sampleElem.text == convertElem.text:
matchIndividualElem = True
break
sampleDistance = Levenshtein.distance(sampleElem.text, convertElem.text)
if sampleDistance < lowestLevDistance or lowestLevDistance == SENTINEL:
lowestLevDistance = sampleDistance
closestMatchingString = sampleElem.text
if matchIndividualElem == False and convertElem.text != None:
safeErrString = convertElem.text.encode('utf-8')
error.write('The following text could not be matched: \'' + safeErrString + '\'. The closest text is: ' + closestMatchingString + \
' with a Levenshtein distance of ' + str(lowestLevDistance) + '.\n\n')
return 1
return 0;
我从命令行安装了Levenshtein python包:
sudo pip install python-Levenshtein
此功能包含在程序中,该程序之前已表明功能良好。它返回一个或零,以添加到目前为止在列表中找到的总不匹配。
程序在没有警告的情况下编译,程序的其余部分按预期执行。但是,错误文件始终显示为:'以下文本无法匹配:''与Levenshtein距离为-1。
该函数应该总是输出一些东西,只要元素存在于'sampleElemList'中,并且我知道这是真的。这是我如何调用库的问题?这是安装问题吗?或者它完全是另一回事?