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>
</category>

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>
</category>

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

Looking good!

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>
</category>

Save and now your bot should say something like this:

Maybe our bots can meet on the beach in Hawaii someday ;)

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 projectand 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