【问题标题】:Find a word in a list of words that has minimum distance with a given word在单词列表中找到与给定单词距离最小的单词
【发布时间】:2021-09-19 20:14:30
【问题描述】:

假设给定一个单词列表['windows','hello','python','world','software','desk']和一个输入单词'widow',如何(快速)从单词列表中找到与输入单词'widow'具有最小编辑距离的单词(答案在这个例如'windows')?是否有可用的库/函数来实现它?谢谢!

【问题讨论】:

    标签: python nlp linguistics word-diff


    【解决方案1】:

    python-Levenshtein 库。 distance() 函数正是您要寻找的。​​p>

    关于列表,我会这样做:

    input = "widow"
    words = ['windows','hello','python','world','software','desk']
    
    distances = [distance(input, word) for word in words]
    closest = words[distances.index(min(distances)]
    

    您必须处理两个单词的输入距离相同的情况。

    【讨论】:

      【解决方案2】:

      内置difflib

      import difflib
      difflib.get_close_matches("widow", lst, n=1)
      
      #out: ['windows']
      

      【讨论】:

        猜你喜欢
        • 2011-11-21
        • 2012-10-01
        • 1970-01-01
        • 2021-12-14
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 2019-04-28
        • 1970-01-01
        相关资源
        最近更新 更多