我正在尝试让我的程序通过一个输入句子(例如“你好!”) 并查看输入中的任何单词是否在列表中。 这是目前为止的代码:
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
i.upper() #i is the inputted variable as a string
WordsDocument = open('WordsDocument.txt').readlines()
for words in WordsDocument:
WordsList.append(words)
for word in i:
if findWholeWord(word) in WordsList:
print("Word Match")
有人可以帮助我开发更好的解决方案/解决这个问题吗?
答案 0 :(得分:0)
import re
def findWholeWord(w): # input string w
match_list = [] # list containing matched words
input_list = w.split(" ")
file = open('WordsDocument.txt', 'r')
text = file.read().lower()
file.close()
text = re.sub('[^a-z\ \']+', " ", text)
words_list = list(text.split())
for word in input_list:
if word in words_list:
print("Word Found: " + str(word))
match_list.append(word)
return match_list