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 stuffopinions.map 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>
</category>

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

--

--

Stephanie Pakrul šŸŒˆšŸ–¤šŸ¦¢

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