Sonia Singla — Published On December 3, 2021 and Last Modified On December 5th, 2022
Advanced NLP Text

This article was published as a part of the Data Science Blogathon.

Are you fed up with waiting in long lines to speak with a customer support representative? Can you recall the last time you interacted with customer service? There’s a chance you were contacted by a bot rather than human customer support professional. We will here discuss how to build a simple Chatbot in Python and its benefits in Blog Post ChatBot Building Using Python.

Table of Contents

1. What is the meaning of Bots?

2. Type of Bots

3. Simple ChatBot build by using Python (How to make chatbot in Python)

4. Chatterbot

5. Benefits of Chatterbot

What is the meaning of Bots?

Bots are specially built software that interacts with internet users automatically. Bots are made up of algorithms that assist them in completing jobs. By auto-designed, we mean that they run on their own, following instructions, and therefore begin the conservation process without the need for human intervention. 

Bots are responsible for the majority of internet traffic. For e-commerce sites, traffic can be significantly higher, accounting for up to 90% of total traffic. They can communicate with people and on social media accounts, as well as on websites.

Some were programmed and manufactured to transmit spam messages in order to wreak havoc.

Creating ChatBot Building Using Python

                                                                                                           image source Canva

Type of Bots

1. ChatBot — An Artificial Intelligence (AI) programme that communicates with users through app, message, or phone. It is most commonly utilised by Twitter.

2. Social Media Bot- Created for social media sites to answer automatically all at once.

3. Google Bot is commonly used for indexing and crawling. Spider Bots—Developed for retrieving data from websites, the Google Bot is widely used for indexing and crawling. 

4. Spam Bots are programmed that automatically send spam emails to a list of addresses. 

5. Transnational Bots are bots that are designed to be used in transactions. 

6. Monitoring Bots – Creating bots to keep track of the system’s or website’s health. 

Build libraries should be avoided if you want to have a thorough understanding of how a chatbot operates in Python. In 1994, Michael Mauldin was the first to coin the term “chatterbot” as Julia.

Simple ChatBot build by using Python

A ChatBot is merely software by which humans can interact with each other. Examples include Apple’s Siri, Amazon’s Alexa, Google Assistant, and Microsoft’s Cortana.

A. Interaction of User for asking the name

First, we will ask Username

print(“BOT: What is your name?”)
user_name = input()

Simple ChatBot build by using Python

                                            image source Jupyter Notebook

B. Response of ChatBot

To begin with, we are assigning questions and answers ChatBot must ask us, for example, we have assigned three variables with different answers, most important point to note it can be used in code and can be also updated automatically by just changing in values of variables. The format is used to pass the values easily.

name = "Bot Number 286" 
monsoon = "rainy" 
mood = "Smiley"
resp = { 
"what's your name?": [ 
"They call me {0}".format(name), 
"I usually go by {0}".format(name), 
"My name is the {0}".format(name) ],
"what's today's weather?": [ 
"The weather is {0}".format(monsoon), 
"It's {0} today".format(monsoon)], 
"how are you?": [ 
"I am feeling {0}".format(mood), 
"{0}! How about you?".format(mood), 
"I am {0}! How about yourself?".format(mood), ],
"": [ 
"Hey! Are you there?", 
"What do you mean by these?", 
 ],
"default": [
"This is a default message"] }
Response of ChatBot

                                                                    image source Jupyter Notebook

C. Creating a Function Response

Library random imported to choose the response. It will select the answer by bot randomly instead of the same act.

import random
def res(message):
if message in resp: 
        bot286_message = random.choice(resp[message])
else: 
        bot286_message = random.choice(resp["default"])
return bot286_message

D. Another Function

Sometimes the questions added are not related to available questions, and sometimes some letters are forgotten to write in the chat. At that time, the bot will not answer any questions, but another function is forward.

def real(xtext): 
  if "name" in xtext: 
        ytext = "what's your name?"
elif "monsoon" in xtext: 
        ytext = "what's today's weather?"
elif "how are" in xtext: 
        ytext = "how are you?"
else: 
        ytext = ""
return ytext

E. Sending back the message function

def send_message(message): 
print((message)) 
response = res(message) 
print((response))

F. Final Step to break the loop

while 1: 
my_input = input() 
my_input = my_input.lower() 
related_text = real(my_input) 
send_message(related_text)
if my_input == "exit" or my_input == "stop": 
break
Final Step to break the loop

                                        image source Jupyter Notebook

Another example is by installing library chatterbot

Chatterbot

Natural language Processing (NLP) is a necessary part of artificial intelligence that employs natural language to facilitate human-machine interaction.

Python uses many libraries such as NLTK, spacy, etc. A chatbot is a computer program that can communicate with humans in a natural language. They frequently rely on machine learning, particularly natural language processing (NLP).

Use the following commands in Terminal in Anaconda prompt.

pip install chatterbot
pip install chatterbot_corpus

You can also install chatterbot from its source: https://github.com/gunthercox/ChatterBot/archive/master.zip

After installing unzip, the file and open Terminal, and type in: cd

chatter_bot_master_directory. Finally, type python setup.py install.


For installing Spacy
pip install -U spacy

For downloading model “en_core_web_sm”

import spacy
from spacy.cli.download import download
download(model="en_core_web_sm")

or

pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.2.0/en_core_web_sm-2.2.0.tar.gz

Testing installation

import spacy
nlp = spacy.blank("en")
doc = nlp("This is a sentence.")

You will need further two classes ChatBot and ListTrainers

from chatterbot import ChatBot
 from chatterbot.trainers import ListTrainer

First Example

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# Create a new chatbot named Charlie
chatbot = ChatBot('name')
trainer = ListTrainer(chatbot)
trainer.train([
    "Hi, can I help you?",
    "Sure, I'd like to book a flight to Iceland.",
    "Your flight has been booked."
])
# Get a response to the input text 'I would like to book a flight.'
response = chatbot.get_response('I would like to book a flight.')
print(response)

Second example by ChatBot

Bot286 = ChatBot(name='PyBot')
talk = ['hi buddy!',
              'hi!',
              'how do you do?',
              'how are you?',
              'i'm fine.',
              'fine, you?',
              'always smiling.']
list_trainer = ListTrainer(Bot286)for item in (talk):
     list_trainer.train(item)
>>>print(my_bot.get_response("hi"))
 how do you do?
from chatterbot.trainers import ChatterBotCorpusTrainercorpus_trainer = ChatterBotCorpusTrainer(Bot286)
 corpus_trainer.train('chatterbot.corpus.english')

Benefits of Bots –

  1. Understandable information about the customer.
  2. Can be called a selling partner by making and sending the products information.
  3. Provides 24hrs services
  4. Satisfy the need of clients as the customer will not go on waiting for your call. They need the action quickly or will turn to another brand.
  5. Most of the customer prefers sending messages, text, SMS to the company for information. Marketing Bot can result or give your Business growth by making higher sales and satisfying the needs. Facebook Messenger is one of the widely used messengers in the U.S.
  6. Recently chatbots were used by World Health Organization for providing information by ChatBot on Whatsapp.
  7. Facebook Messenger, Slack, Whatsapp, and Telegram make use of ChatBot.
  8. The modern need is there for Bot Building for growth of Business to make progress.
  9. Another example of making use of ChatBo is Google Assistant and Siri.
  10. Bots, for the most part, operate on a network. Bots that can communicate with one another will use internet-based services like IRC.

Conclusion

This article is the base of knowledge of the definition of ChatBot, its importance in the Business, and how we can build a simple Chatbot by using Python and Library Chatterbot.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *