AIML Language on Python. Making Chatbots.
Considered a form of weak AI, AIML language is a
Installation
1. Download the Py3kAiml library from:
https://github.com/huntersan9/Py3kAiml 2. Decompress the zip file and leave on a directory where it won't be moved as shown on figure 1.
3. To install open a new command line typing Ctrl+T and type the command: $ sudo python3 setup.py install |
|
4. To test if it works correctly initialize Python3 and type:
$ import aiml
$ import aiml
My First Chatbot application.
After having installed the library its time to create our first chatbot application. This chatbot will load a .XML file that contains all our .AIML file directories and will initialize an infinite loop where it'll receive user input. For every input received it will try to match the string with one of the responses available in the database (the .AIML file) and output an answer.
To stop the bot we must press CTRL + C.
To stop the bot we must press CTRL + C.
bot-startup.xml
This file will load our bot information and tell the robot what are the knowledge base it will require. In this case we only have one active, the basic_chat.aiml
<aiml version="1.0.1" encoding="UTF-8">
<!-- std-startup.xml -->
<!-- Category is an atomic AIML unit -->
<category>
<!-- Pattern to match in user input -->
<!-- If user enters "LOAD AIML B" -->
<pattern>LOAD AIML B</pattern>
<!-- Template is the response to the pattern -->
<!-- This learn an aiml file -->
<template>
<learn>corpus/basic_chat.aiml</learn>
<!-- You can add more aiml files here -->
<!--<learn>more_aiml.aiml</learn>-->
</template>
</category>
</aiml>
This file will load our bot information and tell the robot what are the knowledge base it will require. In this case we only have one active, the basic_chat.aiml
<aiml version="1.0.1" encoding="UTF-8">
<!-- std-startup.xml -->
<!-- Category is an atomic AIML unit -->
<category>
<!-- Pattern to match in user input -->
<!-- If user enters "LOAD AIML B" -->
<pattern>LOAD AIML B</pattern>
<!-- Template is the response to the pattern -->
<!-- This learn an aiml file -->
<template>
<learn>corpus/basic_chat.aiml</learn>
<!-- You can add more aiml files here -->
<!--<learn>more_aiml.aiml</learn>-->
</template>
</category>
</aiml>
basic_chat.aiml
This file contains the responses to the user based on a series of templates. In this case we only have responses to HELLO and WHAT ARE YOU.
<aiml version="1.0.1" encoding="UTF-8">
<!-- basic_chat.aiml -->
<category>
<pattern>HELLO</pattern>
<template>
Well, hello!
</template>
</category>
<category>
<pattern>WHAT ARE YOU</pattern>
<template>
I'm a bot, silly!
</template>
</category>
</aiml>
This file contains the responses to the user based on a series of templates. In this case we only have responses to HELLO and WHAT ARE YOU.
<aiml version="1.0.1" encoding="UTF-8">
<!-- basic_chat.aiml -->
<category>
<pattern>HELLO</pattern>
<template>
Well, hello!
</template>
</category>
<category>
<pattern>WHAT ARE YOU</pattern>
<template>
I'm a bot, silly!
</template>
</category>
</aiml>
ASIMOV_kb.py
This file is the Python3 application that loads the knowledge base and parses the user response. The .aiml and .xml files were stored in a folder called corpus in the application directory and are required to operate.
#Manages the knowledge base used to think.
import sys, glob
from libraries import aiml
kernel = aiml.Kernel()
kernel.learn("corpus/bot-startup.xml")
kernel.respond("LOAD AIML B")
while True: print(kernel.respond(str(input("> "))))
This file is the Python3 application that loads the knowledge base and parses the user response. The .aiml and .xml files were stored in a folder called corpus in the application directory and are required to operate.
#Manages the knowledge base used to think.
import sys, glob
from libraries import aiml
kernel = aiml.Kernel()
kernel.learn("corpus/bot-startup.xml")
kernel.respond("LOAD AIML B")
while True: print(kernel.respond(str(input("> "))))
Testing the application
Speeding up brain load
Despite how powerful the AIML language is, its slow to load when you have a huge number of files operating. In order to overcome that limitation its possible to create a brain file (.brn) that preloads the used responses and ensures they are available on the next execution without having to load the files. Here's the code necessary to create the brain module.
#AIML module for chatbot.
self.kernel = aiml.Kernel()
#Load brain file if available to speed load times.
if os.path.isfile(directory + "/bot_brain.brn"):
self.kernel.bootstrap(brainFile=directory + "/bot_brain.brn")
else:
self.kernel.bootstrap(learnFiles=directory + "/bot-startup.xml", commands="LOAD AIML B")
self.kernel.saveBrain(directory + "/bot_brain.brn")
#AIML module for chatbot.
self.kernel = aiml.Kernel()
#Load brain file if available to speed load times.
if os.path.isfile(directory + "/bot_brain.brn"):
self.kernel.bootstrap(brainFile=directory + "/bot_brain.brn")
else:
self.kernel.bootstrap(learnFiles=directory + "/bot-startup.xml", commands="LOAD AIML B")
self.kernel.saveBrain(directory + "/bot_brain.brn")
Sessions and predicates
Resources
[1] http://www.devdungeon.com/content/ai-chat-bot-python-aiml
[2] https://www.chatbots.org/ai_zone/viewthread/492/
[3] https://www.chatbots.org/ai_zone/viewforum/35/
[4] https://www.pandorabots.com/botmaster/en/tutorial
[5] https://code.google.com/archive/p/aiml-en-us-foundation-alice/downloads
[2] https://www.chatbots.org/ai_zone/viewthread/492/
[3] https://www.chatbots.org/ai_zone/viewforum/35/
[4] https://www.pandorabots.com/botmaster/en/tutorial
[5] https://code.google.com/archive/p/aiml-en-us-foundation-alice/downloads