AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Sample AIML: Processing words of a sentence and applying a “command”
 
 

In playing around with AIML and often trying to get complicated and loop through the words of a sentence and do something like analyze each word I find myself recreating recursive loops again and again for different “commands”.  I just figured out I did not have to do that.  I made one set of categories that handles the looping and takes a command as a parameter.  Others have probably figured this out before but I thought I would share.  Please offer comments.


<category>XFOREACHWORD * DO *</pattern>
<template><think><set name=“command”><star index=“2”></set></think><srai>XFOREACHWORD2 <star></srai></template>
</category>

<category>XFOREACHWORD2 * *</pattern>
<template><srai><get name=“command”> <star></srai> <srai>XFOREACHWORD2 <star index=“2”></srai> </template>
</category>

<category>XFOREACHWORD2 *</pattern>
<template><srai><get name=“command”> <star></srai></template>
</category>
<category>XCAPITALIZE <set>preposition</set></pattern>
<template><star></template>
</category>

<category>XCAPITALIZE *</pattern>
<template><formal><star></formal></template>
</category>

<category>XBURST *</pattern>
<template>| <explode><star></explode></template>
</category>

 

 
  [ # 1 ]

Output:

(Users would not type these commands.  I am just illustrating how the commands can be tested in the training tab of Pandorabots.  They can be called from inside of other category templates using the <srai> tag.)

Human: xforeachword the united states of america do xcapitalize
sienna2: The United States of America

Human: xforeachword letters of the alphabet do xburst
sienna2: | l e t t e r s | o f | t h e | a l p h a b e t

 

 
  [ # 2 ]

More Output:

Human: xforeachword the dog jumped over the fence do xtagpartofspeech  
sienna2: the (article) dog (noun) jumped (verb) over (prep) the (article) fence (noun) 

My simple “part of speech tagger” categories are listed below.  There is no attempt at resolving words that can be multiple parts of speech;  the words are tagged with the first part of speech that matches.  The point of this post is to illustrate that the same looping logic is being reused for various simple commands.

<category><pattern>XTAGPARTOFSPEECH <set>article</set></pattern>
<
template><star/> (article) </template>
</
category>

<
category><pattern>XTAGPARTOFSPEECH <set>noun</set></pattern>
<
template><star/> (noun) </template>
</
category>

<
category><pattern>XTAGPARTOFSPEECH <set>verb</set></pattern>
<
template><star/> (verb) </template>
</
category>

<
category><pattern>XTAGPARTOFSPEECH <set>preposition</set></pattern>
<
template><star/> (prep) </template>
</
category>

<
category><pattern>XTAGPARTOFSPEECH <set>adjective</set></pattern>
<
template><star/> (adj) </template>
</
category>

<
category><pattern>XTAGPARTOFSPEECH <set>adverb</set></pattern>
<
template><star/> (adv) </template>
</
category>

<
category><pattern>XTAGPARTOFSPEECH *</pattern>
<
template><star/> (?) </template>

</
category
 

 
  login or register to react