代码之家  ›  专栏  ›  技术社区  ›  Hypothetical Ninja

Python中的Levenshtein距离循环

  •  1
  • Hypothetical Ninja  · 技术社区  · 11 年前

    我有一组参考单词(拼写正确),我需要使用用户输入的单词。使用Levenstein距离将输入单词与参考列表进行比较,我需要从成本最低的参考列表中返回单词。此外,该参考列表按频率排序,因此较高的频率出现在顶部。如果两个单词的距离相同,则返回频率较高的单词。“NWORDS”是我根据频率排序的参考列表。“候选”是用户输入的单词。

    代码:

    for word in NWORDS: #iterate over all words in ref
        i = jf.levenshtein_distance(candidate,word) #compute distance for each word with user input
    
            #dont know what to do here
        return word #function returns word from ref list with lowest dist and highest frequency of occurrence.
    
    1 回复  |  直到 11 年前
        1
  •  2
  •   jonrsharpe    11 年前

    您可以按如下方式处理:

    match = None # best match word so far
    dist = None # best match distance so far
    for word in NWORDS: #iterate over all words in ref
        i = jf.levenshtein_distance(candidate, word) #compute distance for each word with user input
        if dist is None or i < dist: # or <= if lowest freq. first in NWORDS
            match, dist = word, i
    return match #function returns word from ref list with lowest dist and highest frequency of occurrence