Search code examples
python-3.xchatbotchatterbot

'Chatbot' is not defined in Chatterbot


I am making a chatbot through Chatterbot. I am facing the problems as follow:

  1. when I run the code, it shows error, but the ChatBot is imported from chatterbot at the beginning?

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. I want to make the chatbot to distinguish specific texts then to trigger specific corpus, how can I make it? Is the "chatterbot.conversation.Response(text, **kwargs)" class for this purpose? e.g. when the user types "I am leaving", then it will trigger to call the training set "chatterbot.corpus.chinese.squirral_bye_conversation"?

  2. Is it possible if I can store the reply specifically to the database e.g. MongoDB for different users? e.g. when user A replies "I am sick. I got fever and running nose", then the system store "sick" into "status" and "fever" and "running nose" into "symptoms" in the user A's data so that inside the database it would be like JSON:

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

  3. Is it possible to make the chatbot can text the user in specific time range?

The code for the above mentioned is as following. I am very new in programming so the code may be a bit messy. Please feel free to correct. Many thanks.

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

Solution

  • Your import statements hint a ChatBot class with a capitalized B:

    from chatterbot import ChatBot

    Change

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

    to

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

    Note the capitalize B in ChatBot.