AIML Masterclass: Comparing Two Variables

Steve Worswick
Jul 26, 2018 · 6 min read

One of the things that is often useful to do in a chatbot is to compare two variables to see if they contain the same value. For example, we may want to check whether the user’s name is the same as the chatbot’s name and produce a suitable “that’s a coincidence” comment. In an ideal world we could do this:

Unfortunately, this is not correct AIML and will produce an error, as we cannot directly compare the contents of two variables like this. However, all is not lost and we can code a utility in AIML that will allow us to do this. For this, we need the <learn> tag and so before starting, ensure your interpreter supports this tag. Pandorabots and Program AB do. I believe Program O does as well but please check first if you use a different interpreter.

I’ve coded a utility called BOTCOMPARE X Y which compares the values of X and Y and sets a new variable called MATCH with the result. MATCH can have one of 3 values:

— X and Y are the same
— X and Y are different
— Something went wrong (incorrect number of parameters passed)

We can now amend our category like this:

You can also say DEMOCOMPARE to see a demo of the utility. The results of the demo will be something like this:

The full AIML that does this can be downloaded here.

The rest of this article will explain how the utility is coded to compare the two variables. The following explanation assumes you have a basic knowledge of the AIML chatbot language but feel free to just grab the file and use it in your own chatbots free of charge if you like. You don’t need to know how it works in order to use it, just as long as you get the syntax correct, which is BOTCOMPARE X Y — Where X and Y are the two variables you want to check. Also, check the section near the bottom of this article for advanced use.

The principle behind it is pretty straightforward:
1 — Make a temporary category called “BOTCHECK + the first parameter”
2 — It then <SRAI>s “BOTCHECK + the second parameter”
3 — If category “BOTCHECK *” is called then the two are different.
4 — If category “BOTCHECK + first parameter” is called then the two are the same.
5 — Finally, we reset the temporary category in case we need to check for another match.

So in plain English, let’s say we called “BOTCOMPARE Apple Orange”, the chatbot creates a new category called “BOTCHECK Apple”. The bot then tries to find “BOTCHECK Orange”. This doesn’t hit our new category and so the match is set to NO.

However, if we were to say, “BOTCOMPARE Orange Orange”, the chatbot creates a new category called “BOTCHECK Orange”. The bot then looks for “BOTCHECK Orange”. This time, our new category is called and so the match is set to YES.

With it so far? ;)

It’s usually easier to understand if we examine the AIML in more detail. Feel free to grab the file from here and follow along.

The first part of the file is the licence agreement which we will skip over but you are welcome to include this utility in your own chatbots free of charge.
We then have a few categories to make the demo before we get to the main part of the utility here:

This sets up two variables called comparevalue1 and comparevalue2. Each of these variables contains one of the parameters we passed to the utility. Once they are set up, we can move on to checking if they contain the same information. This is done in the main category of the utility:

This part creates a new category containing a pattern of BOTCHECK “comparevalue1” and a template which sets the “match” variable to YES. So for example, if we had said “BOTCOMPARE Apple Orange”, this would set up:

For those of you not familiar with<learn>, it’s really worth examining what it can do, as it can make your chatbot a whole lot smarter by creating categories on the fly. The <eval>tag means evaluate what is between the tags, so we set up a pattern of instead of

Once this category has been created by the chatbot, we can try <srai>ing to it with the value of the variable “comparevalue2” to see if it exists. The AIML file already contains this category:

So in our example of BOTCOMPARE APPLE ORANGE, the bot will try to call BOTCOMPARE ORANGE. We don’t have a category for this and so BOTCOMPARE * will be matched instead. This sets “match” to NO indicating they are not the same. However, if we try BOTCOMPARE APPLE APPLE, the category BOTCOMPARE APPLE will be matched, which sets “match” to YES. The two are the same.

Now we have our answer, we need to remove the category we just created. Why? Because if we now do BOTCOMPARE GRAPE APPLE, the category BOTCHECK APPLE already exists and so “match” will be incorrectly set to YES. This is done by the last part of the main category which<srai>s BOTREMOVECOMPARISON:

We can’t “unlearn” categories so we have to create a new category to overwrite our existing BOTCOMPARE APPLE category. This is almost exactly the same as our original category apart from we set the “match” to NO.

Now when we do BOTCOMPARE GRAPE APPLE, the value of “match” will be correctly set to NO.

To make it more user friendly, we end the AIML with some error messages to send back to the user if they use the utility incorrectly. In each case, the variable “match” will be set to FAIL. There are three error situations:

No parameters passed to BOTCOMPARE
Just one parameter passed
More than two parameters passed

In each situation, the error message will be displayed along with how the utility should be used.

This is great for comparing variables with single words but what if we want to compare multiple words like “Homer Simpson” and “Bart Simpson”? If we use “BOTCOMPARE Homer Simpson Bart Simpson”, the utility will throw an error, as it thinks we are trying to compare four different things. To get around this, I’ve included an extra category:

Now if we use the separator of XSPLIT, we can call this instead:

which will show the category where each of the values are.

You may have noticed as well that I’ve included categories to correct common errors like “BOT COMPARE” instead of “BOTCOMPARE” and also a fun category:

This allows the user to ask things like “Is a red dog equal to a blue dog?” and may be useful in bot conversations. It can also be used internally by the chatbot to check two variables.

That concludes the explanation of the BOTCOMPARE utility. It’s really useful for things like games or to validate user input and I hope you find it of use for your chatbots. If you have any questions about it, feel free to get in touch.

To bring the best bots to your business, check out or contact us at for more details.


The leading platform for building and deploying chatbots.

Steve Worswick

Written by

Mitsuku's creator and developer. Mitsuku is a 4 times winner of the Loebner Prize and regarded as the world's most humanlike conversational AI


The leading platform for building and deploying chatbots.