4

我正在使用 Pythonfuzzywuzzy在句子列表中查找匹配项:

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'因此,除了上面提到的双重目的,这本书至少是为两个群体写的:1.', 59) 不在列表中

有没有更好的方法在原始列表中找到匹配的索引?有的可以fuzzywuzzy给我吗?自述文件中似乎没有任何关于它的内容。

如何获取由返回的匹配项的原始列表中的索引fuzzywuzzy

4

1 回答 1

3

我觉得有点傻。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'
于 2015-12-02T17:40:19.717 回答