Building A Chatbot Using Chatterbot Library In Python

Chatbot Using Chatterbot Library in Python
Chatbot Using Chatterbot Library in Python

Are you excited to build a Chatbot Using Chatterbot Library in Python? Let’s dive in!

What is a Chatbot?

A chatbot is an Artificial Intelligence-based software designed to interact with humans in their natural languages. Moreover, Chatbots usually make conversations via auditory or textual methods, and they can even easily imitate human languages to communicate with human beings in a human-like manner. Furthermore, the chatbot is one of the best applications of natural language processing (NLP).

Almost every company has a chatbot deployed today to engage with its users. The following are how companies are using chatbots are:

  • To deliver Crime Information
  • To connect customers and their finances
  • As a customer support

Also Read Introduction to Python list

How Chatbots Help Businesses Improve Customer Service

  • Chatbots provide Quick Responses to Users.
  • Chatbots Create Engagement.
  • In addition, Chatbots can help you in saving the cost on Customer Service
  • To Handle some uncomplicated tasks
  • Moreover, Chatbots also reduce Human Error

Chatbots are categorized into the following ways

  • Rule-Based Chatbot: Rule-based chatbots are also called Decision-Tree bots, which are provided with a database of responses and are also given some protocols that will help them match out an appropriate response from the given database.
  •  Independent Chatbots: Independent chatbots are bots that are based on machine learning. In addition, these usually rely on training a neural network to “think” for itself by providing it with thousands of or at times millions of examples of what the bot needs to be capable of understanding. Also, they improve themselves in a short period and are most widely used for entertainment and science.
  • NLP Chatbots: These are the most advanced chatbots so far. They are a combination of best from rule-based chatbots and keyword chatbots. In addition, the chatbots generally use Natural Language Processing to understand the context and the intent in the user’s requests. Hence, it act accordingly. These chatbots can handle multiple requests at a time from the same user at ease.

What is a Chatterbot Library In Python?

ChatterBot is a library in python that makes it easy to generate automated responses to a user’s input. Also, ChatterBot generally uses selective ML algorithms to produce different types of responses. Hence, this makes it easy for developers to in creating chatbots and automate conversations with users.

Don’t Miss the Complete guide to Github for beginners!

Building a Chatbot Using Chatterbot Library In Python

The first step we need to do is to install the Chatterbot library. Even for creating a chatbot, we need to install Chatterbot Corpus. Corpus is generally defined as the collection of words. Moreover, it contains a corpus of data that is going to be included in the chatterbot module. Bots generally use this corpus to train themselves.

Pip method is used to install chatterbot and chatterbot__corpus.

Installation commands for terminal:

pip install chatterbot

pip install chatterbot_corpus

Installation commands for Jupyter Notebook:

!pip install chatterbot

!pip install chatterbot_corpus

Let’s import the Chatbot class of the chatterbot module first.

# Importing chatterbot

from chatterbot import ChatBot

Creating a Chatbot Instance in Python

Now, we will give any name to the chatbot of our choice by creating a Chatbot object.

# Create object of ChatBot class

bot = ChatBot(‘Buddy’)

[nltk_data] Downloading package averaged_perceptron_tagger to

[nltk_data] /root/nltk_data…. .

[nltk_data] Unzipping taggers/averaged_perceptron_tagger.zip.

[nltk_data] Downloading package stopwords to /root/nltk_data…

[nltk_data] Unzipping corpora/stopwords.zip.

[nltk_data] Downloading package wordnet to /root/nltk_data…

[nltk_data] Unzipping corpora/wordnet.zip. .

We can position the storage adapter with the chatbot object. Storage Adapters usually allow you to connect to a particular storage unit or network. To use a storage adapter, we need to specify it. We will be positioning the storage adapter by assigning it to the import path of the storage that we want to put to use. Moreover, here we will use SQL Storage Adapter, which permits the chatbot to connect to its databases in SQL. By using the database parameter, we will now create a new SQLite Database. Follow the code below for creating a new database for our chatbot.

# Creating the object of ChatBot class with the Storage Adapter

bot = ChatBot(

    ‘Buddy’,

    storage_adapter=’chatterbot.storage.SQLStorageAdapter’,

    database_uri=’sqlite:///database.sqlite3′

)

Also, we can position the logical adapter with a chatbot object. When more than one logical adapter is put to use, the chatbot will calculate the confidence level and the response and return the output with the highest calculated confidence. In addition, we have used two logical adapters here: BestMatch and TimeLogicAdapter.

#Creating the object of ChatBot class with the Logic Adapter

bot = ChatBot(

    ‘Buddy’,  

    logic_adapters=[

        ‘chatterbot.logic.BestMatch’,

        ‘chatterbot.logic.TimeLogicAdapter’],

)

Training Our Chatbot With Python In Python

Firstly, let us import the ListTrainer and create its object by passing the Chatbot object and calling the train() method by passing a list of sentences.

# Import ListTrainer

from chatterbot.trainers import ListTrainer

trainer = ListTrainer(bot)

trainer.train([

‘Hi’,

‘Hello’,

‘I need your help regarding my order’,

‘Please, Provide me your order id’,

‘I have a complaint.’,

‘Please elaborate, your concern’,

‘How much time will it take?,

‘An order usually takes 3-5 Business days to get delivered.’,

‘Okay, Thanks’,

‘No Problem! Have a Good Day!’

])

List Trainer: [####################] 100%

Testing our Chatbot In Python

Lastly, the next step is to test the chatterbot’s conversational skills. For testing its responses, we will call get_responses() method from the Chatbot instance.

#Getting a response to the input text ‘I would like to book a flight.’

response = bot.get_response(‘I have a complaint.’)

print(“Bot Response:”, response)

Bot Response: Please elaborate, your concern

Now, we will create a while loop for our chatbot to run in. If we get a “Bye” or “bye” statement from the user, we can end the loop and then stop the program.

name=input(“Enter Your Name: “)

print(“Welcome to the Bot Service! Let me know how may I help you?”)

while True:

    request=input(name+’:’)

    if request==’Bye’ or request ==’bye’:

        print(‘Bot: Bye’)

        break

    else:

        response=bot.get_response(request)

        print(‘Bot:’,response)

Enter Your Name: Avinash

Welcome to the Bot Service! How may I help you?

Avinash: I need your help regarding my order

Bot: Please, Provide me your order id

Avinash:12345

Bot: No Problem! Have a Good Day!

Avinash: Bye

Bot: Bye

Conclusion

Congratulations, you have made a Chatbot here. We hope you have learned how to create Chatbot Using Chatterbot Library in Python. For more exciting tutorials, you can visit our blog section here.

Also Read Installing Jupyter Notebook to learn python from scratch!

LEAVE A REPLY

Please enter your comment!
Please enter your name here