How To Use Chatterbot In Python With Examples – Updated 2022?

Spread the love

Do you want to create your Chatbot with minimum lines of codes? Yes, now it’s possible to build a Chatbot in Python in less than 20 lines of code. In one of my previous articles, I have explained in detail about building a Chatbot In Python Using NLP (NLTK). In this article, we will use a Python library named ChatterBot to build our Chatbot in Python. The ChatterBot library is very easy to use and requires only a beginner level of coding in Python.

Chatbot is becoming very popular nowadays due to their Instantaneous response, 24-hour service, and ease of communication.

Build-A-chatbot-in-python-in-less-than-20-lines-of-codes-ChatterBot
ChatterBot | Chatbot In Python

What Is ChatterBot?

ChatterBot is a Python library developed by Gunther Cox. It is used to generate automated response based on the user’s input. It uses a selection of machine learning algorithm to generate or produce different types of responses. For more information, read official documentation.

Example of ChatterBot response-

user: Good morning! How are you doing?
bot:  I am doing very well, thank you for asking.
user: You're welcome.
bot:  Do you like hats?

How To Install ChatterBot In Python?

ChatterBot is an external library and we have to install it first before using it. There are three methods available to install this library on your local system.

1. Install from PyPi

The Python Package Index (PyPI) is a repository of software for the Python programming language. To install it from PyPi using pip run the following command in your terminal.

pip install chatterbot

2. Installing from GitHub

You can install the latest version directly from GitHub repository using pip.

pip install git+git://github.com/gunthercox/ChatterBot.git@master

3. Installing directly from the source

Follow these steps to install the ChatterBot directly from the source.

3.1. Clone the repository using the command given below

git clone https://github.com/gunthercox/ChatterBot.git

3.2. Install the downloaded code using pip

pip install ./ChatterBot

What Is Training Data Or Corpus In Chatbot?

In any machine learning model, we need a dataset using which we can train the model to predict the desired output. For example, predict future sales using historical data.

In our case, the corpus or training data are a set of rules with various conversations of human interactions.

Corpus can be created or designed either manually or by using the accumulated data over time through the chatbot.

Chatbot Corpus - Chatterbot Or Chatbot in Python
Example of Corpus | ChatterBot In Python

The above image shows the structure of a corpus that includes intents, tags, patterns, responses, and context.

Read article on Python Text To Speech Using Real Jarvis Voice.

How To Implement ChatterBot In Python?

To implement it the Chatbot using ChatterBot in Python, follow the below lines of code.

# Import "chatbot" from
 
#chatterbot package
from chatterbot import ChatBot

# Inorder to train our bot, we have
# to import a trainer package
# "ChatterBotCorpusTrainer"

from chatterbot.trainers import ChatterBotCorpusTrainer

# Provide a name to the chatbot 'Pykit-Bot'
chatbot = ChatBot('Pykit-Bot')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Now train our bot with multiple corpus
trainer.train("chatterbot.corpus.english.greetings",
			"chatterbot.corpus.english.conversations" )

response = chatbot.get_response('Who are you?')
print(response)

Output:

Pykit-Bot: I am just an artificial intelligence.

Note: You can use your own dataset or training corpus to train the Chatbot based on your business requirement. For example, read about training in its official documentation.

Summary

In this article, we discussed about ChatterBot, a Python library and its installation. We have also discussed a very simple example on how to create a Chatbot in Python.

Interested in learning Python, read ‘Python API Requests- A Beginners Guide On API Python 2022‘.

Feel free to contact us in case of any queries.


Spread the love

Related Posts

This Post Has 2 Comments

Leave a Reply

Your email address will not be published.

© 2022 Pykit - All Rights Reserved!