'Chatbot' 未在 Chatterbot 中定义

'Chatbot' is not defined in Chatterbot

我正在通过 Chatterbot 制作聊天机器人。我面临如下问题:

  1. 当我 运行 代码时,它显示错误,但是 ChatBot 是从 chatterbot 开始导入的?

File ".../SquirralBot.py", line 5, in class SquirralBot: File "...SquirralBot.py", line 6, in SquirralBot bot = Chatbot("SquirralBot", NameError: name 'Chatbot' is not defined

  1. 我想让聊天机器人识别特定文本然后触发特定语料库,我该怎么做? "chatterbot.conversation.Response(text, **kwargs)" class 是为了这个目的吗?例如当用户输入"I am leaving"时,就会触发调用训练集"chatterbot.corpus.chinese.squirral_bye_conversation"?

  2. 是否可以将回复专门存储到数据库中,例如MongoDB 针对不同的用户?例如当用户A回复"I am sick. I got fever and running nose",则系统将"sick"存入"status",将"fever"和"running nose"存入用户A的数据"symptoms"中,这样在数据库内部,它就像 JSON:

    { "user A", "gender": "male", "record": [ { "date": "25-12-2018", "status": "fine", "symptoms": "", }, { "date": "26-12-2018", "status": "sick", "symptoms": "fever", "running nose" } }

  3. 是否可以让聊天机器人在特定时间范围内向用户发送短信?

上面提到的代码如下。我是编程新手,所以代码可能有点乱。请随时指正。非常感谢。

import sys 
from chatterbot import ChatBot 
from chatterbot.trainers import ChatterBotCorpusTrainer

class SquirralBot:
    chatbot = Chatbot("SquirralBot",
    logic_adapters=[
        {
            "import_path": "chatterbot.logic.BestMatch",
            "statement_comparison_function": "chatterbot.comparisons.levenshtein_distance",
            "response_selection_method": "chatterbot.response_selection.get_first_response"
        }
    ],storage_adapter = "chatterbot.storage.JsonFileStorageAdapter",database = "./SquirralBot_DB.json")

    def __init__(self):
        self.chatbot.set_trainer(ChatterBotCorpusTrainer)
        self.chatbot.train("chatterbot.corpus.chinese.squirral_greeting", "chatterbot.corpus.chinese.squirral_bye_conversation", "chatterbot.corpus.chinese.squirral_normal_conversation", "chatterbot.corpus.chinese.squirral_rabbit_bye_conversation", "chatterbot.corpus.chinese.squirral_rabbit_conversation")

    def getResponse(self, message=""):
        return self.chatbot.get_response(message)

if __name__ == "__main__":
    bot = SquirralBot()
    print(bot.getResponse(sys.argv[1]))

您的导入语句提示聊天机器人 class 带有大写的 B:

from chatterbot import ChatBot

改变

chatbot = Chatbot("SquirralBot",...)

chatbot = ChatBot("SquirralBot",...)

注意聊天中的大写 BBot.