AIML Masterclass: Comparing Two Objects
A popular way to test bots is to ask them questions like “Harry is taller than Dave. Who is the tallest?”. In this tutorial, I’ll explain how I allow Mitsuku to work with objects and comparisons.
Warning: You will need to have a pretty good grasp of AIML to follow this tutorial but I’ve made an example of the source code here (learn.aiml).
Setting up the different comparisons
First, we need to make a list of the different kinds of comparisons people are going to ask about. Things like taller, faster, younger, quicker, slower etc. For this tutorial, I’m just going to concentrate on “x is taller than y.” but it can easily be adapted for other comparisons.
We’ll make a set called comparisons containing the different types. Here is an example but you can probably think of extra ones to add.
For each of these, we need a category like below.
The different compare predicates are to allow us to be able to ask questions like, “Who is the tallest?”, “Is Harry taller than Dave?”, “Who is shorter than Dave?” and so on. Most categories will just contain 4 compare predicates (the 2 opposites) like below but this method allows for different ways of asking questions.
We also need a category to reset the comparisons after each query or the bot will get confused with old information.
Finally, we need to teach the bot how to learn new pieces of information. So we create a category like this, which we can call to set up new categories automatically using the <learn> tag.
This works by passing 2 parameters to a category called SetUpNewCategory. The first parameter is the pattern, the second one is the template. We use the word XSPLIT to act as a splitter between the 2 parts, as it is unlikely someone is going to say XSPLIT to your bot. The bot then sets up a new category using the <learn> tag.
The main category
Now we have everything set up, we can accept the user’s input. The first part of our category looks like this
If the user says “x is y than z” (Harry is taller than Dave), where y is one of our comparisons, the bot will match this input and say, “Ok. I will remember that.” to the user. It now needs to create new categories to handle any subsequent questions about this.
The first step is to blank out any comparisons from previous interactions by calling the ResetComparisons category we created earlier. The bot now needs to call SetupComparisons find the different compare predicates to be able to set up new categories. Finally in this stage, it sets up predicates called object1 and object2 which are the left and right parts of the user input.
In our example of Harry is taller than Dave, we now have the following predicates set up and can begin to set up the new categories we require in order to answer the user’s queries:
A simple example
Given the input of “Harry is taller than Dave”, let’s teach the bot how to answer, “Is Harry taller than Dave?”
To do this we need to pass 2 parameters (a pattern and a template) to SetUpNewCategory
The pattern will be:
And the template will be something like:
So the complete <srai> will be:
This will now automatically set up a new category which will allow the bot to answer our question.
We now need to set up similar <srai>s for questions like, “Is Dave taller than Harry?”, “Is Harry shorter than Dave?”, “Is Dave shorter than Harry?” and so on to make categories like:
So to answer “Is Dave taller than Harry?”, we would set up this <srai>
Which will create this category:
This method will answer the user’s questions but involves a lot of <srai>ing. Once you feel confident with the technique above, we can make a few shortcuts to make the process more universal.
Instead of asking about Harry and Dave, let’s assume the user said, “Mount Everest is taller than the Eiffel Tower.”. In this case, they would then ask, “Which is the taller?” or “What is the taller?” rather than “Who is the taller?”.
We could create a load more <srai>s for this but let’s use an easier method and set up a category called LearnNewQuestions which can do this for us. Here is a sample of it:
And we call it using:
It looks complicated but all it’s doing is passing a parameter to the start of the pattern and then creating suitable categories. To demonstrate, let’s take LearnNewQuestions WHO
The first 2 <srai>s expand to:
<srai>SetUpNewCategory WHO _ taller # XSPLIT Harry is taller</srai>
<srai>SetUpNewCategory WHO _ tallest # XSPLIT Harry is tallest</srai>
Passing other question starters (Who, What, Where, Which) to LearnNewQuestions allows us to set up all the different categories we need to answer questions.
For this tutorial, I’ve just made a few examples but you can expand these to questions like “Who is taller than Harry?” or even “Between Dave and Harry, which one is the shortest?”. As this is a key component of how Mitsuku works, I hope you will forgive me for not sharing the full AIML file. However, a sample of it is available along with some of my other AIML files here. It’s called learn.aiml
In a future tutorial, I’ll show you how to expand these queries to allow your bot to be able to handle questions with extra objects like, “Harry is taller than Dave but shorter than Pete. Who is shorter than Harry?” but for now, I hope this is useful in your bots.
To bring the best bots to your business, check out www.pandorabots.com or contact us at email@example.com for more details.