1. 程式人生 > >Creating a Personal Chatbot in Python3 using ChatterBot(Part 1)

Creating a Personal Chatbot in Python3 using ChatterBot(Part 1)

Before we get started, we need to get all of the necessary pip installations. Open your terminal and run the following commands:

Pip installations:

pip3 install chatterbotpip3 install python-levenshtein 

Setting up the ChatBot

First, create a new file and name it Chatbot.py. Before we start, we need to import Chatterbot; so we will do so with the following:

from chatterbot import ChatBot

Next, we will create a new instance of the ChatBot class.

chatbot = ChatBot(‘Brandon’, trainer=‘chatterbot.trainers.ListTrainer’)

Now we need to handle getting responses from the user’s input. Use the following to get a response back from the input “Hello”:

response = chatbot.get_response("Hi there")
print(response)

Your ChatBot.py file should look like this so far:

from chatterbot import ChatBot
chatbot = ChatBot('Brandon',trainer='chatterbot.trainers.ListTrainer')
response = chatbot.get_response("Hi there")
print(response)

Training the ChatBot

To begin training our new chatbot, we will create a new file called Train.py in the same directory as ChatBot.py. Again, we need to import Chatterbot, so we’ll use:

from chatterbot import ChatBot

Now we want to begin allowing our chatbot to learn as we communicate with it. Our method for training is setup where the input comes before the response, and both are separated by a comma. Look at the following implementation below to get a better idea.

First, create an instance of your chatbot.

chatbot = ChatBot('Brandon',trainer='chatterbot.trainers.ListTrainers')

Here we will begin our training. You can run through the training process multiple times to improve responses for particular inputs. You can also use multiple conversations to train on a given chatbot instance. We will cover different ways to train the chatbot at another time.

chatbot.train([    "Hello",    "Hi there!",    "How are you doing",    "I'm doing great, how about you",    "That is good to hear",    "Thank you",    "You're welcome"])
chatbot.train([    "Good bye!",    "See you soon!",])

Your Train.py file should look like this so far:

from chatterbot import ChatBot
chatbot = ChatBot('Brandon',trainer='chatterbot.trainers.ListTrainers')
chatbot.train([    "Hello",    "Hi there!",    "How are you doing",    "I'm doing great, how about you",    "That is good to hear",    "Thank you",    "You're welcome"])
chatbot.train([    "Good bye!",    "See you soon!"
])

Test your ChatBot

Now that you have created your chatbot and a way to train it, open your terminal, navigate to the directory with your ChatBot.py and Train.py files, and run the following:

python3 Train.py

After all of the progress bars have finished loading, you can now run ChatBot.py with the following:

python3 ChatBot.py

The result should return “Hello”. You have just finished setting up and testing your chatbot. Feel free to add more dialogue in the Train.py, and try testing it again. Be sure to run your Train.py file again after adding new dialogue.