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