首页 > 解决方案 > 在单词列表中找到与给定单词距离最小的单词

问题描述

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

标签: pythonnlplinguisticsword-diff

解决方案


python-Levenshtein库。该distance()功能是您正在寻找的。

关于清单,我会这样做:

input = "widow"
words = ['windows','hello','python','world','software','desk']

distances = [distance(input, word) for word in words]
closest = words[distances.index(min(distances)]

您将不得不处理两个单词的输入距离相同的情况。


推荐阅读