An important part of any conversation, whether it be 2 humans or 2 bots, is knowing the context of what your conversational partner is talking about. Do you remember being at school, letting your mind wander during class and then suddenly the teacher asks you, “So, what do you think about it?” Argh! How do I answer? I wasn’t listening and don’t know what the teacher is talking about.
It’s the same for bots. They need to keep track of the conversation in order to respond correctly. Luckily, AIML offers us ways to do this quite easily.
I’m going to demonstrate this by showing you how to teach your bot to respond to “knock knock” jokes. I chose this example after seeing this article by Deborah Kay as it’s a nice simple way to introduce context.
Just in case you didn’t know, a knock knock joke is a joke which follows this sequence:
Person 1: Knock Knock
Person 2: Who’s there?
Person 1: xxxx
Person 2: xxxx who?
Person 1: (punchline which involves a play on the words xxxx)
Ken I come in!
In this sequence, the bot needs to know that you are telling a joke and it should reply with suitable responses.
Teaching the bot to respond to knock knock jokes
Let’s begin by training the bot to respond to our joke when we say, “knock knock”. We can do this by using this simple category.
So when we say “knock knock”, the bot will continue the joke and respond, “Who’s there?”. The next bit is a little trickier, as we could say anything at this point but we always want the bot to respond with the next line of the joke. However, we don’t want it to think we are telling a joke if we are talking about something different.
For this, we need to use the <that> tag, so the bot can see what it has just said.
The underscore character is a wildcard that means that the input must be at least one word. It can be as many words as you like but must be at least one word long. The <that> tag means that if the bot has just said the words Who is there, it should respond with this category. It’s set to WHO IS THERE rather than WHO’S THERE, as this is expanded automatically by the standard normal.txt file. If you are using a non standard file, you may need to amend this category.
The template contains <star/> who? This means whatever the user has said, repeat it back but add the word who? on the end to continue the joke.
For the next step, we need to accept the punchline from the user. This could be anything, so we use the underscore wildcard and <that> tag to keep context. We also need to respond to the joke with a laugh.
This is pretty similar to our previous category apart from the <that> tag now contains a wildcard, as we don’t know what the subject of the joke is going to be before it is told. This category will trigger if the bot says, “Ken who?”, “Ivor who?”, “Lucy who?” or whatever the joke is.
Using the underscore wildcard in the pattern tag means that no matter what the user says for the punchline, we will assume it’s funny and produce one of our random responses to acknowledge the joke.
That’s it. Your bot now can play along with your knock knock jokes and even laugh at the end.
Teaching the bot to tell knock knock jokes
This time we want the bot to tell us a joke, so we need to get it to say knock knock. We use wildcards to accept the user input.
Now when we say a sentence that has “knock knock” in it, the bot will begin the joke. As long as the sentence contains at least one word, “knock knock” and then at least one more word the category will be triggered. Some examples are:
Tell me a knock knock joke
Do you know any knock knock jokes?
Excuse me but would you mind telling me a knock knock joke, as I feel in the mood for a laugh
A word of warning, the underscore wildcard is very powerful and will match ANYTHING that the user says. This will cause problems if the user asks, “Do you want to hear a knock knock joke?”, as we want to tell a joke rather than hearing one but we’ll ignore the edge cases for this tutorial.
Now the bot has responded with, “knock knock”, it’s now our turn to say “Who’s there?” and the bot to continue telling the joke.
When we say, “Who’s there?”, the bot will continue the joke with one of 3 random jokes we’ve set up. The <that> tag means that this is only triggered while we are in the middle of this joke routine. If we were to say, “Who is there?” at any other time, the bot will say something like, “It’s me!” and not try to tell us a joke.
Finally, we wait for the user to continue the joke before delivering our punchline. We use <that> tags so the bot knows what joke it is saying.
An example dialogue would be:
Human: Can you tell me a knock knock joke?
Bot: Knock Knock
Human: Who’s there?
Human: Harry who?
Bot: Harry up and let me in!
These are simple examples of how to use the <that> tag to allow your chatbot to keep context. However, we can also use the <topic> tag to keep context and cut down on the number of categories we use. Here we create the entire joke routine in just three categories:
This has the advantage that you can create as many jokes as you like in the main category and you don’t need extra ones for all the different joke responses.
You can also create other ways of accepting input instead of, “Who’s there” by using <srai> tags. Using this method, you can also react when the user doesn’t say anything to continue the joke.
I hope this introduction to context using <that> and <topic> was useful to you. If you have any questions about it, just let me know in the comments below.
To bring the best bots to your business, check out www.pandorabots.com or contact us at firstname.lastname@example.org for more details.