Codementor Events

How to make a basic ChatBot (Wit.ai) Part-1

Published May 03, 2017
How to make a basic ChatBot (Wit.ai) Part-1

With the always depleting phone memory and more and more phones going cloud based, phone memory is turning into a valuable resource which most users dont like wasting on useless apps they hardly ever open.

According to a report by Personetics, “Close to a quarter of all downloaded apps were deleted after just one use. Research shows that most users have just a few applications they use on a regular basis. These are usually the applications that save time or money (or both) and make life easier.”

Clearly those pesky E-commerce and ticket booking apps dont make the cut.So companies have rapidly started migrating to the Chatbots.

A chatbot (also known as a talkbot, chatterbot, Bot, chatterbox, Artificial Conversational Entity) is a computer program which conducts a conversation via auditory or textual methods.

Well thats what Wikipedia says about it.

In simple terms Chatbots are smart talkitive programs with whom you can talk in normal human language. They gather information from you like any other customer care person would and complete your request using that.
This not only saves space and money but also makes the process more fun and intuitive. So instead of groping around the complex UI or filling long boring forms, you just tell the bot what to do and they do it for you, as simple as that.From the business perspective it saves you the cost of employing an army of customer care operators.A simple program can handle thousands of concurrent requests.
If that still doesnt make sense heres an example:

Example:
If you wanted to buy shoes from Nordstrom online, you would go to their website, look around until you find the shoes you wanted, and then you would purchase them.
1*aDlbZhdTnSiFRp6s5mgAKQ.png
If Nordstrom makes a bot, which I’m sure they will, you would simply be able to message Nordstrom on Facebook. It would ask you what you’re looking for and you would simply… tell it.
Instead of browsing a website, you will have a conversation with the Nordstrom bot, mirroring the type of experience you would get when you go into the retail store.

With the declining app download rate and increasing time spent on social media and messaging apps Chatbots seems to have a bright future ahead.
So lets begin making our little Chatbot

So basically Chatbot can be divided into 2 parts:

  1. Natural Language Processing : This involves processing the sentence and determining what the user means by the sentence.NLP uses complex machine learning algorithms to break the sentence into smaller parts which can be broad classified into two parts : Intent(what the user wants to do) and Entities(details)

I want to book a flight to Paris from delhi for tomorrow

Here the intentions are "Booking a flight" so thats the intent
and the destination(Paris),source(Delhi) and time(tomorrow) are the details for the intent. These are called entities.
There are a lot of online services providing NLP with varying complexity like API.ai(google), Luis.ai(microsoft),Watson(IBM) and Wit.ai(Facebook).
We shall be using Facebook's Wit.ai for our tutorial since its easier to use for facebook messenger and well cause its free!!

  1. Conversations: This is the second element of the chatbot. A good chatbot isnt just a NLP system who works on commands cause that would be annoying . Rather a good chatbot should be a freely flowing conversation where the bot gathers information from user in seamless fashion and validating the information and adapting the conversation according to the users replies.Thats what Conversation of a chatbot handles .Simply put it decides what to say and when to say it.

So lets come to the implementation of our bot

NLP
We would be using Wit.ai for our NLP needs.

1.So login to wit using your facebook credentials and create a new app

create-app-1b8ef48b.png

Once youre done with that you're ready to start.Wit provides two services NLP(natural language processing) and NLU(natural language understanding).
while NLP is simple intent and entity break down . NLU also handles the context of the conversation allowing us to map full mock conversations on the dashboard itself

2.So lets first start by creating a simple weather bot.Go to the understanding tab.
detect-first-entity-41f1ac09.gif

Type in a sentence youd like the bot to answer. Something simple like "How's the weather?" or "whats the temperature?"
Now add a new intent from the dropdown and name it Get_Weather. This would help us detect if the user wants to know about the weather.

3.Validate this sentence.
improve-detection-e7ad3da9.gif
Next continue adding similar sentences to help the bot "learn" to detect the pattern.

  1. Now go on the stories tab and create a new story by clicking on new story.
    chatstories.JPG

Type in "How is the weather" on the bubble marked "User says" . set the intent to Get_Weather and click "Bot sends" and type in "The weather is good." and save the story
storystart.JPG

There you just made your first Chatbot!! You may test it in the little testing chatwindow found near the bottom right of the window. Test it out!

  1. Now this Chatbot is clearly pretty useless and mundane. Also the reply is always the same irrespective to the actual weather. Also shouldnt we make it a bit smarter so it can give weather forecast of different areas.

  2. Type in the input box "whats the temperature in New York?". Wit should automatically get the intent out of this as "Get_Weather", if it doesnt then add an intent and select "Get_Weather" intent from the dropdown.
    Next highlight "New York" and make a new entity, lets call it "City". This would allow would give the capability to extract the city from the user's message and give the weather of that city.

Wit.ai basically uses Machine learning to understand a sentence and extract the meaning in form of entities and intent from the sentence.It does this using three NLP techniques namely : free-text,keywords and traits.
You can read about them from the Wit Docs what you need to understand is that traits are found wrt the whole sentence, free-text is just a bunch of words which are found in a specific region of the sentence and keywords are simply pre-defined words which the bot detects.
The "City" entity would be of free-text and keyword type since it is usually found in a specific area of a sentence or we can simply add a bunch of keywords for major cities.
Please note that instead of creating a new entity we could also have used in-built entities provided by Wit.ai.But we made a new one for understanding purpose
date.JPG
Next add a new utterance. "how will be the weather in delhi tomorrow". you would notice that apart from detecting a Get_Weather intent it also detected an inbuilt entity called dateTime(if it didnt just add one :p). Wit uses this amazing library called "duckling" parse the date into dd/mm/yyyy. its pretty robust and since its pre defined you dont need to train it much.
Okay we are set on the training part. Add a few more variations of the utterance we already added to make the bot "smarter". you would notice that it starts automatically mapping the intent and entity automatically just after a training with a few utterance. Thats the true power of Wit. Its smart and simple to train.

Now lets understand how these entities can be used in our conversation.

7.Move back to the stories section, Now lets change the opening sentence of the conversation. Edit it by clicking on it and type in

how would weather be in london tomorrow

Wit automatically maps it to the entities. Now we shall use these to customize the bot's reply.

the weather in {city} {datetime} would be nice

notice we used placeholders in the sentence. Wit will see these placeholders and search if any such key exists for this name and replace it. this makes the process of customizing replies very easy.
And not just entities we can add variables into the "context" of the conversation which we shall be using at a later stage to "remember" the details of the conversation.

Try making a basic conversational bot using this which gives you standard replies and tells jokes.

In the next part we shall discuss how to connect it to our node server,how to hit API to get actual weather results and deploy this chatbot on to facebook.

Discover and read more posts from Srijan Saxena
get started
post comments5Replies
Joelyler
6 years ago

How to make a basic ChatBot is the real question which must be idea for the readers. Different readers will get connected with the bestessay uk and other basic ideas. I am happy to have this blog.

Elisabetta
7 years ago

And the next post, part 2?

Luong Cong Chien
7 years ago

Hey you!
Can you help me , i want to integrate wit.ai with my own chatbot application. Would you tell me how to create it?
Thanks you.

Victor de la Cruz
7 years ago

Hi, maybe I can help u to integrate wit.ai to your proyect, currently I’m working in java and telegram API, I also have an example using scala in this repo
https://github.com/VCGDEV/support-ctx-bot/tree/master

Show more replies