How to create a chatbot by coding

How to create a chatbot by coding

Here I will show a simple algorithm on chatbot using coding that works as a human to communicate with any people who visit your website. Chatbots help to market social sites and contact clients in a few moments. 

Complete instruction for chatbot design by coding

Now we are going to create a chatbot using python. It works to assist a human in interacting with others. Here I create simple chatbots that help me learn more about creating a unique design chatbot. Let’s go to build a chatbot for any website.

Create a new chatbot on your website; you must create before some folder and file on your devices that are like-

  • Chatbot_model.h5 – It is an expert model that provides information and the weight of neurons.
  • Intends.JSON– This is a data file; it selects patterns and responds to any device.
  • Train_chatbot.py– This is a python scripting file that includes the chatbot structure and instructions to set up complete and more unique design chatbots.
  • Classes. pkl-Here include all list categories
  • my-chatbot.py– It provides some unique services and a social process to add chatbot by using GUI. The benefits of this are to connect viewers instantly.
  • Words.pk– This works like a dictionary because it contains a store that includes all of the words on python. 

Create these above files on your device under C drive, then build up a chatbot by coding. Created above files open one by one and type here needed code that makes a perfect chatbot.

Here, you can find out two ways to call python on your devices: to create a chatbot called a chatterbot library, install or install a new python environment library. These both work the same work.

Now I use a second method to create a chatbot for a website.

Following python terminal to install a library using pip

installer.

$ pip installer chatterbot

$ pip installer chatterbot_corus

First of all, open the my-chatbot.py file here and include necessary variables in python. Include these codes under the my-chatbot.py file.

#create a chatbot

Mybot = ChatBot(

Name = ‘Anirban Hossain’,

Read_only = True,

Logic_adapters = [

‘chatterbot.logic.MathematicalEvaluation’,

‘chatterbot.logic.BestMatch’

]

)

Then open train_chatbot.py and click here to type some

instructions like

#training chatbot

Small_convo  = [

‘Hi, there,

‘Hi’,

‘How do you do?’,

‘How are you?’,

‘I’m Okay’,

‘What’s your name?’,

‘Do you know Pythagorean theorem’,

‘I’m Anirban Hossain. Ask me any kind of math questions, please

]

Math_convo_2 =[

‘Pythagorean theorem’,

‘A squared plus b squared equals c squared,

]

Math_convo_2 = [

‘Law of Cosines’,

‘c**2 = a**2 + b**2 – 2*a*b*cos(gamma)’,

 ]

Then include some code under your creating file that like-

#using the ListTrainer class

List_trainee = List Trainer(myBot)

For i in (small_canva_math_convo_1_math_convo_2);

list _trainee.train(i)

Now you can add this section on your website to

communicate with python

#conversation

>>print(myBot.get_response(“Hi, there”))

Hi

>>print(myBot.get_response(“What’s your name?”))

I’m Anirban Hossain. Ask me any kind of math questions, please.

>>print(myBot.get_response(“Do you know Pythagorean theorem”))

A squared plus b squared equals c squared.

>>print(myBot.get_response(“Tell me the formula of law of cosines”))

c**2 = a**2 + b**2 – 2*a*b*cos(gamma)

If you want you can do this process by chatterbot in python. Here you can use it before creating files. First of all, start this process and include some code of python that like- 

from chatterbot import chatbot

    from chatterbot.trainers import ListTrainer

    #creating a new chatbot

    chatbot = Chatbot(‘Edureka’)

    trainer = ListTrainer(chatbot)

    trainer. train([ ‘hi, can I help you find a course’, ‘sure I’d love to  find you a course’, ‘your course have been selected’])

    #getting a response from the chatbot

    response = chatbot.get_response(“I want a course”)

    print(response)

Under my_chatbot.py include some code for creating a

chatbot

    from flask import Flask, render_template, request

    from chatterbot import ChatBot

    from chatterbot.trainers import ChatterBotCorpusTrainer

    app = Flask(__name__)

english_bot = ChatBot(“Chatterbot”, storage_adapter=”chatterbot.storage.SQLStorageAdapter”)

    trainer = ChatterBotCorpusTrainer(english_bot)

    trainer.train(“chatterbot.corpus.english”)

    @app.route(“/”)

    def home():

    return render_template(“index.html”)

    @app.route(“/get”)

    def get_bot_response():

    userText = request.args.get(‘msg’)

    return str(english_bot.get_response(userText))

    if __name__ == “__main__”:

    app.run()

Here you must need to include index.html file and type some

code like-

<!DOCTYPE html>

<html>

<head>

<link rel=”stylesheet” type=”text/css” href=”/static/style.css”>

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js”></script>

</head>

<body>

<h1>Flask Chatterbot Example</h1>

<div>

<div id=”chatbox”>

<p class=”botText”><span>Hi! I’m Chatterbot.</span></p>

</div>

<div id=”userInput”>

<input id=”textInput” type=”text” name=”msg” placeholder=”Message”>

<input id=”buttonInput” type=”submit” value=”Send”>

</div>

<script>

function getBotResponse() {

var rawText = $(“#textInput”).val();

var userHtml = ‘<p class=”userText”><span>’ + rawText + ‘</span></p>’;

$(“#textInput”).val(“”);

$(“#chatbox”).append(userHtml);

document.getElementById(‘userInput’).scrollIntoView({block: ‘start’, behavior: ‘smooth’});

$.get(“/get”, { msg: rawText }).done(function(data) {

var botHtml = ‘<p class=”botText”><span>’ + data + ‘</span></p>’;

$(“#chatbox”).append(botHtml);

document.getElementById(‘userInput’).scrollIntoView({block: ‘start’, behavior: ‘smooth’});

});

}

$(“#textInput”).keypress(function(e) {

if(e.which == 13) {

getBotResponse();

}

});

$(“#buttonInput”).click(function() {

getBotResponse();

})

</script>

</div>

</body>

</html>

Also added some CSS to design your chatbot. Creating a file

and including some CSS code

Body

{

font-family: Garamond;

background-color: black;

}

H1

{

color: black;

margin-bottom: 0;

margin-top: 0;

text-align: center;

font-size: 40px;

}

H3

{

color: black;

font-size: 20px;

margin-top: 3px;

text-align: center;

}

#chatbox

{

background-color: black;

margin-left: auto;

margin-right: auto;

width: 40%;

margin-top: 60px;

}

#userInput {

margin-left: auto;

margin-right: auto;

width: 40%;

margin-top: 60px;

}

#textInput {

width: 87%;

border: none;

border-bottom: 3px solid #009688;

font-family: monospace;

font-size: 17px;

}

#buttonInput {

padding: 3px;

font-family: monospace;

font-size: 17px;

}

.userText {

color: white;

font-family: monospace;

font-size: 17px;

text-align: right;

line-height: 30px;

}

.userText span {

background-color: #009688;

padding: 10px;

border-radius: 2px;

}

.botText {

color: white;

font-family: monospace;

font-size: 17px;

text-align: left;

line-height: 30px;

}

.botText span {

background-color: #EF5350;

padding: 10px;

border-radius: 2px;

}

#tidbit {

Position:absolute;

Bottom:0;

Right:0;

width: 300px;

}

Complete this process you create your new and unique chatbot on your website. Have any questions feel free to ask me in the comment box.

Leave a Reply