Quick Links

Download

Advertisements

Randoms and Conditions Tutorial

Introduction

In this tutorial you will learn how to create and use <random> and <condition> tags in the MakeAiml language.


Random AIML Lists

My favorite AIML tag is the random tag because it is able to give a sense of life to a chatbot. Asking a bot a question and always getting one same answer can get boring, but asking it a question and consistently getting different answers or the same answer phrased in a different way can make a user feel like they are chatting with a person instead of a bot. The random tag in MakeAiml can be used by typing r, but every random tag requires a list of elements to choose at random and the random tag itself must occur within a template tag. To specify list elements, use the r tag followed by list tags (l). A list of random elements can be broken out of at anytime by inserting a template on the next line of MakeAiml. Here's an example of a 4 element random structure:

   p hello
   t 
   r 
   l Hi!  It's nice to meet you!
   l Hello, how are you today?
   l Greetings!
   l Hi, what are you up to?
               

The above code will produce an AIML tag structure that will wait for the input "Hello" and then randomly choose one of 4 responses to reply with. The AIML generated is shown below:

   <pattern> HELLO </pattern><template><random>
      <li> Hi!  It's nice to meet you! </li>
      <li> Hello, how are you today? </li>
      <li> Greetings! </li>
      <li> Hi, what are you up to? </li>
   </random></template>   
               

Conditional AIML

Sometimes a bot won't know an answer to a question or be able to respond properly to a user until an event has already occured. For example, if you were to ask a bot whether or not it liked the way you look, it would need to know your gender to be able to respond properly. If you are male, adjectives such as handsome would be best to describe you, if you are female then pretty would be a better descriptor. To make conditional responses in MakeAiml use the c tag. The condition tag is one of the few tags that has attributes that are able to be applied to it. The AIML tag syntax is <condition [name="X"] [value="Y"]>. The square brackets [ and ] indicate optional attributes that can be used to define a condition. Conditions must always occur inside a template tag. A MakeAiml example is shown below:

   p how do i look
   t I think you look 
   cnv gender male handsome
   cnv gender female pretty
   t !
               

The above example will produce the following AIML output:

   <pattern> HOW DO I LOOK </pattern>
   <template> I think you look 
      <condition name="gender" value="male"> handsome </condition>
      <condition name="gender" value="female"> pretty </condition>!
   </template>
               

The difficult thing about condition tags is that there are many different ways to write them to achieve the same effect. The above condition can also be represented using lists in the following manner:

   p how do i look
   t I think you look 
   cn gender 
   lv male handsome
   cn
   lv gender female pretty
   t !
               

The above example will produce the following AIML output:

   <pattern> HOW DO I LOOK </pattern>
   <template> I think you look 
      <condition name="gender">
         <list value="male"> handsome </li>
         <list value="female"> pretty </li>
      </condition>!
   </template>
               

It is very important to be familiar with how conditional statements can be structured in the AIML 1.0.1 standard and what the effect of each one is so that you structure the MakeAiml code in the way that will produce the results you are looking for. It is very important to realize that conditions cannot be generated by MakeAiml inside list tags due to the way MakeAiml handles the list elements.


< Prev Tutorial



About the Author

Grant Dryden works as a computer engineer. He writes software in C and C++ for embedded systems as well as firmware in VHDL. He has a Bachelor of Science in Computer Engineering.