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']]
]

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()
添加回答
舉報