AIML Tutorial: The <srai> tag

One of the most versatile tags in AIML is <srai>. In this article, we’ll take a closer look at what it is and how to use it.

What is <srai>?

Believe it or not, there is no official definition for what the acronym SRAI means. It was created as part of AIML 1.0 by Dr Richard Wallace and is usually recognised as Symbolic Reduction in Artificial Intelligence. Whatever is between the <srai> and </srai> tags is normalized and then passed back through the interpreter. This is known as recursion and continues until the chatbot reaches a final category.

This basically means that a category can call other categories to make your code easier to manage but it’s probably better to see a few examples to understand how it works.

Calling other categories

The most common use of <srai> is to deal with all the different ways that a user can say the same thing. For example, someone could say hi, howdy, yo, wassup and these all mean the same as hello.

Let’s say we had a category like this one, which remembers the user’s name when they say, “My name is xxx”.

This will work with no problem but if the user says, “I am called xxx”, “My friends call me xxx” or “xxx is my name” the pattern will not be matched. It wouldn’t be efficient to duplicate the category for all the different ways of saying, “My name is”, so we can use the <srai> tag like this:

Now if someone says, “I am called xxx”, the second category will be activated which automatically calls our main category of “My name is xxx”. This saves on a lot of duplication of code and also means that if you want to change the template in “MY NAME IS *”, you don’t have to change it in all the other categories as well.

Correcting spelling errors

Another really popular use for <srai> is to amend common spelling errors by replacing a misspelled word with the correct one like this:

If the user says, “My naem is Steve”, this category will replace naem with name to make “My name is Steve” and then pass this back through the interpreter to be dealt with by another category.

Understanding internet slang

Some younger visitors and teenagers like to use slang like lol, rofl, brb and so on. We can use <srai> to allow our chatbot to understand this, similar to how we correct spelling errors. Here are a couple of examples:

In the top category, if the user says, “I will BRB”, the interpreter will parse this as, “I will be right back”.

In a similar fashion to translating internet speak, we can also use this method to understand foreign languages:

Removing unnecessary words

Many words spoken to your chatbot are not actually needed for it to understand what the user means. If someone says, “I am very happy”, we can usually remove very to still understand the input. Similarly, if someone starts a message with erm, we can remove this too.

Here we use <srai> to clip the words very and erm from the input before passing it back through the interpreter. The <srai><star/></srai> use is extremely common and so AIML has a shortcut to this which is explained at the end of the article.

Calling functions

In AIML, we often create our own functions to perform common tasks. These may be counting how many letters in a word or doing simple math operations. We use <srai> to call these functions. Here is an example of a function called XMATHADD which adds two numbers together. We call it by passing two parameters to it (the numbers to be added)

Notice how we can use <srai> to call this function and then process the output. The conversation could go like this:

Human: What is 7 plus 2?
Bot: The answer is 9.
Human: If I have 3 apples and buy 1 more, how many will I have?
Bot: You would now have 4 apples.

Context in <that>

When working with context and <that> tags, <srai> is useful for passing messages to other categories:

In this example, if the bot asks something similar to, “Do you like pizza” and the user says, “yes”, the <srai> translates this to “I like pizza” and passes this back into the interpreter. This is far easier than coding separate categories for everything the user could possibly like.

Multiple <srai>s

We can use more than one <srai> in a category, as in this example:

Here we imagine the user is being a bit tricky and says, “My age is 14 plus 12”. First we call our XMATHADD function to get the result of 26 and then the second <srai> passes “I am 26 years old” back into the interpreter.

We can also use <srai> in categories we have already <srai>ed to, as in this example:

If the user input is “I like to eat pasta”, the first category will use <srai> and pass “My favorite food is pasta” to the second category. This in turn will pass, “I like pasta” to the generic pattern at the bottom. This is known as a recursion level of 2, as we have had to use the interpreter 2 extra times after out initial input. Now take a look at these two categories and see if you can work out what would happen if the user said, “hello”:

The first category would pass hi to the second category, which would then pass hello back to the first category. This would repeat forever causing an infinite loop and give a “Too much recursion in AIML” error. If you ever see that error, it means your coding is looping and must be amended.

<sr/>

As mentioned above, it is very common to use <srai><star/></srai> to pass input between categories, so common in fact that it has its own shortcut of <sr/>

Let’s see how to use it. Imagine the user says, “Do you like me now?” or “What are you doing now?”, the word now at the end isn’t needed and so we can trim it out using <sr/> like this:

This says that if the input ends with the word “now”, chop it from the end and process the input again. So, “ I need to leave now” becomes “I need to leave”.

Reducing long input into something more meaningful

Trimming messages is also useful when people say things like, “Excuse me, could you possibly tell me what your name happens to be please?” instead of “What is your name”. Let’s work through this example:

Original input
Excuse me, could you possibly tell me what your name happens to be please?

We trim “excuse me” and use <sr/>
could you possibly tell me what your name happens to be please?

Remove the unnecessary word possibly with <srai> as explained above
could you tell me what your name happens to be please?

Trim, “could you” from the start with <sr/>
tell me what your name happens to be please?

Trim, “please” from the end with <sr/>
tell me what your name happens to be?

Convert, “happens to be” into is
tell me what your name is?

Convert, “tell me what * is” into “What is *”
What is your name?

This is a pretty long winded example but some people like to test chatbots with nonsense like this. Using recursion, we can handle it quite easily.

As you can see <srai> is an extremely flexible and useful tag in AIML and can help you in all kinds of ways. I hope this tutorial has helped explain its use in more detail for you.

To bring the best bots to your business, check out www.pandorabots.com or contact us at info@pandorabots.com for more details.