在 Python Chatbot 中成对调用打印函数时出现值错误
Value error when called printing function in pairs in Python Chatbot
我正在 chatbot.But 上工作,同时使用功能链接成对出现一些错误 occured.I 想要在用户可以选择 [=22= 的主题后打印 list.And 中的主题] 打印主题时,出现了一些无法解决的问题
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)
我找不到那个错误的原因 return。我检查了循环但没有任何变化。
错误发生是因为配对列表在第一个索引中有 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']]
]
比如我有问题;
众所周知,这是基本代码(当然是缩写):
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()
我正在 chatbot.But 上工作,同时使用功能链接成对出现一些错误 occured.I 想要在用户可以选择 [=22= 的主题后打印 list.And 中的主题] 打印主题时,出现了一些无法解决的问题
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)
我找不到那个错误的原因 return。我检查了循环但没有任何变化。
错误发生是因为配对列表在第一个索引中有 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']]
]
比如我有问题;
众所周知,这是基本代码(当然是缩写):
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()