获取python fuzzywuzzy match

时间:2015-12-02 17:32:28

标签: python fuzzy-search fuzzywuzzy

我使用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 :(你'因此,除了上面提到的双重目的之外,本书至少是为两组写的:1。',59)不在列表中

有没有更好的方法在原始列表中找到匹配的索引? fuzzywuzzy可以给我一些怎样的礼物? <{3}}中似乎没有关于它的任何内容。

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

1 个答案:

答案 0 :(得分:1)

我觉得有点傻。 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'