Hello, buddies! So in the first of the month we're going to make a simple chat bot using python. I don't want to make you bore so I simplified steps and let's start to make our
Install and Import
You will need to install ChatterBot for this. ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input. As a result, ChatterBot uses a selection of machine learning algorithms to produce different types of responses.
After installing ChatterBot, import ChatBot and ListTrainer!
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer # method to train the chatbot
Now, create the chatbot. Here i have given the name of chatbot as BuddyBot.
bot = ChatBot('Buddy_Bot')
ChatBot()method is used to create the chatbot.
- The chatbot is store in the variable bot.
Setting Trainer to train bot
So, now we have to set the trainer to train the bot.
bot.set_trainer(ListTrainer) #set the trainer
set_trainer()method is used to set trainer to the bot.
Opening the Conversation file
Now, we have to open the file where the conversations are stored. For this we write the following code.
conversation = random.choice(['Hello!', 'How are you?', 'Fine', ''Do you have coffee?])# or open('chats.txt', 'r').readlines()
random.choice()is to choose words randomly.
- Instead of this method, you can use
open('chats.txt', 'r').readlines()But I recommend to use
Train the bot
And now we need to train the bot with the data we have loaded into this script.
bot.train(conversation) # train the bot
train()method is used to train the bot along with loaded data.
- Here we need to pass the conversation as an argument.
Taking input from the user and replying by the bot
Now we have to code for taking input from user and the reply by the bot. Like this,
While True: message = input('You:') if message.strip()!= 'Bye': reply = bot.get_response(message) print('ChatBot:',reply) if message.strip()=='Bye': print('ChatBot:Bye') break
message = input(‘You:’)statement is used to take input from the user.input() function takes input from the user and store it in message variable.
- Now we have to include a condition that is
if message.strip()!= ‘Bye’: .It means if user don’t enter Bye message till then bot will reply the user messages.
- The reply will be generated by the bot using
.get_response()is used to reply to the user.This method takes an argument message which is entered by the user.
print(‘ChatBot:’,reply)statement print the reply of the chatbot.
- Now, if the user enters Bye message then chatbot also reply Bye message and the process will end with exit code 0.
Let's make him more funny😂
- First you need to install
Pyjokesand import it.
- and next add this to the
if message == 'Be funny': joke = pyjokes.get_joke(language='en', category='all')
So the final code of out Python Chat Bot will be as follows,
import pyjokes import random from chatterbot import ChatBot from chatterbot.trainers import ListTrainer # method to train the chatbot bot = ChatBot('MyChatBot') bot.set_trainer(ListTrainer) #set the trainer conversation = conversation = random.choice(['Hello!', 'How are you?', 'Fine', 'Do you have coffee?']) # or open('chats.txt', 'r').readlines() bot.train(conversation) # train the bot while True: message = input('You:') if message.strip()!= 'Bye': reply = bot.get_response(message) if message == 'Be funny': joke = pyjokes.get_joke(language='en', category='all') print(joke) print('ChatBot:', reply) if message.strip()=='Bye': print('ChatBot:Bye') break
And our final result is successful! You can jump from the chair!
This is my results. And second one was funny enough -
As funny you reply, he will answer funny!
Don't forget to read these about python bots!
- How to make an Instagram Bot with Python by Ayushi Rawat
- How I Created a Python Meme Bot for Instagram ? by Hrithwik Bharadwaj
- Building Discord Bots with Python by Dallas Spohn
Hope you leant something new here and don't forget there is a comment section below! Thanks for reading!