我正在尝试创建一个程序来检查用户输入的字符串是否只包含来自文本文件的单词,该文本文件将包含英语词典中的所有单词。这将删除任何俚语。如果您有任何其他方式,请告诉我,因为我对python相对较新。 提前谢谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
首先,将所有单词读入字符串,然后将单词分成按空格分割的列表:
words = []
with open('data.txt', 'r') as myfile:
data = myfile.read().replace('\n', '')
words = data.split()
然后检查您的单词是否在列表中:
if checkWord.lower() in words:
wordCheck = True
答案 2 :(得分:0)
def slang_remover(PATH_TO_DICTIONARY_TEXT_FILE):
#Opening the Dictionary textfile which contain all dictionary words except slangs.
mydictionaryfile=open(PATH_TO_DICTIONARY_TEXT_FILE)
#Reading the whole dictionary as a text.
alltext_from_dictionary=mydictionaryfile.read()
#Getting the sentence from the User.
user_sentence=raw_input("Give me the sentence!")
#Spliting User sentence into word so that check for slang and remove it.
splited_user_sentence=user_sentence.split()
slang_removed_splited_user_sentence=[word for word in splited_user_sentence if word in alltext_from_dictionary ]
slang_removed_user_sentence=' '.join(slang_removed_splited_user_sentence)
print "Here is the slang removed sentence"
return slang_removed_user_sentence
致电,如:slang_remover("dictionary.txt")