我今年14岁(实际上不到一个小时就满15岁了 😀 ),我在这方面遇到了很大的困难。我正在尝试创建一个非常基础的学习型AI。目前我只是在尝试搭建它的框架,即帮助它学习新词的部分,所以它现在有点混乱和粗糙。无论如何,我的想法是,当它不理解某个词时,它会询问这个词是什么,然后如果用户提供的同义词被识别,它会将这个词添加到该同义词的同义词列表中。不幸的是,我不断遇到错误和问题,现在我感到很困惑。请帮我看一下。哦,我为它的糟糕结构和几乎所有其他方面感到抱歉:
#standard dictionarydictionary = {1:"hi",2:"yes",3:"no"}wordvariations=[[],["yes,Yes"],["no,No"]]#variablesbreaker=0response = "error"success=0#importfrom random import choice#word typesquestion_words= {"who","what","where","why","when","which","how","whom","Who","What","Where","Why","When","Which","How","Whom"}#What does that mean?def what_does_that_mean(new_word): link_word = input("Sorry, I don't know what that means. Could you tell me? It means the same as the word...\n") success=0 for current_dict in wordvariations: if link_word in current_dict == TRUE: print ("Thanks") current_dict.append[new_word] success=1 break if success == 0: tryagain = input("Sorry, I don't know what that means either. Would you like to try again? (y/n) \n") if input == "y": testword= input("Give me another word \n") what_does_this_mean(testword) else: return#First startpossible_greeting =input("Hey. Nice to meet you.\n")split_greeting = possible_greeting.split() for each_word in split_greeting: if each_word in question_words: response = "Sorry, I can't answer questions yet" breaker=1 break elif (each_word == ("too")): response = "Thanks" breaker=1 break else: passif breaker ==1: passelse: yes_no = input("Is that how you usually greet people?\n") if yes_no == "yes": wordvariations[1].append(possible_greeting) elif yes_no=="no": new_greeting = input("Ok then. How do you often greet people?") wordvariations[1].append(new_greeting) else: what_does_that_mean(yes_no)#print (response)#Other stuffcurrent_greeting = choice(wordvariations[1])print (current_greeting)
如果任何缩进看起来非常不对劲,那可能是我在问题框中放错了位置。
我非常感谢任何帮助 – 我感觉自己在原地打转。目前,搜索似乎不起作用,因为它从未找到结果。最需要修复的是查找单词的功能区域,其余部分只是一个启动脚本,用来启动程序。尽量忽略那些完全无用的部分,这些部分只是为了以后使用而存在的。
提前感谢 🙂
回答:
祝贺@人名 😀
以下几点会让你的工作更容易
>>> 'How'.lower()'how'if word.lower() in word_list: # where word_list contains lower case only
所以你可以通过始终将其转换为小写来删除单词的大写/小写版本。
if link_word in current_dir == TRUE:
可以简单地写成
if link_word in current_dir:
你也可以使用布尔值(True/False)而不是整数
success = 1
可以改为 success = True
,并且有了布尔值,你可以简单地用 if success
来检查它们
(如果你愿意的话,实际上有一个 for ... else
可以不使用 success 变量就实现你想要的功能 为什么 Python 在 for 和 while 循环后使用 ‘else’?)
current_dict.append[newword]
应该改为 current_dict.append([newword])
或 current_dict.append(newword)
你还有一些调用变量 input
的错误,比如 if input=='y'
你使用的是 Python 2 还是 Python 3?在 Python 2 中,input('?')
不是你想要的(应该是 raw_input('?')
)
我认为你希望 wordvariations
在问候部分有自己的用途。比如称之为 greetings。这样你就不必每次都使用 ...[1]
你写了 what_does_this_mean
而不是 what_does_that_mean