Search code examples
pythonnltkaimlpandorabots

How to use AIML with Python


I would like to integrate python scripts into my pandorabot written in aiml.

I know that you can tag aiml syntax with javascript, but I haven't found any documentation on python, except the following, which uses <oob> (out of bounds) tags, running services on the background:

<oob>
  <mrl>
    <service>python</service>
    <method>exec</method>
    <param>myfuction()<param>
  </mrl>
</oob>

<mrl>tags stands for myrobot lab, and it is part of program-ab, a java framework for actual robotics.

But I would like to use my app solely on the web...

I also came across pyAiml, but as for now I haven't see how it would help me to achieve my goal.

MY GOAL:

I want to use python because it manipulates NLTK (http://www.nltk.org/), a Natural Language Toolkit which handles huge literary corpus, and I would like to integrate this library to my bot capabilities.

lets say I have a <pattern>PYTHON</pattern>, and it would run a python script.

the script, on its turn, would import nltk (and its corpus), linking AIML patterns, or "questions" to PYTHON templates, or "answers"?

any clues on how I could achieve this? many thanks in advance.


Solution

  • While I have no experience working with python in conjunction with pandorabots, but I did work with php, and this is what I came up with conceptually. The objective was similar, but in my case I needed to add information for the pandorabot response from an external api, and the following is what I did :

    I used symbols/delimiters to
    1. Flag the response that needs to be modified.
    2. Used delimiters to fragment the response into parts that need to be modified, and parts that dont.
    3. The modifiable parts were in my case php function calls, where functions were already predefined.
    4. I then combined the response from the api with the non modified bot response and rendered it to the client.

    The end output was that I was basically able to translate an aiml response to a php call.


    Example:
    In my case I used '#" at the beginning of the response to mark the response as modifiable. I used '%' to mark the beginning and end of the segment I wanted to modify, and used ',' to separate the function call, and parameters.

    So the stored aiml response looked like :

    <template>#Response to be modified %method call,param1% continued response.</template>
    

    Algo :

     So for every response,check if it contains a # at the beginning,  
      If it does, remove the #   (for php I used substr ($response,1) )
       Extract the function call (for php I used explode($str, '%') ) 
        Process function call.
    

    I believe you can use a similar logic to extract a query and send it to nltk. Hope this helps.