Playing with PyAIML

Take a Python interpreter Terminal and just try to import the AIML module using the following command:

    >>> import aiml

If the module is loaded properly, the pointer you will come to the next line without getting an error. Congratulations! Your installation is correct.

Let's see how to load an AIML file using this module.

To play with this module, first we need an AIML file. Save the following content in an AIML file called sample.aiml in the home folder. You can save the file anywhere, but it should be in the same path where the Python Terminal was started.

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

After saving the AIML file, let's try to load it. The first step is to build an object of the PyAIML module called Kernel(). The object name here is bot:

    >>> bot = aiml.Kernel()

Kernel() is the main class doing the searching from the AIML knowledge base.

We can set the robot name using the following command:

    >>> bot.setBotPredicate("name", ROBIN)

The next step is to load the AIML files; we can load one or more AIML files to memory.

To learn a single AIML file, use the following command:

    >>> bot.learn('sample.aiml")

If the AIML file is correct, then you will get a message like this:

    Loading sample.aiml... done (0.02 seconds)

This means that the sample AIML file is loaded properly in memory.

We can retrieve the response from the AIML file using the following command:

    >>> print bot.respond("MY NAME IS LENTIN")
    'NICE TO SEE YOU LENTIN'

If the user input is not in the file, you will get the following message:

    'WARNING: No match found for input:'
..................Content has been hidden....................

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