Introduction to AIML

AIML files are a subset of Extensible Mark-up Language (XML) that can store different text patterns in the form of tags. AIML was developed by the Alicebot free software community (http://www.alicebot.org/). AIML is mainly used to implement Chatbots, a natural language software agent in which a user can ask questions to the robot and it can give an intelligent reply. This same technique is used in ChefBot. Using speech recognition, the robot gets input text from the user and an AIML interpreter; a software program that can interpret AIML files and retrieve an intelligent reply from the AIML dataset. The reply will be converted to speech. AIML files mainly consist of various tags. Here are a set of commonly used AIML tags.

Introduction to AIML tags

AIML files consist of a set of commonly used AIML tags. Let's take a look at them.

The <aiml> tag: Each AIML code begins with this tag and is closed using the </aiml> tag. This tag also consists of attributes such as the version and encoding scheme of the file. The AIML file can parse even without these attributes, but this will be useful in big projects. The version attribute corresponds to the current version of AIML that we will use. The encoding attribute is the type of character encoding we will use in this file. The following code snippet shows an example usage of AIML tags:

<aiml version="1.0.1" encoding="UTF-8">
...
</aiml>

The <category> tag: The basic knowledge blocks of AIML are called categories. Each category block consists of two sections. One is the user input in the form of a sentence and the other is a corresponding response to user input which comes from robot. The category tag is represented using the opening <category> tag and the closing tag is represented using the </category> tag. These categories must be inside the <aiml> and </aiml> tags. The category tags consist of two tags, namely, the <pattern> tag and the <template> tag. The input given by users is inside the <pattern> tag and the answers are in the <template> tag. For example, look at this following conversation:

User: How are you?

Robot: I am fine.

In this conversation, the user dialog will be in the <pattern> tag and the robot's response will be in the <template> tag. The following code shows the representation of the preceding dialogs in the AIML format:

<aiml version="1.0.1" encoding="UTF-8">
  <category>
    <pattern> HOW ARE YOU </pattern>
    <template> I AM FINE </template>
  </category>
</aiml>

We need to save this file in the .aiml or .xml format.

The <pattern> tag: The pattern tag comprises of possible user inputs. There will be only one <pattern> tag in a category block. The <pattern> tag will be the first element of the <category> tag and, in the <pattern> tag, words are separated by single spaces. The sentence in <pattern> tag may have words or wild cards such as "*" or "_", which can replace a string in this position. In the preceding example, the <pattern> HOW ARE YOU </pattern> code indicates the possible user input in this category.

The <template> tag: The <template> tag comprises of possible answers for the user input. The <template> tag will be within the <category> tag and will be placed after the <pattern> tag. The <template> tag can save a particular answer or it can trigger programs. Also, we can give conditional form of answers too. In the preceding code, the <template> tag sentence: "I AM FINE" will be the answer for the "HOW ARE YOU" pattern. We can insert additional tags in the <template> tag. The following tags are used in the <template> tag:

The <star index = "n"/> tag: This tag is used to extract a part of the user text input sentence. The n index indicates which fragment of text has to be extracted and taken from the entire sentence:

  • <star index="1"/>: This indicates the first fragment of a sentence
  • <star index="2"/>: This indicates the second fragment of a sentence

The main application of this tag is to extract and store the text from user input. The following is a dialog between the robot and the user. The wildcard can be anything, such as a name or something else. Using this tag, we can extract this wildcard portion and use it in the answering section:

User: My name is *

Robot: Nice to meet you *

So, if the user says, "My name is Lentin", then the robot will reply, "Nice to meet you Lentin". This kind of conversation is only possible using the <star> tag and wildcards such as "*". The complete AIML example using the star tag is as follows:

<aiml version="1.0.1" encoding="UTF-8">
  <category>
    <pattern> MY NAME IS * </pattern>
    <template>
      NICE TO MEET YOU <star/>
    </template>
  </category>
  <category>
    <pattern> MEET OUR GUEST * AND * </pattern>
    <template>
    NICE TO MEET YOU <star index="1"/> AND <star index="2"/>.
    </template>
  </category>
</aiml>

If we load this example to the AIML interpreter, we will get the following reply when we give the following input:

USER: MY NAME IS LENTIN

ROBOT: NICE TO MEET YOU LENTIN

The previous conversation uses one wildcard and the following conversation will use both wildcards:

USER: MEET OUR GUEST TOM AND JOSEPH

ROBOT: NICE TO MEET YOU TOM AND JOSEPH

Here, the name is "TOM", the index number is "1", and "JOSEPH" is indexed as "2".

The <srai> tag: Using the <srai> tag, we can target multiple patterns from a single <template> tag. Using the <srai> tag, the AIML interpreter can search recursively for the answer that is replacing the current template text with the template text of another pattern. The following code is an example of the usage of the <srai> tag:

<aiml version="1.0.1" encoding="UTF-8">
  <category>
    <pattern> WHAT IS A ROBOT? </pattern>
    <template>
    A ROBOT IS A MACHINE MAINLY DESIGNED FOR EXECUTING REPEATED TASK WITH SPEED AND PRECISION.
    </template>
  </category>
  <category>
    <pattern> DO YOU KNOW WHAT A * IS ? </pattern>
    <template>
      <srai> WHAT IS A <star/> </srai>
    </template>
  </category>
</aiml>

When a user asks the robot, "DO YOU KNOW WHAT A ROBOT IS", it will go to the second category template and extract the wildcard section from user input, "ROBOT" and put the complete sentence, "WHAT IS A ROBOT", and put it in the <srai> tag. The <srai> tag can call the pattern called "WHAT IS A ROBOT" and fill the output of this template to the actual template text.

For more AIML tags, you can refer to http://www.alicebot.org/documentation/aiml-reference.html.

After discussing AIML files and tags, we will discuss the Python AIML interpreter to decode AIML files. The AIML interpreter can retrieve the template text from user input. We will use a Python module called PyAIML to interpret AIML files. Let's discuss more about PyAIML.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229