我正在构建一个程序,从用户那里获取输入,然后检查列表中所有字母相似的单词。
但我只设置它以查看用户输入的单词是否在列表中。
global words
words = ["Hi", "Hello", "Bye", "His", "Her"]
def get_input():
word = raw_input("See if there are any matches: ")
match(word)
def match(word_input):
word = word_input
if word in words:
print("The word '" + word + "' is in the list")
get_input()
关于如何打印与用户输入的单词相似的所有单词的任何想法?
答案 0 :(得分:2)
根据复杂程度,您可能希望尝试使用difflib.get_close_matches(请注意,您可能希望使用cutoff=
选项,以及该模块中可能有助于其他功能的其他功能)。
import difflib
words = ["Hi", "Hello", "Bye", "His", "Her"]
print(difflib.get_close_matches('Hi', words))
# ['Hi', 'His']
在一些简单的情况下,这可能会有用,但更复杂的是,你最好在拼写,同义词,缩写,拼写错误,最长的子串等等方面寻找其他地方......等等......