First steps to a chatterbot with AIML
Intro
Git-Repo with ALICE aiml and minimal python3 aiml bot (not exactly the code from this older post, but very similar)
https://github.com/datenhahn/python-aiml-chatbot .
Who isn’t impressed by all these real cool computers in the sciene fictions movies which can be controlled by voice commands, and which talk back. So am I and I thought about how cool it would be if this would be possible today. Totally aware that this is way out of my skill range I thought about an easier solution to make the computer actually talk to me. Remembering an article about chatterbots I searched a bit the net and found AIML ( http://en.wikipedia.org/wiki/AIML ) the “Artificial Intelligence Markup Language”. It’s a xml dialect for creating chatterbot databases. And luckily some programmers already put together some tools. I decided to use “PyAIML” because it seems to be really easy to use. But I can still hear no talking, you think? Just wait a bit, here it comes: espeak. It’s a speech synthesizer which reads out text you pass to it. So let’s get started!
Getting the tools
espeak
I have ubuntu linux and used apt-get to install some tools, it surely works almost the same on other systems with their package systems.
apt-get install espeak espeak-data
PyAIML
Download it here and extract to a directory
http://pyaiml.sourceforge.net/
Some Basic AIML files
Just download and extract as well. I started with the “Standard” AIML file.
http://aitools.org/Free_AIML_sets
A little chatterbot
The PyAIML Page gives pretty clear instructions how to create your own chatbot. Here is mine:
#!/usr/bin/python
import aiml
import commands
k = aiml.Kernel()
# load the aiml file
k.learn("firsttry.aiml")
# set a constant
k.setBotPredicate("name", "Chatty")
while True:
input = raw_input("> ")
response = k.respond(input)
# print out on the shell
print response
# and as speech
print commands.getoutput("/usr/bin/espeak -v en+f4 -p 99 -s 160 \"" + response + "\"")
and here the firsttry.aiml
<?xml version="1.0" encoding="utf-8"?>
<aiml version="1.0.1" xmlns="http://alicebot.org/2001/AIML-1.0.1"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1 http://aitools.org/aiml/schema/AIML.xsd">
<category>
<pattern>*</pattern>
<template>Hello, I'm glad to see you</template>
</category>
<category>
<pattern>WHAT IS YOUR NAME</pattern>
<template>My name is <bot name="name"></bot></template>
</category>
</aiml>
Invoking the bot
Now you just have to start the program
python mybot.py
And it already gives some answers.
Loading jgk.aiml... done (0.02 seconds)
> Hello
Hello, I'm glad to see you
Of course you have to add additional patterns to really let the bot talk a bit. The example files (look in the upper section of this tutorial) are a good start.
9 comments
Archived from the original WordPress blog. Comments are closed.
You can easily add a quit-functionality by checking about some keyword in the while loop. I used “quit” for example. you have to import sys, for the exit function:
It should be very easy. Just add a sleep command between the input and the output of the bot (response = …) . to make it more “human like” you could add a random time and make it wait longer if the input text was also long.
time.sleep(secs)
Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
Hi Abhimanyu,
I only played around a bit with AIML some years ago, so I don’t have too much experience, also without the file I think it is difficult to make assumptions about what is going wrong.
Maybe these links help you:
https://www.chatbots.org/ai_zone/viewthread/49/
https://www.chatbots.org/ai_zone/viewthread/479/
http://blog.pandorabots.com/aiml-use-cases-part-1/
The best would be, if you add some debug option into the bot if possible, or if that is no option you can try the first link which offers some simple debugging using a modified AIML file.
I would be happy if you share your experience, when you solved your problem.
Jonas