【问题标题】:Get index of python fuzzywuzzy match获取 python 模糊匹配的索引
【发布时间】:2015-12-02 17:32:27
【问题描述】:

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

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

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

【问题讨论】:

    标签: python fuzzy-search fuzzywuzzy


    【解决方案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'
    

    【讨论】:

    • 这仅适用于process.extract 方法,并且仅因为返回的匹配项保证在列表中。我正在使用fuzzywuzzy 搜索长文本块中的子字符串,使用仅返回分数的fuzz.partial_ratio。我想我将不得不出于我的目的检查 SequenceMatcher。
    • this post 似乎是个不错的选择。
    • 这似乎增加了一个额外的不必要的步骤,我希望fuzzywuzzy过程以(索引,文本,分数)的形式返回元组
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2011-08-17
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多