[alicebot-style] Multiple choice quiz in AIML

Kim Sullivan alicebot-style@list.alicebot.org
Mon, 11 Jul 2005 16:01:30 +0200


This is a multi-part message in MIME format.

------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


> I know you are thinking, why use AIML for this task?  BASIC would be
> better.  Let's just say it's a challenge.  My code works alright in
> isolation, but loaded together with the rest of the AAA set it runs into
> problems if the client answers something like "B. Virginia" and there is
> another pattern somewhere "B *".

The code is escaped by a topic. Are you using the interface/function code
also? The "main event handler" assures that there is always the correct
topic set (using an underscore), so if your interpreter is working
correctly, there is no way another pattern that begins with B can be
triggered.

For clarity, I've attached all files that are needed for the multiple choice
question code to work. The only thing that needs customizing is the CLOZE
INIT category in cloze.aiml, and of course the questions in the Qx.aiml
files. Of course, the pattern "RESPONSE B" is already escaped, you MUST
leave the word "RESPONSE" intact. If you want to capture things like "B
Virginia", the pattern has to look like this: "RESPONSE B *".

The presented AIML is almost watertight (i.e. due to the fact that a
"function" is used, it's near impossible to "break out" of the test loop),
and it presents the questions in a random order (each time, the order is
different!).

Kim

------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="interface.aiml"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="interface.aiml"

<?xml version=3D"1.0" encoding=3D"ISO-8859-1"?>

<aiml version=3D"1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name=3D"author" content=3D"Dirk Scheuring"/>
<meta name=3D"author" content=3D"Kim Sullivan"/>
<meta name=3D"language" content=3D"en"/>


<!-- set up a function call -->
<category>
<pattern>FUNCTION *</pattern>
<template>
<think>
<set name=3D"oldtopic"><get name=3D"topic"/></set>
<set name=3D"topic">INTERFACE</set>
<set name=3D"function"><star/></set>
</think>
</template>
</category>

<!-- the main 'event handler' -->
<topic name=3D"INTERFACE">

  <category>
  <pattern>_</pattern>
  <template>
  <think>
  <set name=3D"topic">FUNCTION <get name=3D"function"/></set>
  </think>
  <srai>RESPONSE <star/></srai>
  </template>
  </category>

</topic>

<!-- miscellaneous routines -->
<topic name=3D"FUNCTION *">

  <!-- End a function call -->
  <category>
  <pattern>END FUNCTION</pattern>
  <template>
  <think>
  <set name=3D"topic"><get name=3D"oldtopic"/></set>
  <set name=3D"function"/>
  <set name=3D"oldtopic"/>
  </think>
  </template>
  </category>
=20
  <!-- A good bot should reprompt if the response was unknown -->
  <category>
  <pattern>RESPONSE *</pattern>
  <template>
  AIML Exception on function <get name=3D"function"/>: unhandled =
response.
  <srai>END FUNCTION</srai>
  </template>
  </category>

  <!-- If for some weird reason the interface was exited, return to it =
ASAP -->
  <category>
  <pattern>*</pattern>
  <template>
  <think><set name=3D"topic">INTERFACE</set></think>
  </template>
  </category>

  <!-- After an unknown response/repromt -->
  <category>
  <pattern>LOOP</pattern>
  <template>
  <think><set name=3D"topic">INTERFACE</set></think>
  </template>
  </category>

</topic>

</aiml>

------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="shuffle.aiml"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="shuffle.aiml"

<?xml version=3D"1.0" encoding=3D"ISO-8859-1"?>

<aiml version=3D"1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name=3D"author" content=3D"Kim Sullivan"/>
<meta name=3D"language" content=3D"en"/>


<category>
<pattern>MULTI SHUFFLE *</pattern>
<template>
<srai>SHUFFLE <srai>SHUFFLE <srai>SHUFFLE <srai>SHUFFLE <star =
index=3D"1"/></srai></srai></srai></srai>
</template>
</category>


<category>
<pattern>SHUFFLE * *</pattern>
<template>
<random>
<li><star index=3D"1"/> <srai>SHUFFLE <star index=3D"2"/></srai></li>
<li><star index=3D"2"/> <srai>SHUFFLE <star index=3D"1"/></srai></li>
</random>
</template>
</category>

<category>
<pattern>SHUFFLE *</pattern>
<template>
<star/>
</template>
</category>

</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="cloze.aiml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="cloze.aiml"

<?xml version="1.0" encoding="ISO-8859-1"?>

<aiml version="1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Kim Sullivan"/>
<meta name="language" content="en"/>

<category>
<pattern>CLOZE INIT</pattern>
<template>
<think>
<set name="QUESTIONS"><srai>MULTI SHUFFLE Q1 Q2 Q3 Q4</srai> QEND</set>
</think>
<srai>FIRST QUESTION</srai>
</template>
</category>

<category>
<pattern>FIRST QUESTION</pattern>
<template>
<srai>FUNCTION <srai>STACK QUESTIONS POP</srai></srai>
<srai>SAYQUESTION</srai>
</template>
</category>


<topic name="FUNCTION *">

<category>
<pattern>NEXT QUESTION</pattern>
<template>
<srai>END FUNCTION</srai>
<srai>FUNCTION <srai>STACK QUESTIONS POP</srai></srai>
<srai>SAYQUESTION</srai>
</template>
</category>

</topic>

<topic name="FUNCTION QEND">

<category>
<pattern>RESPONSE SAYQUESTION</pattern>
<template>
This was the last question of the test.
<srai>END FUNCTION</srai>
</template>
</category>
</topic>


</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="q1.aiml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="q1.aiml"

<?xml version="1.0" encoding="ISO-8859-1"?>

<aiml version="1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Kim Sullivan"/>
<meta name="language" content="en"/>

<topic name="FUNCTION Q1">

<category>
<pattern>RESPONSE SAYQUESTION</pattern>
<template>
What letter comes first in the alphabet?<br/>
A: b <br/>
B: c <br/>
C: a <br/>
<srai>LOOP</srai>
</template>
</category>

<category>
<pattern>RESPONSE A</pattern>
<template>
Wrong, the right answer was C.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE B</pattern>
<template>
Wrong, the right answer was C.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE C</pattern>
<template>
Of course it's A.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>


<category>
<pattern>RESPONSE *</pattern>
<template>
Please respond only A, B or C. <br/>
<srai>SAYQUESTION</srai>
</template>
</category>
</topic>
</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="q2.aiml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="q2.aiml"

<?xml version="1.0" encoding="ISO-8859-1"?>

<aiml version="1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Kim Sullivan"/>
<meta name="language" content="en"/>

<topic name="FUNCTION Q2">

<category>
<pattern>RESPONSE SAYQUESTION</pattern>
<template>
Who is the president of the U.S.A?<br/>
A: Bill Clinton <br/>
B: George Bush <br/>
C: Richard Nixon <br/>
<srai>LOOP</srai>
</template>
</category>

<category>
<pattern>RESPONSE A</pattern>
<template>
Wrong, the right answer was B.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE B</pattern>
<template>
That was easy, wasn't it?<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE C</pattern>
<template>
Nope, it's Bill Clinton.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>


<category>
<pattern>RESPONSE *</pattern>
<template>
Please respond only A, B or C. <br/>
<srai>SAYQUESTION</srai>
</template>
</category>
</topic>
</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="q3.aiml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="q3.aiml"

<?xml version="1.0" encoding="ISO-8859-1"?>

<aiml version="1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Kim Sullivan"/>
<meta name="language" content="en"/>

<topic name="FUNCTION Q3">

<category>
<pattern>RESPONSE SAYQUESTION</pattern>
<template>
Is the whale a fish?<br/>
<srai>LOOP</srai>
</template>
</category>

<category>
<pattern>RESPONSE YES</pattern>
<template>
Sorry, it's a mammal.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE NO</pattern>
<template>
That is correct, the whale is a mammal.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE *</pattern>
<template>
Please respond only with "yes" or "no". <br/>
<srai>SAYQUESTION</srai>
</template>
</category>
</topic>
</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="q4.aiml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="q4.aiml"

<?xml version="1.0" encoding="ISO-8859-1"?>

<aiml version="1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Kim Sullivan"/>
<meta name="language" content="en"/>

<topic name="FUNCTION Q4">

<category>
<pattern>RESPONSE SAYQUESTION</pattern>
<template>
Who wrote this AIML example?<br/>
A: Kim Sullivan <br/>
B: Thomas Ringate <br/>
C: Richard Wallace <br/>
<srai>LOOP</srai>
</template>
</category>

<category>
<pattern>RESPONSE A</pattern>
<template>
Yes!<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE B</pattern>
<template>
Sorry, the right answer was A.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>

<category>
<pattern>RESPONSE C</pattern>
<template>
No, it was written by Kim.<br/>
<srai>NEXT QUESTION</srai>
</template>
</category>


<category>
<pattern>RESPONSE *</pattern>
<template>
Please respond only A, B or C. <br/>
<srai>SAYQUESTION</srai>
</template>
</category>
</topic>
</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070
Content-Type: application/octet-stream;
	name="questionstack.aiml"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="questionstack.aiml"

<?xml version="1.0" encoding="ISO-8859-1"?>

<aiml version="1.0">

<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Kim Sullivan"/>
<meta name="language" content="en"/>

<category>
<pattern>STACK QUESTIONS PRINT</pattern>
<template>
<get name="QUESTIONS"/>
</template>
</category>

<category>
<pattern>STACK QUESTIONS CLEAR</pattern>
<template>
<set name="QUESTIONS"/>
</template>
</category>

<category>
<pattern>STACK QUESTIONS INIT *</pattern>
<template>
<set name="QUESTIONS"><star/></set>
</template>
</category>


<category>
<pattern>STACK QUESTIONS PUSH *</pattern>
<template>
<think>
<set name="QUESTIONS"><star/> <get name="QUESTIONS"/></set>
</think>
</template>
</category>

<category>
<pattern>STACK QUESTIONS POP *</pattern>
<template>
<star/>
<think><set name="QUESTIONS"/></think>
</template>
</category>

<category>
<pattern>STACK QUESTIONS POP * *</pattern>
<template>
<star index="1"/>
<think><set name="QUESTIONS"><star index="2"/></set></think>
</template>
</category>

<category>
<pattern>STACK QUESTIONS POP</pattern>
<template>

<srai>STACK QUESTIONS POP <get name="QUESTIONS"/></srai>

</template>
</category>


</aiml>
------=_NextPart_000_0020_01C58631.CD2AF070--