Wildcards in AIML

In this blog, I’ll discuss the various wildcards in AIML . This will give you a solid introduction to using them in your own code.

What is a wildcard?

The Ultimate Default Category (UDC) is used by the bot to provide an answer if no other suitable category can be matched.

<category>
<pattern>*</pattern>
<template>I have no answer for that.</template>
</category>

We use an asterisk * in the pattern to capture the user's input. This symbol in AIML is known as a wildcard.

Wildcards are used to capture many inputs using only a single category. There are a number of different wildcards that can be used in AIML. Please note that wildcards apply only to words and not characters within a word!

The * Wildcard

The * The * symbol is able to capture one (1) or more words in the user input

<pattern>HELLO *</pattern>

This pattern would match all of the following inputs:

Hello there!
Hello Daniel.
Hello my good friend.
Hello, anyone there?

But not the word “Hello” by itself, because there must be at least one word captured by the * wildcard to form a match.

The ^ Wildcard

The ^ symbol is also a wildcard, however, it can capture 0 or more words.

<pattern>HELLO ^</pattern>

This pattern would match all of the following inputs:

Hello.
Hello there!
Hello Daniel.
Hello my good friend.
Hello, anyone there?

What if both patterns exist? Which one will form a match?

HELLO *

HELLO ^

Wildcards are ranked in order of priority, so that certain patterns will take precedence over others.

The ^ has higher priority, so if the input is "Hello there", the 2nd pattern would be matched.

Exact Matches

If a pattern forms an exact match with the input, that category will take priority over any containing the ^ or * wildcards that it could potentially match.

HELLO THERE

HELLO ^

HELLO *

If the input is “Hello there”, the first pattern will match before the other wildcard patterns.

A Quick Word About <set>

Let’s assume we have two patterns as below:

<pattern>1000</pattern>
:
<pattern><set>number</set></pattern>

If a pattern contains an exact match, this will be triggered as a higher priority than if the match appears in a set. This may cause issues if you are checking for items in a set and I’ll post a blog demonstrating how we can write some AIML to get around this.

The _ and # Wildcards

There are two other wildcards, _ and #. These wildcards take the highest priority when matching. Even if the input forms an exact match with a pattern, the match can be overridden by a pattern containing one of these wildcards.

HELLO _

HELLO THERE

If the input is “Hello there”, the first pattern will form a match.

_ is a "1 or more" wildcard (like *)
# is a "0 or more" wildcard (like ^)

Be very careful when using these wildcards because they will override all other patterns, including exact match!

Matching Priorities

This example shows the matching priority for all four wildcards, along with a pattern that contains an exact match and also a set. For this example, we assume the words “HELLO THERE” appear in a set called greetings.

HELLO #
HELLO _
HELLO THERE
<set>greetings</set>
HELLO ^
HELLO *

Highest Priority Matching

Sometimes there is an exact match that we would like to take highest priority, overriding the _ and # wildcards.
We can use the $ sign to signify that a pattern will be matched first given a particular word.

<pattern>$WHO IS MIKE</pattern>
:
<pattern>_ MIKE</pattern>

The first pattern matches “Who is Mike?” while the second pattern matches all other inputs ending with “Mike”.

Note, that the $ symbol is not a wildcard. It is a marker that says “for this particular word(s) (e.g WHO), override the category that would have otherwise been matched.

Visualizing Matching Priority

All of the bot’s AIML categories are loaded into a structure called the “Graphmaster.” The order in which patterns take matching priority can be visualized in the Graphmaster:

Multiple Wildcards

You can have more than one wildcard per pattern. You can echo multiple wildcards in your pattern by using <star index="x"/>, where x corresponds to the index number (position in the sentence) of the wildcard. If you don;t include an index, the interpreter will assume you mean the first wildcard. For example:

<category>
<pattern>MY NAME IS * AND I AM * YEARS OLD</pattern>
<template>Hi <star/>. I am also <star index="2"/> years old!</template>
</category>

As <star index="1"/> is commonly used, we can shorten this to <star/>to save time. Also, instead of writing <srai><star/></srai>, we can shorten this to just <sr/>, as this is also commonly used. We can also reference wildcards in <that>tags in a similar manner by using<thatstar index="x"/> like below:

<category>
<pattern>WHY</pattern>
<that>I LIKE *</that>
<template><srai>Why do you like <thatstar index="1"/>
</template>
</category>

This covers conversations like:

Bot: I like computers.
Human: Why?
Bot: Because I am a computer myself.

Using wildcards will save you a ton of time when writing categories and is certainly something I would recommend mastering.

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