检查单词是否是英语Python

问题描述:

因此,我正在做一个项目,其中有一个英文单词列表,并希望它检查我写的单词是否在列表中,并告诉我它是否是英文单词,我没有知道如何做到这一点,但是这就是我应该做的,所以我要求你的帮助检查单词是否是英语Python

text = open("dict.txt","r") 
#Opens the dict.txt file I have and reads it 

word = input("Type a word to check if it's english.") 
#Type in a word to check if is english or not 

if(word in text == true): 
print(word, "Is in English.") 
elif(word in text == false): 
print(word, "Is not in English.") 
#Check if word is true or false in the dict.txt and print if it is english or not. 
+0

你需要格式化你的问题 –

+0

在python中,它是大写的“True”和“False”。 – Will

+0

对不起,我总是在尝试时遇到某种错误,所以我不知道该怎么办,谢谢帮我纠正它。 – Kobbi

在你的代码,text是一个文件对象,你首先需要从某种程度上阅读。你可以,例如,阅读到一组(因为的O(1)查找时间):

with open("dict.txt", "r") as f: 
    text = {line.strip() for line in f} # set comprehension 

word = input("Type a word to check if it's english.") 
if word in text: 
    print(word, "Is in English.") 
else: 
    print(word, "Is not in English.") 

正如有人在NLP背景:试图实际上测试一个字是否有效英语比你想象的更复杂。用足够大的字典(也包含变形表格),你应该有很高的准确性。