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

我正在研究聊天机器人。但是在成对使用功能链接时,出现了一些错误。我想在列表中打印主题。在用户可以选择所需的主题之后。但是在打印主题时,出现了一些我可以解决的问题解决。


from nltk.chat.util import Chat, reflections

from tkinter import *

import re

import numpy as np


subjectAreaList = ["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)

我找不到为什么会返回错误。我检查我的循环但没有任何变化。


跃然一笑
浏览 198回答 2
2回答

侃侃无极

发生错误是因为配对列表在第一个索引中有 3 个项目,而[(re.compile(x, re.IGNORECASE), y) for (x, y) in pairs] 语句期望有 2 个项目。所以你可以试试&nbsp;pairs = [&nbsp; &nbsp; ['i want to see reports', [['In which subject area would you like to see the reports?'],listSubjectArea()]],&nbsp; &nbsp; ['subject1(.*)', ['blah blah blah']],&nbsp; &nbsp; ['subject2(.*)', ['blah blah blah']],&nbsp; &nbsp; ['subject3(.*)', ['blah blah blah']]]或者pairs = [&nbsp; &nbsp; ['i want to see reports', ['In which subject area would you like to see the reports?',listSubjectArea()]],&nbsp; &nbsp; ['subject1(.*)', ['blah blah blah']],&nbsp; &nbsp; ['subject2(.*)', ['blah blah blah']],&nbsp; &nbsp; ['subject3(.*)', ['blah blah blah']]]

阿晨1998

例如,我有一个问题;众所周知,这是基本代码(当然是缩写):from nltk.chat.util import Chat, reflectionspairs = (&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; r"I need (.*)",&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Why do you need %1?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Would it really help you to get %1?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Are you sure you need %1?",&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; r"Why don\'t you (.*)",&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Do you really think I don't %1?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Perhaps eventually I will %1.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Do you really want me to %1?",&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ))&nbsp; &nbsp;&nbsp;reflections = {&nbsp; &nbsp; "i am": "you are",&nbsp; &nbsp; "i was": "you were",&nbsp; &nbsp; "i": "you",&nbsp; &nbsp; "i'm": "you are"}def chatty():&nbsp; &nbsp; print("Hi, how are you?") #default message at the start&nbsp; &nbsp; chat = Chat(pairs, reflections)&nbsp; &nbsp; chat.converse()&nbsp; &nbsp;&nbsp;if __name__ == "__main__":&nbsp; &nbsp; chatty()我们想按如下方式组织代码。当提出我们确定的问题时,它会采取行动。例如,在网站或类似网站上进行搜索。from nltk.chat.util import Chat, reflectionsfrom googlesearch import searchgs=search(Chat)pairs = (&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; r"I need (.*)",&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Why do you need %1?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Would it really help you to get %1?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((gs)+"%1?"),&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ),&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; r"Why don\'t you (.*)",&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Do you really think I don't %1?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Perhaps eventually I will %1.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Do you really want me to %1?",&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ))&nbsp; &nbsp;&nbsp;reflections = {&nbsp; &nbsp; "i am": "you are",&nbsp; &nbsp; "i was": "you were",&nbsp; &nbsp; "i": "you",&nbsp; &nbsp; "i'm": "you are"}def chatty():&nbsp; &nbsp; print("Hi, how are you?")&nbsp; &nbsp; chat = Chat(pairs, reflections)&nbsp; &nbsp; chat.converse()&nbsp; &nbsp;&nbsp;if __name__ == "__main__":&nbsp; &nbsp; chatty()我们实际上想要这个。能够干扰 chat.converse()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python