Developing a chatbot using ALICE and AIML

To develop a chatbot, we need an AIML interpreter or reference implementation of the AIML. One such tool for this is Program AB, which can be found at https://code.google.com/archive/p/program-ab/. In the download section, the ZIP file is available for Program AB. Extract the file, which will have the following directories:

  • bots: Contains the super folder to show the name of the bot
  • data: Contains sample text
  • lib: Contains Ab.jar
  • out: Contains a class file

In the super subdirectory of the bots directory, we can see the directory names aiml, aimlf, config, data, maps, and sets. These are standard directories that are needed for creating a chatbot using AIML and ALICE. Let's test the chatbot. Open a new Terminal and move to the program-ab folder that we extracted and execute the following command:

program-ab-0.0.4.3$ java -cp lib/Ab.jar Main bot = test action=chat trace=false

It will load all the files and will present you with a prompt, as follows:

Human :

Try to chat with some text and you will soon realize that it works, but not always and not for all queries. The following is a demo chat:

Now, let's create out own chatbot for appointment scheduling. The first step is to create an AIML file.

Create the following folder structure in your new NetBeans project and add Ab.jar in your project library:

In the aiml directory, let's create an AIML file with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<aiml>
<!-- -->
<category><pattern>I WANT TO BOOK AN APPOINTMENT</pattern>
<template>Are you sure you want to book an appointment</template>
</category>
<category><pattern>YES</pattern><that>ARE YOU SURE YOU WANT TO BOOK AN APPOINTMENT</that>
<template>Can you tell me date and time</template>
</category>
<category><pattern>NO</pattern><that>ARE YOU SURE YOU WANT TO BOOK AN APPOINTMENT</that>
<template>No Worries.</template>
</category>
<category><pattern>DATE * TIME *</pattern><that>CAN YOU TELL ME DATE AND TIME</that>
<template>You want appointment on <set name="udate"><star index="1"/> </set> and time <set name="utime"><star index="2"/></set>. Should i confirm.</template>
</category>
<category><pattern>YES</pattern><that>SHOULD I CONFIRM</that>
<template><get name="username"/>, your appointment is confirmed for <get name="udate"/> : <get name="utime"/></template>
</category>
<category><pattern>I AM *</pattern>
<template>Hello <set name="username"> <star/>! </set></template>
</category>
<category><pattern>BYE</pattern>
<template>Bye <get name="username"/> Thanks for the conversation!</template>
</category>
</aiml>

Let's explore the AIML file. Using the set and get tags, the context can be saved in variables and retrieved when required:

<category><pattern>I AM *</pattern>
<template>Hello <set name="username"> <star/>! </set></template>
</category>

This shows the use of the set property, so when the user inputs "I am ashish", it is saved in the variable name and the response is "Hello Ashish !". Now, this can be used anywhere in AIML by using get to print the username. So, this means that using the set and get tag context can be maintained.

The next part is to create an appointment. When the user asks for an appointment, the response will ask for confirmation, as follows:

<category><pattern>I WANT TO BOOK AN APPOINTMENT</pattern>
<template>Are you sure you want to book an appointment</template>
</category>

Now, the expected request from the user will be yes or no, according to which the next response is generated. To continue the conversation in context with the last question, the tag is used, as follows:

<category><pattern>YES</pattern><that>ARE YOU SURE YOU WANT TO BOOK AN APPOINTMENT</that>
<template>Can you tell me date and time</template>
</category>
<category><pattern>NO</pattern><that>ARE YOU SURE YOU WANT TO BOOK AN APPOINTMENT</that>
<template>No Worries.</template>
</category>

If the user says "YES", the chatbot will ask for the date and time, which again is saved, and confirmation is asked as to whether the user wants to book an appointment on the stated date and time, as follows:

<category><pattern>DATE * TIME *</pattern><that>CAN YOU TELL ME DATE AND TIME</that>
<template>You want appointment on <set name="udate"><star index="1"/> </set> and time <set name="utime"><star index="2"/></set>. Should i confirm.</template>
</category>
<category><pattern>YES</pattern><that>SHOULD I CONFIRM</that>
<template><get name="username"/>, your appointment is confirmed for <get name="udate"/> : <get name="utime"/></template>
</category>

A sample chat output is as follows:

Robot : Hello, I am your appointment scheduler May i know your name
Human :
I am ashish
Robot : Hello ashish!
Human :
I want to book an appointment
Robot : Are you sure you want to book an appointment
Human :
yes
Robot : Can you tell me date and time
Human :
Date 24/06/2018 time 4 pm
Robot : You want appointment on 24/06/2018 and time 4 pm. Should i confirm.
Human :
yes
Robot : ashish!, your appointment is confirmed for 24/06/2018 : 4 pm

Save this AIML file as myaiml.aiml in the aiml directory. The next step is to create the AIML intermediate format CSV files. Create a Java file named GenerateAIML.java and add the following code:

public class GenerateAIML {

private static final boolean TRACE_MODE = false;
static String botName = "appointment";

public static void main(String[] args) {
try {

String resourcesPath = getResourcesPath();
System.out.println(resourcesPath);
MagicBooleans.trace_mode = TRACE_MODE;
Bot bot = new Bot("appointment", resourcesPath);

bot.writeAIMLFiles();

} catch (Exception e) {
e.printStackTrace();
}
}

private static String getResourcesPath(){
File currDir = new File(".");
String path = currDir .getAbsolutePath();
path = path.substring(0, path.length()-2);
System.out.println(path);
String resourcePath = path + File.separator + "src/chapter12/mybot";
return resourcePath;
}
}

Execute this file. It will generate myaiml.aiml.csv in the aimlif directory.

Change the ResourcePath variable according to your package in NetBeans. In this case, chapter12 is the package name, and mybot is the directory inside the package.

Create another Java file to test the bot, follows:

public class Mychatbotdemo {
private static final boolean TRACE_MODE = false;
static String botName = "appointment";
private static String getResourcePath(){
File currDir = new File(".");
String path = currDir .getAbsolutePath();
path = path.substring(0, path.length()-2);
System.out.println(path);
String resourcePath = path + File.separator + "src/chapter12/mybot";
return resourcePath;
}
public static void main(String args[]){
try
{
String resourcePath = getResourcePath();
System.out.println(resourcePath);
MagicBooleans.trace_mode = TRACE_MODE;
Bot bot = new Bot(botName, resourcePath);
Chat chatSession = new Chat(bot);
bot.brain.nodeStats();
String textLine = "";
System.out.println("Robot : Hello, I am your appointment scheduler May i know your name");
while(true){

System.out.println("Human : ");
textLine = IOUtils.readInputTextLine();
if ((textLine==null) || (textLine.length()<1)){
textLine = MagicStrings.null_input;
}
if(textLine.equals("q")){
System.exit(0);
} else if (textLine.equals("wq")){
bot.writeQuit();
} else {
String request = textLine;
if(MagicBooleans.trace_mode)
System.out.println("STATE=" + request + ":THAT" + ((History)chatSession.thatHistory.get(0)).get(0) + ": Topic" + chatSession.predicates.get("topic"));
String response = chatSession.multisentenceRespond(request);
while(response.contains("&lt;"))
response = response.replace("&lt;", "<");
while(response.contains("&gt;"))
response = response.replace("&gt;", ">");
System.out.println("Robot : " + response);
}
}
}
catch(Exception e){
e.printStackTrace();
}

}
}

Execute the Java code and you will see the prompt saying Human:, and it will wait for an input. Pressing Q will end the program. As per our AIML file, our dialogue is limited as we have only asked for basic information. We can integrate it with the super folder and add our AIML file in the super directory so that we can use all the available conversations by default and our custom conversation for appointments.

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

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