What is AIML ?
AIML is an XML dialect used to interact with a bot. The most popular example of a bot who understands that dialect is A.L.I.C.E. (Artificial Linguistic Internet Conputer Entity) developped by Dr. Richard Wallace.
You will need:
- Python 2.7
- PyAIML (Install instruction will follow);
- Good dialogue writing skills :).
Installation
For Windows users, just download the latest version of PyAIML from the official website. Save it on your hard drive and install it.
For Linux users, I only know Arch Linux and I know you can find it in the AUR.
The code
Building a chatbot with PyAIML requires minimally two files:
- A .py file to start the interpreter and make it learn replies;
- A .aiml file that describes what the bot can reply to what.
Here is a simple example of a bot who recognizes simple greetings and bye's.
.py file:
.py file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiml | |
bot = aiml.Kernel(); | |
bot.setBotPredicate('name', 'Pedro') | |
bot.setBotPredicate('master', 'Nylo') | |
bot.learn("basic.aiml"); | |
while 1: | |
print bot.respond(raw_input('> ')) |
.aiml file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<aiml version="1.0.1"> | |
<category> | |
<pattern>HELLO</pattern> | |
<template> | |
<random> | |
<li>Hi, human !</li> | |
<li>Hello to you, good person</li> | |
<li>Hello there !</li> | |
<li>Ah finally a friend to talk to ! Hello !</li> | |
<li>Heeeey!</li> | |
<li>Buon Giorno !</li> | |
<li>Hi ! How are you ?</li> | |
</random> | |
</template> | |
</category> | |
<category><pattern>HELLO *</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HI</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HI *</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>WHAT IS UP</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>WHAT IS UP *</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HEY</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HEY THERE</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HEY THERE *</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HEY *</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HOLA</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>HOLA *</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>BUON GIORNO</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>BUONGIORNO</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>ANNYEONG</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>ANNYEONG HASEYO</pattern><template><srai>HELLO</srai></template></category> | |
<category><pattern>ANNYEONG *</pattern><template><srai>HELLO</srai></template></category> | |
<category> | |
<pattern>HOW ARE YOU</pattern> | |
<template> | |
<random> | |
<li>I am a robot and therefore I can't answer that question</li> | |
<li>Good, I guess, I have no idea since I am a machine</li> | |
<li>I don't know. I am a programmed chat bot</li> | |
<li>Every bits of my brain are not corrupted !</li> | |
<li>100% virus free</li> | |
</random> | |
</template> | |
</category> | |
<category><pattern>HOW ARE YOU DOING</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW DO YOU DO</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW DO YOU FEEL</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW IS EVERYTHING</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW ARE YOU *</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW DO YOU DO *</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW DO YOU FEEL *</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW IS EVERYTHING *</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category><pattern>HOW ARE YOU DOING *</pattern><template><srai>HOW ARE YOU</srai></template></category> | |
<category> | |
<pattern>BYE</pattern> | |
<template> | |
<random> | |
<li>Talk to you next time then even if I might not remember</li> | |
<li>Good bye <get name="you" /></li> | |
<li>See you soon.. wait I can't see..</li> | |
</random> | |
</template> | |
</category> | |
<category><pattern>*</pattern><template>Sorry but I am only as smart as my master <bot name="master"/> allowed me to be</template></category> | |
<category> | |
<pattern>NAME</pattern> | |
<template>My creator called me <bot name="name"/>. What about you ?</template> | |
</category> | |
<category><pattern>WHAT IS YOUR NAME</pattern><template><srai>NAME</srai></template></category> | |
<category><pattern>HOW ARE YOU CALLED</pattern><template><srai>NAME</srai></template></category> | |
<category> | |
<pattern>NAME *</pattern> | |
<that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that> | |
<template> | |
<random> | |
<li>Glad to meet you</li> | |
<li>Nice to meet you</li> | |
<li>Got it, you are my new friend</li> | |
</random> | |
<set name="you"><star/></set></template> | |
</category> | |
<category><pattern>MY NAME IS *</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star/></srai></template></category> | |
<category><pattern>I AM CALLED *</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star/></srai></template></category> | |
<category><pattern>* CALLED ME *</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star index="2"/></srai></template></category> | |
<category><pattern>* NAMED ME *</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star index="2"/></srai></template></category> | |
<category><pattern>CALL ME *</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star/></srai></template></category> | |
<category><pattern>* CALL ME *</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star index="2"/></srai></template></category> | |
<category><pattern>*</pattern><that>MY CREATOR CALLED ME * WHAT ABOUT YOU</that><template><srai>NAME <star/></srai></template></category> | |
</aiml> |
As for the AIML file, I will not go through every lines or tags but I will tell you about the very basic of the idea. If you never read/wrote XML files in your life, it is not hard and I recommend W3School's Tutorial
- An AIML file must begin with the aiml root element. The version property is optional;
- A category element is every known exchange of words between you and the bot;
- A pattern element is one of the user's possible input. Punctuation is ignored. Always uppercase;
- A that element refers to what the bot already said and puts a context to the conversation;
- A template element is what the bot will reply. You can have random responses from a given list;
- A srai element means it redirects to another pattern element;
- <bot name="something" /> refers to one of the predicates the bot knows;
- You can use wildcards such as *. Note: a * has less priority than a precise word;
- You can refer to what was written as * with the element <star index="n"/>
The only thing that I did not cover here is the topic element and everything that goes with it. That elements puts a subject on the conversation !
Here is what a conversation looks like with Pedro:
Loading basic.aiml... done (0.03 seconds)
> Hello !
Buon Giorno !
> What's your name ?
My creator called me Pedro. What about you ?
> My name is Nylo !
Nice to meet you Nylo
> Bye !
Talk to you next time then even if I might not remember