AIML TUTORIAL

Pema Grg
4 min readNov 23, 2018

--

AIML stands for Artificial Intelligence Modelling Language. AIML is an XML based markup language meant to create artificial intelligence applications. Here’s the list of AIML tags:

AIML TAGS

To start with the aiml, first of all, declare the starting tag i.e:

<?xml version=”1.0" encoding=”ISO-8859–1"?>

<aiml>

<aiml> tag is the parent tag, we need to close the aiml tag </aiml> as well at the end of the file.

<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
.......
</aiml>

<category>

Within the category tag, there should be two sets of tags, the “matching” part of the expression and the “returning” part of the expression. Generally, these are represented by the <pattern> and <template> tags respectively.

<category>
<pattern> HELLO</pattern> "Matching part"

<template> "Returning part"
Hello
</template>

</category>

<pattern>

The pattern is matched within the <pattern> tags and the response that is given back is in the <template> tag.

The * represents a “wildcard match” that matches one or more words in the <pattern> tag. And _ (underscore) represents wildcard matching, but with more precedence than other matches.

Like the *, the _ also represents wildcard matching, but with more precedence than other matches.

<category>
<pattern> HELLO *</pattern> "Matching part"

<template> "Returning part"
Hello
</template>

</category>

<template>

When the <pattern> tag within the <category> is matched, template helps to get the response.

<category>
<pattern> HELLO </pattern> "Matching part"

<template> "Returning part"
Hello
</template>

</category>

<star/>

Used when embedded in a <srai> tag, this matches whatever information was generalized with the * (or the underscore)in the pattern tag.

<category>
<pattern>DO YOU KNOW WHO * IS?</pattern>

<template>
<srai>WHO IS <star/></srai>
</template>

</category>

<srai>

This tag allows for the same question to be “passed around” to different parts of the program. Using srai, we can return a simple response when the user types a specific keyword, say, School, no matter where “school” is present in the sentence.

"CREATE A CATEGORY"
<category>
<pattern>SCHOOL</pattern>
<template>School is an important institution in a child's life.</template>
</category>
"CREATE GENERIC CATEGORIES"
<category>
<pattern>_ SCHOOL</pattern>
<template>
<srai>SCHOOL</srai>
</template>
</category>

<category>
<pattern>_ SCHOOL</pattern>
<template>
<srai>SCHOOL</srai>
</template>
</category>

<category>
<pattern>SCHOOL *</pattern>
<template>
<srai>SCHOOL</srai>
</template>
</category>

<category>
<pattern>_ SCHOOL *</pattern>
<template>
<srai>SCHOOL</srai>
</template>
</category>

<random>

This tag selects a random element from the list of <li>s that appear within it.

<category>
<pattern>HI</pattern>

<template>
<random>
<li> Hello! </li>
<li> Hi! Nice to meet you! </li>
</random>
</template>

<category>
OUTPUT:
Human: Hi
Robot: Hi! Nice to meet you!
Human: Hi
Robot: Hello!

<li>

Specifies a list item. Used within the <random> tag for random responses from the given list.

<category>
<pattern>ONCE I *</pattern>
<template>
<random>
<li>Go on.</li>
<li>Can you 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>What is it?</li>
</random>
</template>
</category>

<set>

Sets whatever is within the tags to the variable VALUE. Can be retrieved through the use of <get> tag

<category>
<pattern>MY NAME IS *</pattern>
<template>
<set name="name"><star/></set> is a nice name.
</template>
</category>

<get>

Outputs whatever is in the variable VALUE. If VALUE has not been <set>, defaults to “”.

<category>
<pattern>WHAT IS MY NAME</pattern>
<template>
Your name is <get name="name"/>.
</template>
</category>

<that>

<that> Tag is used in AIML to respond based on the context.

<category>
<pattern>WHAT ABOUT MOVIES</pattern>
<template>Do you like comedy movies</template>
</category>

<category>
<pattern>YES</pattern>
<that>Do you like comedy movies</that>
<template>Nice, I like comedy movies too.</template>
</category>

<category>
<pattern>NO</pattern>
<that>Do you like comedy movies</that>
<template>Ok! But I like comedy movies.</template>
</category>
Output:Human: What about movies?
Robot: Do you like comedy movies?
Human: No
Robot: Ok! But I like comedy movies.

<topic>

The tag is used in AIML to store a context so that later conversation can be done based on that context. It helps AIML to search categories written within the context of the topic.

<category>
<pattern>LET DISCUSS MOVIES</pattern>
<template>Yes <set name = "topic">movies</set></template>
</category>

<topic name = "movies">
<category>
<pattern> * </pattern>
<template>Watching good movie refreshes our minds.</template>
</category>

<category>
<pattern> I LIKE WATCHING COMEDY! </pattern>
<template>I like comedy movies too.</template>
</category>

</topic>
OUTPUT:
Human: let discuss movies
Robot: Yes movies
Human: Comedy movies are nice to watch
Robot: Watching good movie refreshes our minds.
Human: I like watching comedy
Robot: I too like watching comedy.

<think>

This tag essentially makes bot perform whatever is between the tags but not output anything to the user. Most useful when combined with <set> or <get>.

<category>
<pattern>My name is *</pattern>
<template>
Hello!<think><set name = "username"> <star/></set></think>
</template>
</category>

<category>
<pattern>Byeee</pattern>
<template>
Hi <get name = "username"/> Thanks for the conversation!
</template>
</category>
OUTPUT:
Human: My name is Pema
Robot: Hello!
Human: Byeee
Robot: Hi Pema Thanks for the conversation!

<! — …… ->

The comment tag, often used for commenting any set of text.

Ref:

  1. http://annabot.sourceforge.net/aiml.html
  2. https://www.tutorialspoint.com/aiml/

Note: To make a chat using python, refer to the tutorial: “The easiest way to create a Chatbot using AIML”

--

--

Pema Grg

curretly an NLP Engineer @EKbana(Nepal)| previously worked@Awesummly(Bangalore)| internship@Meltwater, Bangalore| Linkedin: https://www.linkedin.com/in/pemagrg/