2016-11-25 6 views
-1

私のプログラムが入力文(例えば「こんにちは!」) を調べ、入力中の単語がリストに含まれているかどうかを確認しようとしています。ここ は、これまでのコードです:Pythonは単語がリストにあり、入力されているかどうかをチェックする方法を教えてください。

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
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 
関連する問題