我正在使用 Python fuzzywuzzy 在句子列表中查找匹配项:
def getMatches(needle):
return process.extract(needle, bookSentences, scorer=fuzz.token_sort_ratio, limit=3)
我正在尝试打印匹配项及其周围的句子:
for match in matches:
matchIndex = bookSentences.index(match)
sentenceIndices = range(matchIndex-2,matchIndex+2)
for index in sentenceIndices:
print bookSentences[index],
print '\n\n'
不幸的是,脚本无法在原始列表中找到匹配项:
ValueError: (u'Thus, in addition to the twin purposes mentioned above, this book is written for at least two groups: 1.', 59) is not in list
有没有更好的方法在原始列表中找到匹配项的索引? fuzzywuzzy 可以给我一些吗? readme里面好像什么都没有关于它。
如何获取 fuzzywuzzy 返回的匹配项的原始列表中的索引?
请您参考如下方法:
我觉得有点傻。 fuzzywuzzy 返回一个包含分数的元组,而不仅仅是匹配项。解决方案:
for match in matches:
matchIndex = bookSentences.index(match[0])
sentenceIndices = range(matchIndex-2,matchIndex+2)
for index in sentenceIndices:
print bookSentences[index],
print '\n\n'
