1

I am creating a chatbot using Java and program ab. In few places I can’t answer the questions directly, I have to process something or call a web service and process the result and then reply back. In such cases how to include the result of my java function to the responses in the aiml.

Say,

User: What is the price of the product A?
Bot: The price of product A is $50 

In the above example, $50 is not going to be same always. I have to take that in run time. So how to solve this problem?

**AIML:**

<category>
    <pattern>WHAT IS THE PRICE OF THE *</pattern>
    <template>The price of <star/> is $<call some function price(productA)> 
    </template>
</category>

**JAVA:**

public int price(String product){
   // gets the product price
   // do the conversion 
   // apply discount
   return price;
}

Please someone help me. Thanks in advance.

Kavipriya
  • 441
  • 4
  • 17

2 Answers2

2

Typically AIML extensions are implemented as an extension tag. So you wouldn't call a programming language method/function directly from AIML script. In the AB documentation you can find more details about implementing this kind of functionality here. Below is the relevant text with an updated link to PCAIMLProcessorExtension found in a forked project on GitHub. There a couple of practical examples on of working extensions can be found.

AIMLProcessorExtension

Program AB defines a Java Interface called AIMLProcessorExtension that you can use to define new AIML tags.

A class implementing AIMLProcessorExtension must provide:

  • a Set of tag names.
  • a function to recursively evaluate the XML parse tree for each node associated with a new tag.

The Program AB source includes a sample implementation of this interface called PCAIMLProcessorExtension, which defines a collection of tags simulating a contacts database.

A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
  • Thanks! But is there any tutorial or reference material or any documentation on how to use AIMLProcessorExtension? I couldn't find anything useful. – Kavipriya Apr 17 '17 at 10:14
  • Updated the answer with a link to the example extension you seek. – A. Kootstra Apr 17 '17 at 10:45
  • Thanks! can you also please look at this question? http://stackoverflow.com/questions/43463390/aimlprocessorextension-tag-not-working-in-aiml – Kavipriya Apr 18 '17 at 04:09
2

There is a simple and general option, you can keep a keyword to be used in switch later on, e.g.

AIML template will have a keyword for operation,

<category>
   <pattern>WHAT IS THE PRICE OF THE *</pattern>
   <template>PRICE,The price of <star/> is,<star/> </template>

And update java code like:

String response  = aimlResponse(request);
String [] responseComponents = reponse.parse(",");
String method = responseComponents[0];

//Then use switch, also apply size check on array after parsing in case of response with No keywords

Switch method:
{
case PRICE:
//here add the price to response string
String price = price(responseComponents[2]);
response = responseComponents[1]+ price;
break;
}
  • What if there are two or more components in the response on which the function to be called depends? something like : Is the room X door open? here room makes sense as function related to room has to be called but X also makes sense because which room is it to check for is important too. – Dev_Man Jun 11 '17 at 03:49
  • Just append all required elements separated by a comma and in the program you can parse them. Like in the above example, we know for this response, responseComponents[0] is method, you can store custom element at [1], in this case the room number. – Hamza Idrees Jun 11 '17 at 09:00