亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號