我需要帮助查找列表中的列表(以及其他内容)

时间:2014-02-12 23:34:32

标签: python list machine-learning artificial-intelligence

我14岁(实际上不到一个小时15次:D)我真的很挣扎。 我正在尝试创建一个非常基础的学习AI。目前我只是想设置它的框架,这个部分可以帮助它学习新单词,所以它有点混乱和粗糙。 无论如何,这个想法是,当它不理解一个单词时,它询问它是什么,然后如果识别出用户提供的同义词,它将把它添加到该同义词的同义词列表中。 不幸的是,我一直在遇到错误和问题,现在我只是感到困惑。 请帮我看一下。哦,我为它糟糕的结构而道歉,而对其他一切都很差:

#standard dictionary

dictionary = {1:"hi",2:"yes",3:"no"}
wordvariations=[[],["yes,Yes"],["no,No"]]


#variables

breaker=0
response = "error"
success=0

#import

from random import choice

#word types

question_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 start

possible_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: pass

if breaker ==1:
    pass
else:
    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 stuff

current_greeting = choice(wordvariations[1])
print (current_greeting)

如果任何缩进看起来真的没关系,我可能会把它放在问题框中错误。

我真的很感激这里的任何帮助 - 我觉得我要围成一圈。目前,搜索似乎没有起作用,因为它从未找到结果。它是需要修复最多的单词查找功能区域,其余的只是一种启动脚本来实现目标。尝试并忽略那些以后完全无用的位。

提前致谢:)

1 个答案:

答案 0 :(得分:1)

祝贺杰克:D

一些可以让您更轻松的事情

>>> '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在没有成功变量Why does python use 'else' after for and while loops?的情况下完全按照你要做的那样做事。

current_dict.append[newword]应为current_dict.append([newword])current_dict.append(newword)

您还调用了变量input,例如if input=='y'

我也在运行python 2或3?在python 2中input('?')不是你想要的(raw_input('?')

我认为你希望wordvariations成为问候部分的自己。喜欢称它为问候。 (这样你就不必一直...[1]

你写了what_does_this_mean而不是what_does_that_mean