How to make a chatbot in Python

How to make a chatbot in Python


First stop for building a real chatbot in Python would be to use PyAIML, which can be downloaded here
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

download PyAIML, unpack it and run
1
$ 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
1
2
3
4
5
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
1
2
3
4
<?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
1
2
3
4
<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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
&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
Once you’ve started playing and grokked the concept then next check out
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:

Source: pythonism.wordpress

SHARE

About the Author: Roni Mondal

About Roni Mondal : Beyond blogging and digital marketing , Roni Mondal is an entrepreneur at heart who has made his hobby turned passion. Becoming a blogger, It was the most important part of his journey.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment