Rule-based StandAlone AIML chatbots (Chatbots Part-2)

Chethan Kumar GN
Chatbots Life
Published in
4 min readDec 10, 2018

--

AIMLArtificial Intelligence Markup Language (Rule-based Chatbots)

Pre-requisite: So you want to build a chatbot from scratch? (Chatbots Part-1)

AIML stands for Artificial Intelligence Markup Language. AIML was developed by the Alicebot free software community and Dr Richard S. Wallace during 1995–2000. AIML is used to create or customize Alicebot which is a chat-box application based on A.L.I.C.E. (Artificial Linguistic Internet Computer Entity) free software.

AIML Tags:

There are around 14 tags that we normally use in AIML. There are much more present and there are differences in AIML 1.0 and 2.0 please do check out the version before you use other specific tags.

Basic Tags:

  1. <aiml>: Defines the beginning and end of an AIML document
  2. <category>: Defines the knowledge in a knowledge base.
  3. <pattern>: Defines the pattern to match what a user may input.
  4. <template>: Defines the response of an Alicebot to user’s input.
<aiml version = "1.0.1" encoding = "UTF-8"?>   <category>
<pattern> HELLO </pattern>

<template>
Hello User!
</template>
</category>
<category>
<pattern> WHAT ARE YOU </pattern>

<template>
I am a bot silly!
</template>
</category>
</aiml>

Top 4 Most Popular Bot Design Articles:

1. One metric, one platform and one vertical

2. Designing Chatbot Conversations

3. Distributing a slack app

4. ChatBots — The Rise of Conversational UI

Result:

User: Hello
Bot: Hello User!
User: WHAT ARE YOU
Bot: I am a bot silly!

More Tags:

  1. <star>: Used to match wildcard * character(s) in the <pattern> Tag.
  2. <srai>: Multipurpose tag, used to call/match the other categories.
  3. <random>: Used <random> to get random responses.
  4. <li>: Used to represent multiple responses.
  5. <set>: Used to set value in an AIML variable.
  6. <get>: Used to get value stored in an AIML variable.
  7. <that>: Used to respond based on the context.
  8. <topic>: Used to store a context so that later conversation can be done based on that context.
  9. <think>: Used to store a variable without notifying the user.
  10. <condition>: Similar to switch statements in a programming language. It helps bot to respond to match the input.

For a more detailed description of the tags click here. Now let us get down to actual implementation.

Steps:

Steps in building an AIML rule-based chatbot:

  1. Install AIML modules.
  2. Create a standard startup file
  3. Creating AIML Files
  4. Including random responses in AIML files.
  5. Write a python program with brain module for faster response.

1. Install AIML modules:

For python 2
pip install aiml
For python 3
pip install python-aiml
OR
pip3 install python-aiml

2. Creating a standard startup file:

It is standard to create a startup file called std-startup.xml as the main entry point for loading AIML files. In this case, we will create a basic file that matches one pattern and takes one action. We want to match the pattern load aiml b and have it load our aiml brain in response.

<!-- In the file std-startup.xml --><aiml version="1.0.1" encoding="UTF-8">
<category>

<!-- Pattern to match in user input -->
<!-- If user enters "LOAD AIML B" -->
<pattern>LOAD AIML B</pattern>

<!-- Template is the response to the pattern -->
<!-- This learn an aiml file -->
<template>
<learn>basic_chat.aiml</learn>
<!-- You can add more aiml files here -->
<!--<learn>more_aiml.aiml</learn>-->
</template>

</category>

</aiml>

3. Creating AIML Files:

Above we created the AIML file that only handles one pattern, load aiml b. When we enter that command to the bot, it will try to load basic_chat.aiml. It won’t work unless we actually create it. Here is what you can put inside basic_chat.aiml. We will match two basic patterns and respond.

<!-- In file basic_chat.aiml --><aiml version="1.0.1" encoding="UTF-8">

<category>
<pattern>HELLO</pattern>
<template>
Well, hello!
</template>
</category>

<category>
<pattern>WHAT ARE YOU</pattern>
<template>
I'm a bot, silly!
</template>
</category>

</aiml>

4. Including random responses in AIML file:

You can also add random responses like this. This one will respond randomly when it receives a message that starts with “One time I “. The * is a wildcard that matches anything. As I would like to call it gives more colourful responses

<category>
<pattern>ONE TIME I *</pattern>
<template>
<random>
<li>Go on.</li>
<li>How old are you?</li>
<li>Be more specific.</li>
<li>I did not know that.</li>
<li>Are you telling the truth?</li>
<li>I don't know what that means.</li>
<li>Try to tell me that another way.</li>
<li>Are you talking about an animal, vegetable or mineral?</li>
<li>What is it?</li>
</random>
</template>
</category>

It can be fun to write your own AIML files, but it can be a lot of work. I think it needs around 10,000 patterns before it starts to feel realistic. So feel free to use the AIML files available online.

5. Project and source code:

It is built using python with flask as framework and AIML

Here is a link to the GitHub repo where we can access the code for a standalone chatbot. https://github.com/chethangn/ChatbotAIML

Should I really be using AIML:

The last version of AIML was released 6 years ago. There is a draft for a v2 specifications what was released in 2013 but now news since then.

Even the website http://alicebot.org now redirects to another domain. But, there are plenty of platforms to build chatbots without dealing with AIML.

But I wanted you to know the simplest way in which we can build it

Credits:

  1. https://www.tutorialspoint.com/aiml/
  2. http://www.sohelamin.com

Want to learn more check out the topics below.

More references:

  1. Artificial Intelligence real or is it just a hype of this decade??
  2. Artificial Intelligence: Definition, Types, Examples, Technologies
  3. Artificial Intelligence vs Machine Learning
  4. Why Machine learning for achieving Artificial Intelligence? “ The Need for Machine Learning
  5. Machine Learning Types and Algorithms
  6. Linear Regression Part -1
  7. Linear Regression Part -2(example implementation)
  8. How to get AI related jobs?

Make sure to follow me on medium, linkedin, twitter, Instagram to get more updates. And also if you liked this article make sure to give a clap and share it.

Join our WhatsApp community here.

--

--