简体   繁体   English

如何在python的列表中找到最相似的单词

[英]How to find the most similar word in a list in python

I have a list of words我有一个单词列表

list = ['car', 'animal', 'house', 'animation']

and I want to compare every list item with a string str1 and the output should be the most similar word.我想将每个列表项与字符串str1进行比较,输出应该是最相似的单词。 Example: If str1 would be anlmal then animal is the most similar word.示例:如果str1anlmalanimal是最相似的词。 How can I do this in python?我怎么能在python中做到这一点? Usually the words I have in my list are good distinguishable from each other.通常,我的列表中的单词可以很好地相互区分。

Use difflib :使用difflib

difflib.get_close_matches(word, ['car', 'animal', 'house', 'animation'])

As you can see from perusing the source , the "close" matches are sorted from best to worst.正如您从阅读源中看到的那样,“接近”匹配按从最好到最差的顺序排列。

>>> import difflib
>>> difflib.get_close_matches('anlmal', ['car', 'animal', 'house', 'animation'])
['animal']

I checked difflib.get_close_matches(), but it didn't work for me correctly.我检查了 difflib.get_close_matches(),但它对我不起作用。 I write here a robust solution, use as:我在这里写了一个强大的解决方案,用作:

closest_match, closest_match_idx = find_closet_match(test_str, list2check)最近匹配,最近匹配idx = find_closet_match(test_str,list2check)

def find_closet_match(test_str, list2check):
scores = {}
for ii in list2check:
    cnt = 0
    if len(test_str)<=len(ii):
        str1, str2 = test_str, ii
    else:
        str1, str2 = ii, test_str
    for jj in range(len(str1)):
        cnt += 1 if str1[jj]==str2[jj] else 0
    scores[ii] = cnt
scores_values        = numpy.array(list(scores.values()))
closest_match_idx    = numpy.argsort(scores_values, axis=0, kind='quicksort')[-1]
closest_match        = numpy.array(list(scores.keys()))[closest_match_idx]
return closest_match, closest_match_idx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM