【问题标题】:Extract words in a paragraph that are similar to words in list提取段落中与列表中的单词相似的单词
【发布时间】:2021-05-16 18:51:18
【问题描述】:

我有以下字符串:

"The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"

要提取的单词列表:

["town","teddy","chicken","boy went"]

注意:给定句子中的 town 和 teddy 拼写错误。

我尝试了以下方法,但我得到的其他词不属于答案:

import difflib

sent = "The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"

list1 = ["town","teddy","chicken","boy went"]

[difflib.get_close_matches(x.lower().strip(), sent.split()) for x in list1 ]

我得到以下结果:

[['twn', 'to'], ['tddy'], ['chicken.', 'picked'], ['went']]

代替:

'twn', 'tddy', 'chicken','boy went'

【问题讨论】:

    标签: python python-3.x difflib


    【解决方案1】:

    difflib.get_closest_matches() 文档中的注意事项:

    difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6)

    返回最佳“足够好”匹配的列表。 word 是需要紧密匹配的序列(通常是字符串),并且 possibilities 是匹配 word 的序列列表 (通常是字符串列表)。

    可选参数n(默认3)是要返回的最大匹配数; n 必须大于 0

    可选参数cutoff(默认0.6)是[0, 1]范围内的浮点数。得分至少与单词相似的可能性是 忽略。


    目前,您正在使用默认的 ncutoff 参数。

    您可以指定其中一个(或两个)来缩小返回的匹配范围。

    例如,您可以使用 0.75 的 cutoff 分数:

    result = [difflib.get_close_matches(x.lower().strip(), sent.split(), cutoff=0.75) for x in list1]
    

    或者,您可以指定最多只返回 1 个匹配项:

    result = [difflib.get_close_matches(x.lower().strip(), sent.split(), n=1) for x in list1]
    

    在任何一种情况下,您都可以使用列表推导来展平列表列表(因为difflib.get_close_matches() 总是返回一个列表):

    matches = [r[0] for r in result]
    

    由于您还想检查二元组的紧密匹配,您可以通过提取相邻“单词”的配对并将它们作为possibilities 参数的一部分传递给difflib.get_close_matches()

    这是一个完整的实际工作示例:

    import difflib
    import re
    
    sent = "The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"
    
    list1 = ["town", "teddy", "chicken", "boy went"]
    
    # this extracts overlapping pairings of "words"
    # i.e. ['The boy', 'boy went', 'went to', 'to twn', ...
    pairs = re.findall(r'(?=(\b[^ ]+ [^ ]+\b))', sent)
    
    # we pass the sent.split() list as before
    # and concatenate the new pairs list to the end of it also
    result = [difflib.get_close_matches(x.lower().strip(), sent.split() + pairs, n=1) for x in list1]
    
    matches = [r[0] for r in result]
    
    print(matches)
    # ['twn', 'tddy', 'chicken.', 'boy went']
    

    【讨论】:

      【解决方案2】:

      如果您阅读 Python 文档 fordifflib.get_close_matches() https://docs.python.org/3/library/difflib.html 它返回所有可能的最佳匹配。 方法签名: difflib.get_close_matches(单词,可能性,n=3,cutoff=0.6)

      这里 n 是要返回的最大匹配数。所以我认为你可以将其作为 1 传递。

      >>> [difflib.get_close_matches(x.lower().strip(), sent.split(),1)[0] for x in list1]
      ['twn', 'tddy', 'chicken.', 'went']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-29
        • 2014-07-06
        • 2020-01-28
        • 1970-01-01
        • 2018-10-24
        相关资源
        最近更新 更多