×

Search anything:

Getting started with AIML to create Chatbots

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

AIML stands for Artificial Intelligence Markup Language and is used to create Chatbots. Chatbots are software applications with whom you can have normal conversations instead following an elaborate syntax or commands.

Following article contains some basic utilities which can equip you to write a fulling functioning AIML bot.

UDC - Ultimate Default Category

It derives itself from eXtensible Markup Language, and so every opening tag has a closing Tag, <aiml> shall be closed with a backward slash as follows <\aiml>.

Below it the UDC file with nothing much incorporated.

<?xml version="1.0" encoding="UTF-8"?>
<aiml>
  <category>
    <pattern>*</pattern>
    <template>I have no answer for that.</template>
  </category>
</aiml>

Inside the AIML tag we have categories, Everything a bot can understand and revert to is contained in categories wherein.
Pattern - It describes the user input
Template - It describes the Bot's output.

For example if I include the following:

<category>
    <pattern>NAME A COOL WEBSITE</pattern>
    <template>I think you are looking for iq.opengenus.org</template>
</category>

This is how it will reflect in your chatbot.
ExampleCategory

You may notice we used Uppercase alphabets for our pattern, that is because patterns are not case-sensitive and do not consider punctuation marks.

Wildcards to deal with numerous categories

  • **The Asterisk ' * ' **

If the user says one or more words, where the asterisk is placed, it will pertain to them and deal with them similarily.

For instance if you add:

<category>
    <pattern>NAME A COOL *</pattern>
    <template>OpenGenus it is!</template>
</category>

AsteriskCategory

If you want an exact amount of words, you may add exact amount of space separated asterisks!
Take note if you have an exact category defined, it doesn't override that.
AsteriskCategoryNonOverride

  • The Underscore ' _ '

The underscore is the same as the asterisk, but it does override the exact case

For instance if you change it to:

<category>
    <pattern>NAME A COOL _</pattern>
    <template>OpenGenus it is!</template>
</category>

UnderscoreCategory

Underscores aren't very practical and need a lot of handling while care, and so we will switch to asterisk.

The star tag

These help repeating what the user said!

<category>
    <pattern>SAY *</pattern>
    <template><star/></template>
</category>

In case of multiword inputs, you can index them according to appearance.

<category>
    <pattern>SAY * IS BETTER THAN *</pattern>
    <template>I believe <star index="1"/> and <star index="2"/> must be given an equal chance! </template>
</category>

<star/> is by default <star index="1"/>.
Also did you notice the '/' at the end? Star is a self closing tag, so it precedes with a / and not written as </star>
Thats a smart bot, isn't it?

StarCate4gory

Predicates 'set' and 'get'

Our conversations become worthwhile with the ability to take note of specifics from the sentences exchanged.

Let's take a very simple example, building a conversation as follows
User - Hey
Bot - Hi! My I know your name please.
User - I am John.
Bot - Hey John! How can I help you?
User - Tell me a secret!
Bot - But John, it won't be a secret then!

Any person introduced to programming shall get the idea of saving the name in a variable to be used later! Thats exactly what we do.
We do this by writing <set name = "firstname"><star/></set> Everything enclosed in the set tag is saved.
To retrive it we use 'get' , by saying <get name = "firstname"/>

To implement it, use something like the following!

<category>
    <pattern>HEY *</pattern>
    <template>Hi! My I know your name please.</template>
  </category>
  
  <category>
    <pattern>I AM *</pattern>
    <template>Hey <set name = "firstname"><star/></set> How can I help you?</template>
  </category>
  
  <category>
    <pattern>TELL ME A SECRET</pattern>
    <template>But <get name = "firstname"/>, it won't be a secret then!</template>
  </category>

And there is our little conversation!
SetGetConvo

Having fun with the 'random' tag

Replying the same to everything is boring! Sometimes you say 'Hi' , the other times 'Hey there' sometimes 'Glad to see you!', the random tag gives you the option to get a random selection from a list of objects.
You just need to list your objects (as list items be the <li> tag) within the <random> tag, inside your <template> tag. Demonstrated as follows!

<category>
        <pattern>ROLL A DICE</pattern>
        <template>
            You get a <random>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
            </random>
        </template>
    </category>

RollADice

Getting Behind the scenes with the 'think' tag

You may have noticed that when we used the <set> tag, we had to display the variable itself too! Instead of that we could also work behind the scenes! Anything enclosed in the <think> tag, will not be displayed.

We can edit our category, where the user tell's its name to take a full name, by saving the surname using a <think> tag, and catch up on the full name later as follows!

<category>
    <pattern>I AM * *</pattern>
    <template>
        Hey <set name = "firstname"><star index = "1"/></set>. 
        <think><set name = "surname"><star index = "2"/></set></think> 
        How can I help you?
    </template>
  </category>
  
  <category>
      <pattern>DO YOU REMEMBER MY FULL NAME</pattern>
      <template>Yes! You are <get name = "firstname"/> <get name = "surname"/></template>
  </category>

There you have it!

ThinkTag

Getting contexts by means of the 'that' tag

Sometimes same words mean different things with context! For instance a person saying yes to help is different from a yes to destroy, but it is still a yes! Not all answers can be governed by words, we also need context.

The context is referred to as a part of the <that> tag, which contains the sentence which is being replied to while reflecting a pattern.

To illustrate this, lets give the bot some questioning ability and see what happens!

<category>
        <pattern>ASK ME A QUESTION</pattern>
        <template>
            <random>
                <li>Do you like me?</li>
                <li>Would you rather be talking to a human right now?</li>
            </random>
        </template>
    </category>
    
    <category>
        <that>DO YOU LIKE ME</that>
        <pattern>YES</pattern>
        <template>Yayy! I find you good as well!</template>
    </category>
    
    <category>
        <that>DO YOU LIKE ME</that>
        <pattern>NO</pattern>
        <template>Such a pity! Someday you will understand the magic of AIML!</template>
    </category>
    
    <category>
        <that>WOULD YOU RATHER BE TALKING TO A HUMAN RIGHT NOW</that>
        <pattern>YES</pattern>
        <template>I'll get back to so with some technological advancements then!</template>
    </category>
    
    <category>
        <that>WOULD YOU RATHER BE TALKING TO A HUMAN RIGHT NOW</that>
        <pattern>NO</pattern>
        <template>Seems like I'm getting good at this!</template>
    </category>

It works as follows!
thatag

Note - If you want to use the star present in your <that> tag instard of the <pattern> tag, you may use <thatstar/> instead of <star/>.

The 'srai' tag

You might find online that it stands for symbolic recursion in artificial intelligence. This isn't official, but might be true!
Using it you can use patterns from other categories!

For instance, there are several ways to ask for a question, putting th following code will make them all refer to the category with <pattern>ASK ME A QUESTION</pattern>.

<category>
        <pattern>ANOTHER QUESTION PLEASE</pattern>
        <template><srai>ASK ME A QUESTION</srai></template>
    </category>
    
    <category>
        <pattern>ASK ME MORE</pattern>
        <template><srai>ASK ME A QUESTION</srai></template>
    </category>
    
    <category>
        <pattern>CAN I HAVE A QUESTION FOR ME</pattern>
        <template><srai>ASK ME A QUESTION</srai></template>
    </category>

It works as follows
sraitag

This can help you reduce repetition in a lot of places.
TRY YOURSELF - Try using a combination of <srai> tag with <star/> or <thatstar> to reduce repetition even further!

You can also catch patterns and work with them to make your bot seem more friendly and familiar as follows! The example below also reflects a good use of the '_'.

<category>
        <pattern>_ PLEASE</pattern>
        <template>You are so polite. <srai><star/></srai></template>
    </category>

Please

Note - Beware of infinite loops!
TRY YOURSELF - Try adding some typos or slangs, and make it understandable using <srai>.

Properties

You can go to the properties file in the Systems folder, instead of hradcoding certain information in your bot, you can add some more information in the properties, like the following

  • Name of the Bot
  • Name of the organisation
  • Name of the owner
  • etc.

Example
Add the following to it

["name","OpenGenusBot"],

and the following to your udc file

 <category>
      <pattern>WHO ARE YOU</pattern>
      <template>I am <bot name = "name"/></template>
  </category>

Properties

Note - You can use the properties in your patterns, topics etc. too.

Conditional Statements

There are certain factors that decide our reaction to a situation. This makes are chatbot friendlier and gives it more utility. Sometines it is also a simple logical requirement.

You may pass a list of conditions and how to react to them as follows!

 <category>
        <pattern>CAN I GET A * ON THE DICE</pattern>
        <template>
            <think><set name = "dicevalue"><star/></set></think>
            <condition name = "dicevalue">
                <li value = "One">Yes!</li>
                <li value = "Two">Yes!</li>
                <li value = "Three">Yes!</li>
                <li value = "Four">Yes!</li>
                <li value = "Five">Yes!</li>
                <li value = "Six">Yes!</li>
                <li value = "1">Yes!</li>
                <li value = "2">Yes!</li>
                <li value = "3">Yes!</li>
                <li value = "4">Yes!</li>
                <li value = "5">Yes!</li>
                <li value = "6">Yes!</li>
                <li>I don't think so.</li>
            </condition>
        </template>
    </category>

Conditional

Try Yourself - In the functionality where we reply to the user with their name, inform them if you don't already know it. (When you don't know it, you must look for value="unknown".

Topics

Sometimes we talk about or on certain topics, that is the context we're referring to. For example Countries, Trees, Names anything!

Let the following serve as an example.
We enclose our categories in the <topic> tag, which needs to be set accordingly.

<category>
        <pattern>LETS TALK ABOUT TECH</pattern>
        <template>    
            <think><set name="topic">TECH</set></think>
            Sure, I like this!
        </template>
    </category>
    
    <topic name = "tech">
        <category>
            <pattern>WHAT IS YOUR FAVOURITE</pattern>
            <template>I'm definitely into NLP! I want to talk better.</template>
        </category>
        
        <category>
            <pattern>* CNN</pattern>
            <template>I'm into bringing more perspective to vision too!</template>
        </category>
        
        <category>
            <pattern>*</pattern>
            <template>Sounds interesting!</template>
        </category>
    </topic>

Try Yourself- Build more topics, use them together and make conversations!

Formatting

  • <lowercase> makes enclosing text lowercase.
  • <uppercase> makes enclosing text uppercase.
  • <formal> makes enclosing text, such that each word's first alphabet is capitalised.
  • <explode> adds a space after each letter
 <category>
            <pattern>DISPLAY * IN LOWERCASE</pattern>
            <template>
                <lowercase><star/></lowercase>
            </template>
        </category>
    <category>
            <pattern>DISPLAY * IN UPPERCASE</pattern>
            <template>
                <uppercase><star/></uppercase>
            </template>
        </category>
        
    <category>
            <pattern>SPELL * </pattern>
            <template>
                <explode><uppercase><star/></uppercase></explode>
            </template>
        </category>
        
    <category>
            <pattern>DISPLAY * FORMALLY</pattern>
            <template>
                <formal><star/></formal>
            </template>
        </category>

Formatting-Tag

TRY YOURSELF - Display name in aforementioned category with formal tag.

Learning with the 'learn' tag

Sometimes some things are appropriate only when something has been previously spoken.
For example lets modify a previously written tag.

<category>
        <pattern>NAME A COOL *</pattern>
        <template>
            OpenGenus it is!
            <learn>
                <category>
                    <pattern>WHERE IS COOL <eval><star/></eval></pattern>
                        <template>Go to iq.opengenus.org!</template>
                </category>
            </learn>
        </template>
    </category>

<eval> tag is used to refer to the <star/> in the initial <pattern>.
When we declare the cool reference os Opengenujse, it shall be always found at iq.opengenus.org! And so is learnt by the learn tag.

LearnTag

That is the basic knowledge you need to start yourself with AIML. There are greater possibilities and opportunities to it! Take it one step at a time!

With this article at OpenGenus, you are nearly ready to create a fully functioning chatbot powered by AIML! Happy Coding!

Getting started with AIML to create Chatbots
Share this