We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

How to Create Chatbot Using Python

How to Create Chatbot Using Python

What is a chatbot

A chatbot is also known chatterbot, is an artificial intelligence-driven software program that serves the purpose of making conversation with users either by text or speech

Conversation with text

Normally these types of chatbots are been found in most of the websites to answer questions of the users. These chatbots are been programmed to answer the user questions.

Conversation With Text

Conversation with speech

Conversation with speech

For this kind of chatbots, Siri, Alexa, Google Mini are some of the best examples

Chatbots are been programmed to perform certain tasks for the user. They are used for performing tasks like booking hotel rooms, setting alarms, making transactions, etc., They are many possibilities with chatbots, which become endless with the advancement in technology in the field of artificial intelligence.

Chatbots evolution

In 1966 Joseph Weizenbaum created a Natural Language Conversational program. It featured a conversation between a user and a computer program. After this came the new age of technology for creating chatbots

Limitations of chatbots

With the advancements in technology, there are also certain limitations or difficulties in working with chatbots.

Domain knowledge:

It is difficult for chatbots to fill the boundaries when it comes to conversation with a human. It lacks the domain knowledge

How does a chatbot work?

When it comes to the working of chatbots, it is categorized into two,

  •  Self-learning approach
  •  Rule-based approach

Self-learning approach

In this category, the bots follow the machine-learning approach and it is further categorized into two types,

  • Generative models

Rather than searching from a set of answers, these models come up with answers, which makes them intelligent

  • Retrieval-based models

According to the user’s inputs, the bots retrieve the best suitable approach for the user from the list of responses.

Libraries for a chatbot in python

If these libraries are not present in your machine, open a command prompt and type the below commands to install the required packages

pip install chatterbot-corpus
pip install chatterbot

Click on the enter button after typing the first command, the packages and all its dependencies will automatically be downloaded, an internet facility is required for downloading the packages.

Follow the same steps and install the second package using the second command

Working

After installing the required packages, we have to import the packages into our IDE(Integrated Development Environment), the platform in which we are going to write our python codes.

Importing packages

#codes
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

Creating a bot and training it

#codes
chatbot = ChatBot(name='MyBot', read_only=True,
logic_adapters=['chatterbot.logic.MathematicalEvaluation',  'chatterbot.logic.BestMatch'])

name – represents the name of the bot

read_only = True – to disable the bots ability to learn from training

logic_adapters – it is a list of adapters used for training the bot

There are several other of them like the above provided by chatterbot,

‘chatterbot.logic.MathematicalEvaluation’ – it enables the bot to solve the math problems

‘chatterbot.logic.BestMatch’ – it chooses the best match from the provided responses

#Codes
inputs = ['hi','hai!',
'how do you do?',
'I am cool, how about you?',
'always cool buddy',
'glad to hear that',
'I really feel awesome',
'Excellent, glad to hear that',
'not so good',
'sorry to hear that',
'what is your name?',
'I am a chatbot. You can ask me a math questions']
math_que1 = ['Pythagorean theorem', 'a1 squared plus b1 squared equals c1 squared']
math_que2 = ['law of cosines','c1**2 = a1**2 + b1**2 - 2 * a1 * b1 * cos(gamma)']

Now, we need to train our bot

# Codes
list_trainer = ListTrainer(chatbot)for input in (inputs, math_que1, math_que2):
list_trainer.train(input)

Now the bot is ready to communicate

We can use .get_response() to communicate with our chatbot.

# Codes
print(chatbot.get_response(‘hi))
hai print(chatbot.get_response('how do you do?'))
I am cool, how about you? print(chatbot.get_response(‘what is your name?’))
I am a chatbot. You can ask me a math questions print(chatbot.get_response('Pythagorean theorem'))
'a1 squared plus b1 squared equals c1 squared' 
print(chatbot.get_response(‘Any idea on law of cosine?’))
'c1**2 = a1**2 + b1**2 - 2 * a1 * b1 * cos(gamma)'

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring
Besant Technologies WhatsApp