DeRive

Intern-City
Intern-City
Published in
4 min readMay 31, 2018

Hello folks, so today we would be developing a chat bot using Rivescript and this article is focused on guiding our steps. So before we begin, I have to explain what a chat bot is and what AI is.

According to Techopedia, “Artificial intelligence (AI) is an area of computer science that emphasizes the creation of intelligent machines that work and react like humans. Some of the activities computers with artificial intelligence are designed for include: Speech recognition. Learning.” While chat bot is the application of AI to build a chat response system that mimics a human.

RiveScript is an amazing tool for building chat bot especially if you are a developer because it allows us to use a couple of languages from JavaScript to Java to Python, etc. But in this tutorial, we would be focused more on JavaScript.

To get started, we need to install node JS. so navigate to https://nodejs.org/en/download/ and download for your operating system. To make sure node installed, open your CMD and type the command “node -v” you ought to see the version of node installed. Also node comes along with npm (node package manager) which allows us to download many cool packages.

So let’s create a new project. Create a new working folder and open your terminal in that working directory then type in the command “npm install rivescript” this would install all the packages needed to develop your chat bot. So let the coding begin.

Create a file called index.js and input the following code there:

const Readline = require('readline');const chalk = require('chalk');const Rivescript = require('rivescript');const bot = new Rivescript();const rl = Readline.createInterface({input:process.stdin,output :process.stdout})bot.loadFile('./training_data.rive', function(){bot.sortReplies();ask();}, function (error){console.log("error reading file" + error);})function ask(){rl.question('You:',(message) =>{var reply=bot.reply('local-user', message);console.log(chalk.yellow('bot-name: '+ reply));ask();} )}

Then create a file with the file extension .rive in the case of our code above, it would be named training_data.rive. This is where the training and modification of the bot would take place. To run the bot on your CMD, type the command “node index.js”

Our first training data would be

+ hello
- Hi

What this does is that whenever we tell our bot hello, the bot replies hi. Cool huh? So guys we just built our first chatbot. Before we proceed, we need to observe some rules in RiveScript. When we are typing the request (the text followed the + that triggers a response the text followed by a -) we don’t use uppercase or punctuations.

Now our chatbot is very basic and if we ask it any other thing that isn’t hello, it throws an error because it doesn’t understand. So we need more training to make it catch responses and make it catch all random questions it doesn’t understand.

To make our bot have multiple replies for a particular question, we could do this:

+ what is your name
- My name is Bot.
- My name is Kevin
- My name is my name.

This would make the bot give a random reply from any of the three responses provided each time you ask the bot for it’s name. So far our bot is doing well. So let’s do something funny here. We could set the bot variable name, age, height, etc. By using the Var keyword. To set the variable names:

! var name = kevin! var age = 20! var height = 10ft.

We can then use it in our request and response. For example:

+ what is your name
- my name is <bot name>

So whenever we ask the bot for it’s name, it tells us “my name is Kevin”. Now our bot is more responsive, now let’s make it recognize abbreviations such as lol, brb, wad up, etc. We could use the following syntax:

! sub lol = laughing! sub brb = be right back

So when we ask our bot a question and we type lol, it recognises it as laughing. Now that our bot is taking shape, let’s make it catch for questions that we didn’t type in. All we have to do is :

+ *
- Sorry I didn’t get that.
- What did you say

One last thing we would do is learn how to catch words used in the request and put it in the reply. For this we use star(*) eg

+ i am *
- Why are you <star1>
- I’m sorry you are <star1>

So if we say I am angry, the bot would either reply why are you angry or I’m sorry you are angry. Other ways we could use it is:

+   i am * and *
- I’m sorry you are <star1> and <star2>

So people this is our simple bot. Though there are hundreds of other commands we could use. To see the full tutorial, visit the url https://www.rivescript.com/docs/tutorial

intern001

--

--

Intern-City
Intern-City

A free spot for the interns at Genesys tech hub to express themselves and their ideas. www.genesystechhub.com