Python 3.x 在python中创建自动更正和单词建议程序
基本上,当我在“big.txt”中有一个拼写正确的单词的字典时,如果我从用户输入和字典中得到相似的结果,我会打印出一行 在我整理好后,我正在比较两个字符串 但是,我无法执行该行Python 3.x 在python中创建自动更正和单词建议程序,python-3.x,Python 3.x,基本上,当我在“big.txt”中有一个拼写正确的单词的字典时,如果我从用户输入和字典中得到相似的结果,我会打印出一行 在我整理好后,我正在比较两个字符串 但是,我无法执行该行 def autocorrect(word): Break_Word = sorted(word) Sorted_Word = ''.join(Break_Word) return Sorted_Word user_input = "" while (user_input == ""):
def autocorrect(word):
Break_Word = sorted(word)
Sorted_Word = ''.join(Break_Word)
return Sorted_Word
user_input = ""
while (user_input == ""):
user_input = input("key in word you wish to enter: ")
user_word = autocorrect(user_input).replace(' ', '')
with open('big.txt') as myFile:
for word in myFile:
NewWord = str(word.replace(' ', ''))
Break_Word2 = sorted(NewWord.lower())
Sorted_Word2 = ''.join(Break_Word2)
if (Sorted_Word2 == user_word):
print("The word",user_input,"exist in the dictionary")
当我尝试使用其他字符串进行硬编码时,例如
if (Sorted_Word2 == user_word):
print("The word",user_input,"exist in the dictionary")
成功了。我的代码怎么了?如何比较文件中的两个字符串?这是什么意思?它会引发异常吗?如果是这样的话,把它贴出来 但是,我无法执行该行
def autocorrect(word):
Break_Word = sorted(word)
Sorted_Word = ''.join(Break_Word)
return Sorted_Word
user_input = ""
while (user_input == ""):
user_input = input("key in word you wish to enter: ")
user_word = autocorrect(user_input).replace(' ', '')
with open('big.txt') as myFile:
for word in myFile:
NewWord = str(word.replace(' ', ''))
Break_Word2 = sorted(NewWord.lower())
Sorted_Word2 = ''.join(Break_Word2)
if (Sorted_Word2 == user_word):
print("The word",user_input,"exist in the dictionary")
如果(排序的单词2==用户单词):
打印(“单词”,用户输入,“存在于词典中”)
因为我可以运行你的程序的一个版本,结果和预期的一样
if ("a" == "a"):
print("The word",user_input,"exist in the dictionary")
要输入的关键字:druge
用户词“degru”
字典里不存在druge这个词
要输入的关键字:Mike
用户词“eikm”
(‘单词’、‘迈克’、‘存在于字典中’)
此外,我不知道这些“自动更正”的东西在做什么。您似乎只需要在单词列表中搜索您的搜索单词的一个实例。对搜索词中的字符进行“排序”一无所获