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 were 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:

Now create a file and add your content:

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.

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).

Talk to me about mental health, nerdy things, entrepreneurship, sex work, polyamory, web dev, & life in a hippie hacker dorm in San Francisco.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store