Building a personal chatbot: AIML maps & wildcard priority

So far we’ve covered AIML template/pattern syntax, sets, basic variables, wildcards, and simple recursion. Now let’s look at implementing a map of interests and opinions.

I’m choosing to do some of my interests as a map rather than written out in AIML because there will be many topics that I don’t really need to expand upon or perform any kinds of actions on, so a simple key-value pair for the text response is adequate. They’ll be easier to update and manipulate in the future as a map file rather than in markup.

Game, Set, and Map

First we’re going to create a set with all the input variables we’ll be using in the map. I’m continuing with my content for interests and experiences, now in the music section.

Here’s my stuffopinions.set file:

[[“guitar”], [“classical”], [“country”], [“electronic”], [“metal”]]

Now create a file and add your content:

[[“classical”, “I love classical music (and sing chamber music), but I find listening to it while doing other things to be distracting.”], 
[“country”, “Sorry, not a fan.”], [“electronic”, “Love it. I listen to a lot of downtempo, house, trip hop, and dark electronica.”],
[“guitar”, “Can’t play but it’s a lovely instrument.”],
[“metal”, “\\m\/(>.<)\\m\/ (in the right mood)”]]

Here’s what it looks like on the Pandorabots platform:

And here is the AIML to reference the set and map. Note that I also have an opinion.set file that contains synonyms for “like”, to capture additional variations.

<category><pattern># DO YOU # <set>opinion</set> # <set>stuffopinions</set></pattern>
<template><map name=”stuffopinions”><star index=”5"/></map></template>

This code looks for the final word in the stuffopinions.set file, finds it in the .map file, and outputs the text that corresponds to that value.

Tip: any wildcard or set in the input pattern counts as a variable, so in this case, the <star> index for the final word is 5.

And here we go!

Remember this is a good way to do a lot of simple responses. If you want to set topics, use variables, perform recursion or random responses, etc then you’ll need to do this the usual way in AIML markup.

More <srai>

The other thing I tackled today was additional recursion and randomness in responses. Here is my AIML file for talking about some tech topics:

It’s using the same ‘opinion’ synonyms set as I mentioned before to broaden the question formats that a user may input. The <srai> tag tells a match for line 9 to substitute the text in the template below (i.e. go to line 5).

I’m also using the <random> tag, which selects a random item from the list below. On line 24, the <srai> tag is used inside the <li>. The bot will respond with either the hardcoded response on line 23, or line 24, which will jump to line 5.

You can see how this gets very powerful and helps your bot sound more natural and varied!

Wildcard priority

Here’s a diagram of how wildcard matching works in AIML. The top level will always take precedence over lower levels.

It became necessary in the above code example to use the # wildcard to override content from the default Rosie bot files. She had her own structures set up for things like “Do you like…” and “Are you a fan of…” using * wildcards that I wanted to replace with my own wording when it matched.

the project
First published in 2000, StephTheGeek is an online presence with a giant case of scope creep. StephTheBot is an attempt to bring disparate sources of personal data together in friendly, timely, chatbot form. You can read about the project and chat with her now (work in progress).