Thursday, July 17, 2008

Saying a list of AIML responses in order

Here is a good question that came in about AIML style:

Question:
I use the <random> tag in AIML when I have a list of responses. But instead of saying the responses randomly, is there any way I can get the bot to say them in a specific order? For example, I have four responses for one input pattern. Can I get the bot to say them one by one each time that pattern is activated?

Answer:
There is no specific tag for that in AIML, but it is not too hard to implement using existing AIML tags. In this example, the input xxx results in "Response One" the first time it is entered, "Response Two" the second time, "Response Three" the third time, then "Response Four", then cycles back to "Response One."

The program uses the AIML variable "state" to keep track of which response is next. Some categories are provided to implement a "successor" function to advance the state variable through I, II, III, IV and back to I.


<aiml version="1.0">


<category>
<pattern>xxx</pattern>
<template>

<think>
<set name="state">
<srai>SUCCESSOR <get name="state"/></srai>
</set>
</think>
<srai>say response <get name="state"/></srai>

</template>
</category>

<category><pattern>SUCCESSOR</pattern>
<template>I</template>
</category>
<category><pattern>SUCCESSOR *</pattern>
<template>I</template>
</category>
<category><pattern>SUCCESSOR I</pattern>
<template>II</template>
</category>
<category><pattern>SUCCESSOR II</pattern>
<template>III</template>
</category>
<category><pattern>SUCCESSOR III</pattern>
<template>IV</template>
</category>
<category><pattern>SUCCESSOR IV</pattern>
<template>I</template>
</category>

<category>
<pattern>SAY RESPONSE I</pattern>
<template>Response One.</template>
</category>

<category>
<pattern>SAY RESPONSE II</pattern>
<template>Response Two.</template>
</category>

<category>
<pattern>SAY RESPONSE III</pattern>
<template>Response Three.</template>
</category>

<category>
<pattern>SAY RESPONSE IV</pattern>
<template>Response Four.</template>
</category>

<category>
<pattern>SAY RESPONSE *</pattern>
<template>Undefined.</template>
</category>






</aiml>

No comments:

Post a Comment

 

blogger templates | Make Money Online