【问题标题】:I need some help finding things in lists in lists (among other things)我需要一些帮助来查找列表中的内容(除其他外)
【发布时间】:2014-02-12 23:34:32
【问题描述】:

我今年 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)

如果任何缩进看起来真的不对,我可能把它放在问题框中是错误的。

我非常感谢这里的任何帮助 - 我觉得我在兜圈子。目前,搜索似乎无法正常工作,因为它从未找到结果。最需要修复的是找词功能区,剩下的只是一个启动脚本,让事情顺利进行。试着忽略那些完全无用的部分,这些部分只是为了以后。

提前致谢:)

【问题讨论】:

  • 您能否举例说明您将与此进行的交互以及您希望它做什么?
  • “是,是”的预期用途是什么?
  • 结构看起来可以正常工作(但可能是我累了)

标签: python list machine-learning artificial-intelligence


【解决方案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

【讨论】:

  • 谢谢。我进行了您建议的更改,并且添加了一些缺少的语音标记-完全修复了它。非常感谢 - 我现在对此非常满意。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2016-07-17
  • 2018-12-18
相关资源
最近更新 更多