Pythonism

code and the oracular

How to make a chatbot in Python

with 4 comments

First stop for building a real chatbot in Python would be to use PyAIML, which can be downloaded here

http://sourceforge.net/projects/pyaiml/files/

AIML (Artificial Intelligence Markup Language) is an XML based format for encoding a chatbots “brain”. It was Developed by Richard Wallace and the resulting bot, ALICE was the best at the time.

you can also download the standard ALICE brain here

http://sourceforge.net/projects/pyaiml/files/Other%20Files/Standard%20AIML%20set/


download PyAIML, unpack it and run

$ sudo python setup.py install

in its folder. then download the Alice brain from thr second link and unpack it. The file std-startup.xml should be on python path and the other files in their folder below.

To make a quick script that will get you chatting use the following

import aiml
k = aiml.Kernel()
k.learn("std-startup.xml")
k.respond("load aiml b")
while True: print k.respond(raw_input("> "))

this is the ultimate start you off script for starting to chat with PyAIML, but I advise you to dive in and start to write your own AIML fairly soon. It is not a full programming language, but an xml dialect to specify statements and responses.

Each aiml file has a header like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<aiml version="1.0">
<meta name="author" content="luke Dunn"/>
<meta name="language" content="en"/>

Then it consists of stuff like

<category>
<pattern>hello</pattern>
<template>Hi there</template>
</category>

Each <category> element holds a pattern and a template. the pattern is the input to PyAIML, ie what the user says to the bot and the template holds the output, what the bot will say back.

with that knowledge alone you can start customising, but I’ll add a bit more. Consider ways to shorten the code. There is little difference between statements like “Hello”, “Hi”, “Howdy” in terms of their interactional meaning. So AIML has a way to map multiple inputs to the same output. Its done like this:


&lt;category&gt;
&lt;pattern&gt;HEY&lt;/pattern&gt;
&lt;template&gt;&lt;srai&gt;hello&lt;/srai&gt;&lt;/template&gt;
&lt;/category&gt;

&lt;category&gt;
&lt;pattern&gt;HI THERE&lt;/pattern&gt;
&lt;template&gt;&lt;srai&gt;hello&lt;/srai&gt;&lt;/template&gt;
&lt;/category&gt;

&lt;category&gt;
&lt;pattern&gt;HOWDY&lt;/pattern&gt;
&lt;template&gt;&lt;srai&gt;hello&lt;/srai&gt;&lt;/template&gt;
&lt;/category&gt;

&lt;category&gt;
&lt;pattern&gt;HELLO&lt;/pattern&gt;
&lt;template&gt;hello human&lt;/template&gt;
&lt;/category&gt;

the <srai> tag tells the system that “Hey”, “Hi there”,”howdy” and “hello” all mean the same thing.. simple really.

You can also use wildcards, which are designated by *, and you can group multiple differing inputs all to give the same response using the <srai>. There is also a “random.choice()” type function that can choose from a list of responses and all the above can be composed together allowing lots of different possibilities.

You can also set topics which contain different sets of answers conditional on what the topic is.

A more complete bit of documentation is here

http://www.pandorabots.com/pandora/pics/wallaceaimltutorial.html

Once you’ve started playing and grokked the concept then next check out

https://files.ifi.uzh.ch/cl/hess/classes/seminare/chatbots/style.pdf

which is an excellent book on aiml written by Wallace himself. There is so much more to learn but you’ll have hours of fun with what I’ve pointed you at here !

One other nice thing is that AIML can perform a system command where it accesses a shell. This is the starting point for crazy dreamers who might want to make a voice controlled OS frontend, perhaps using CMU Sphinx etc see my post: here

also check this:

Advertisements

Written by Luke Dunn

October 7, 2013 at 9:35 am

4 Responses

Subscribe to comments with RSS.

  1. Hi man, I have problems using Pyaiml, I get a error when i try to install it. Can you help me please. What versión of Pyaiml are you using?

    Jose Luis

    June 8, 2014 at 1:23 am

    • if you are windows omit the “sudo”… otherwise tell me your system and what the error is…

      Luke Dunn

      August 29, 2014 at 9:40 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: