How to create a rule-based chatbot by using RiveScript

Kinder Sham
Analytics Vidhya
Published in
4 min readJun 10, 2020
Photo by Alex Knight on Unsplash

What is a chatbot? It is a service that allows users to interact with it through AI (Artificial Intelligence) or custom automatic rules through the chat communication interface. The user interacts with chatbots through the chat interface. The chatbot can provide various types of services. For example, in the application of the insurance industry, it can provide policy inquiry, premium inquiry, payment inquiry, premium change application, claims inquiry, etc.

With the development of instant messaging (IM) technology, many companies have begun to use the IM platform as an important channel for providing services. The chatbot is a new-generation user interface, companies can build a chatbot on the company’s website or on social software, such as Telegram, Line, and Facebook Messenger, to provide quality customer service.

Is it difficult to build a Chatbot? If I don’t understand the program at all, can I still create my own chatbot? In simple terms, it is not difficult to build a basic operation Chatbot. To create a “smart” Chatbot, it needs to be achieved through artificial intelligence or machine learning technology. And there are many tools on the market that can help you build a chatbot with artificial intelligence.

RiveScript is a scripting language for chatterbots which is very similar to AIML, but matching Mode and condition control are more complete than AIML. RiveScript is a rule-based engine, where rules are created by humans writers in program scripts through a process called dialog flow scripting. These use a scripting metalanguage (simply called a “script”) as their source code. I will show you how to create a chatbot using Python and RiveScript.

In this tutorial, we will be using one Python libraries that require installation. You can easily do this via the pip install command as follows to install RiveScript:

$ pip install rivescript
# --or--
$ easy_install rivescript

RiveScript works well in python 2 and 3, after installation, create a chatbot.py and import the following library. Let’s take a look at the code of the chatbot app that we are building today. You will see that there are less than 20 lines of code:

import re
from rivescript import RiveScript
bot = RiveScript(utf8=True, debug=False)
bot.unicode_punctuation = re.compile(r'[.,!?;:]')
bot.load_directory("eg/brain")
bot.sort_replies()
msg = sys.argv[1]def response(text):
text = text.encode("utf-8")
reply = bot.reply("localuser", text)
reply_text = reply.encode("utf-8").strip()

return reply_text
def main():
input_text = msg
input_text = input_text.encode("utf8")
reply_text = response(input_text)
print ("Bot: " + str(reply_text))
if __name__ == "__main__":
main()

Let’s take some time to make some understanding of the above code.

Lines 1 and 2
Imports re and rivescript libraries.

Lines 3 to 6
Setup the bot dictionary.

Lines 7 to 11
Create a function call response, which is the information returned by the chatbot to humans.

Lines 12 and 16
Create a function call main, which is the main structure of the program.

Line 17 and 18
call the main function.

The standalone mode makes for an excellent RiveScript development environment. It allows you to run interactive conversations, then interacts with them using: commands. This is a special set of interactive command tools for testing and debugging your dialogs during the development and debug phase.

You can define the bot’s dialog flows with a script stored as a normal text file. This is much simpler than the methods used by other chatbot tools. Other chatbot tools usually use a browser-based user interface, JSON, or XML. Writing scripts as text files gives you complete control over the conversation flow and makes it easier to process and upgrade session code using back-end scripts and tools.

To create a chat script, you need to create the file under ./eg/brain, and I will create a file called chat.rive.

Here what a RiveScript script file looks like:

+ (Hi)
- Hello!
- Hi, how are you?
+ (What is your name?)
- (I am Pepper)
+ (Good Morning)
- Morning

In fact, there are many complex functions that can be put into SuperScript scripts, like the following example: create a script to answer what time is it.

> object askTime python
import time

now_hour = int(time.strftime("%H"))
now_min = (time.strftime("%M"))
if (now_hour > 12):
ampm = "pm"
now_hour = now_hour - 12
else:
ampm = "am"
output = str(ampm) + str(now_hour) + str(now_min)

return output
< object
+ (|*)(What time is it now?)(|*)(what time now?)(|*)
- <call>askTime</call>

After saving all the code into a files fire up the command prompt and run the following command:

>python chatbot.py "What is your name?"
>Bot:My name is Pepper

In addition to using RiveScript to create rule-based chatbots, it can also be combined with Microsoft’s Q&AMaker or Google’s Dialogflow to create a chatbot suitable for itself

Thanks for reading! If you enjoyed the post, please appreciate your support by applauding via the clap (👏🏼) button below or by sharing this article so others can find it.

In the end, I hope that you can learn how to create a basic rule-based chatbot. You can also find the full project on the GitHub repository.

--

--

Kinder Sham
Analytics Vidhya

Data scientist, cycling and game player enthusiast. Focus on how to use data science to answer questions.