Building a personal chatbot: likes & dislikes using AIML sets
For a second dive into AIML, we’re going to create interaction patterns so your bot can tell chatters where you’ve travelled and some personal thoughts about those locations.
If you haven’t done so already, head over to Pandorabots and set up a free AIML-based bot on their platform. Add the Rosie bot as your base, which will assist with creating input patterns that your chatbot can recognize.
AIML set basics
Sets are one of the key ways to efficiently expand the scope and realism of your bot. They are ideal for collections of words or phrases that would have the same response. This makes them perfect for questions like, “Have you visited ________?”
We’re ultimately going to create four sets for four categories of responses: places I’ve visited and liked, places I’ve visited and didn’t like, places I’d like to travel to, and a catch-all for the remainder.
Create a visitedplaces.set file and add a list of places you’ve been.
[[“Canada”], [“Barcelona”], [“Spain”], [“the beach”], [“Vancouver”], [“New York”], [“New York City”], [“Oakland”], [“Montreal”], [“San Francisco”], [“New Orleans”]]
Scroll to the end of your personality.aiml file and add the following code:
<category><pattern>HAVE YOU VISITED <set>visitedplaces</set></pattern>
<template>I have been there and really enjoyed it!</template>
Great! The bot will look in the visitedplaces.set file after anything starting with “Have you visited”. Now let’s repeat that for another set.
Create a wishfulplaces.set file similar to visitedplaces.set with locations you’d like to visit, and add this code to your personality.aiml file:
<category><pattern>HAVE YOU ^ VISITED <set>wishfulplaces</set> ^</pattern>
<template>I haven’t been to <star index=”2"/> but I’d very much like to visit!</template>
There’s a little more going on here — we’ve got wildcards (the ^ character represents zero or more words), and we’re referencing the variable from the set in the bot’s response. The index=”2" means it’s in the second position in the pattern, between the two ^ wildcards.
Your conversation should look like this (in Pandorabots’ test platform).
Note the bot capitalized ‘Hawaii’ even though the human didn’t, because it is using the value from the set file. If you need to capitalize user input in a response, use <formal> tags around the element.
AIML and <srai>
This is great if everyone talks the same way. What about the many other forms in which this question can be asked?
We’re going to add one more major enhancement to our interaction that is a huge part of building your chatbot — redirecting equivalent patterns to the conversation. The <srai> (Symbolic Reductions AI) tag simply says: if you match this pattern, go to this other pattern.
This allows you to easily add variations, such as, “Have you been to Hawaii?” and have them redirect to our previous response template.
Add another category in your personality file:
<category><pattern>HAVE YOU BEEN TO <set>wishfulplaces</set> ^</pattern>
<template><srai>HAVE YOU ^ VISITED <set>wishfulplaces</set> <star/></srai></template>
Save and now your bot should say something like this:
Maybe our bots can meet on the beach in Hawaii someday ;)