为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python Chatbot 中成对调用打印函数时出现值错误

在 Python Chatbot 中成对调用打印函数时出现值错误

跃然一笑 2023-02-07 11:10:45
我正在研究聊天机器人。但是在成对使用功能链接时,出现了一些错误。我想在列表中打印主题。在用户可以选择所需的主题之后。但是在打印主题时,出现了一些我可以解决的问题解决。from nltk.chat.util import Chat, reflectionsfrom tkinter import *import reimport numpy as npsubjectAreaList = ["subject1","subjec2","subject3"]    def listSubjectArea():    i = 1    for a in subjectAreaList:        print(i,". ",a)        i = i + 1                pairs = [    ['i want to see reports', ['In which subject area would you like to see the reports?'],listSubjectArea()],    ['subject1(.*)', ['blah blah blah']],    ['subject2(.*)', ['blah blah blah']],    ['subject3(.*)', ['blah blah blah']]]reflections = {    'i am' : 'you are',    'i was' : 'you were',    'i': 'you'}chat = Chat(pairs, reflections)print("Hi,What do you want to do ?")chat.converse(quit='by')但是我得到了这个错误:Traceback (most recent call last):  File "c:/Projects/demo.py", line 71, in <module>    chat = Chat(pairs, reflections)  File "C:\Python38-32\lib\site-packages\nltk\chat\util.py", line 52, in __init__    self._pairs = [(re.compile(x, re.IGNORECASE), y) for (x, y) in pairs]  File "C:\Python38-32\lib\site-packages\nltk\chat\util.py", line 52, in <listcomp>    self._pairs = [(re.compile(x, re.IGNORECASE), y) for (x, y) in pairs]ValueError: too many values to unpack (expected 2)我找不到为什么会返回错误。我检查我的循环但没有任何变化。
查看完整描述

2 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

发生错误是因为配对列表在第一个索引中有 3 个项目,而[(re.compile(x, re.IGNORECASE), y) for (x, y) in pairs] 语句期望有 2 个项目。

所以你可以试试


 pairs = [

    ['i want to see reports', [['In which subject area would you like to see the reports?'],listSubjectArea()]],

    ['subject1(.*)', ['blah blah blah']],

    ['subject2(.*)', ['blah blah blah']],

    ['subject3(.*)', ['blah blah blah']]

]

或者


pairs = [

    ['i want to see reports', ['In which subject area would you like to see the reports?',listSubjectArea()]],

    ['subject1(.*)', ['blah blah blah']],

    ['subject2(.*)', ['blah blah blah']],

    ['subject3(.*)', ['blah blah blah']]

]


查看完整回答
反对 回复 2023-02-07
?
阿晨1998

TA贡献2037条经验 获得超6个赞

例如,我有一个问题;


众所周知,这是基本代码(当然是缩写):


from nltk.chat.util import Chat, reflections



pairs = (

    (

        r"I need (.*)",

        (

            "Why do you need %1?",

            "Would it really help you to get %1?",

            "Are you sure you need %1?",

        ),

    ),

    (

        r"Why don\'t you (.*)",

        (

            "Do you really think I don't %1?",

            "Perhaps eventually I will %1.",

            "Do you really want me to %1?",

        ),

    )

)


    

reflections = {

    "i am": "you are",

    "i was": "you were",

    "i": "you",

    "i'm": "you are"

}





def chatty():

    print("Hi, how are you?") #default message at the start

    chat = Chat(pairs, reflections)

    chat.converse()

    


if __name__ == "__main__":

    chatty()

我们想按如下方式组织代码。当提出我们确定的问题时,它会采取行动。例如,在网站或类似网站上进行搜索。


from nltk.chat.util import Chat, reflections

from googlesearch import search



gs=search(Chat)



pairs = (

    (

        r"I need (.*)",

        (

            "Why do you need %1?",

            "Would it really help you to get %1?",

            ((gs)+"%1?"),

        ),

    ),

    (

        r"Why don\'t you (.*)",

        (

            "Do you really think I don't %1?",

            "Perhaps eventually I will %1.",

            "Do you really want me to %1?",

        ),

    )

)


    

reflections = {

    "i am": "you are",

    "i was": "you were",

    "i": "you",

    "i'm": "you are"

}






def chatty():

    print("Hi, how are you?")

    chat = Chat(pairs, reflections)

    chat.converse()

    


if __name__ == "__main__":

    chatty()

我们实际上想要这个。能够干扰 chat.converse()


查看完整回答
反对 回复 2023-02-07
  • 2 回答
  • 0 关注
  • 197 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号