First steps to a chatterbot with AIML

Intro

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.

pixelstats trackingpixel

Leave a comment

Your comment

Security Code: