source: https://pixabay.com/illustrations/chatbot-bot-assistant-support-icon-4071274/

Easy Chatbot using AIML

Creating your own chatbot from scratch ain’t so hard 😉

Pema Grg
EKbana
Published in
3 min readNov 23, 2018

--

As said in Wikipedia, “AIML [Artificial Intelligence Markup Language] is an XML dialect for creating natural language software agents.”

These days as you can see that Chatbots are in high trend when it comes to Artificial Intelligence. As most of the organizations have already started implementing chatbots on their sites especially on e-commerce websites.

Some of the best examples of chatbots are:

  1. Replika
  2. Watson Assistant
  3. Alexa
  4. Dialogflow
  5. Cleverbot

So you want to build a chatbot? No worries!

We will be using AIML because, to build a chatbot using NLP/ML/Deep Learning takes a lot of time to build while AIML helps to build a chatbot easily but the only problem is that you need to feed as many data as you can for the bot to learn and here data doesn’t just mean the questions and its category but also the question pattern.

To understand about AIML coding structure, refer to the previous article published: AIML TUTORIAL

I hope you know from the previous article what each tag denotes and how it works.

Here you will need 3 files:

1 Python file: conversation.py

2 aiml file: conversation.aiml

3 learningFileList: learningFileList.aiml

conversation.py

import aimlk = aiml.Kernel()
k.learn("learning_file_list.aiml")
k.respond("LEARN AIML")
while True:
reply = k.respond(input("User > "))
if reply:
print("bot > ", reply)
else:
print("bot > :) ", )

Note: Kernel object is the public interface to the AIML interpreter. “learn” method loads the contents of an AIML file into the kernel. While the “respond” method is used to get the response from the learned AIML file.

To make a pre-trained AIML, instead of running the AIML again do:

conversation.py

#!/usr/bin/python3
import os
import aiml
BRAIN_FILE="brain.dump"k = aiml.Kernel()if os.path.exists(BRAIN_FILE):
print("Loading from brain file: " + BRAIN_FILE)
k.loadBrain(BRAIN_FILE)
else:
k.bootstrap(learnFiles="learningFileList.aiml", commands="LEARN AIML")
print("Saving brain file: " + BRAIN_FILE)
k.saveBrain(BRAIN_FILE)
while True:
input_text = input("USER > ")
response = k.respond(input_text)
print(response)

learningFileList.aiml

<aiml version="1.0">
<category>
<pattern>LEARN AIML</pattern>
<template>
<!-- Load standard AIML set -->
<learn>conversation.aiml</learn>
</template>
</category>
</aiml>

Note: As you can notice “LEARN AIML” is the pattern that k.respond from conversation.py calls. The <learn> tag loads the AIML file to respond.

conversation.aiml

Get more data from my GitHub.

<?xml version="1.0" encoding="ISO-8859-1"?>
<aiml version="1.0">
meta name="language" content="en"/>
<category>
<pattern> HELLO * </pattern>
<template>
Hello User!
</template>
</category>
</aiml>

OUTPUT:

Loading learning_file_list.aiml…done (0.07 seconds)
Loading conversation.aiml…done (0.00 seconds)
User > Hello Bot
bot > Hello! Nice to meet you.

DEMO:

You can get the full code on GitHub.

Easy wasn’t it? Add more categories to the conversation.aiml so that your bot can answer any questions! You can also make your chatbot to any domain-specific like hotel booking, food ordering, flight booking, etc.

Try it out and let me know how it goes 😍

--

--

Writer for

curretly an NLP Engineer @EKbana(Nepal)| previously worked@Awesummly(Bangalore)| internship@Meltwater, Bangalore| Linkedin: https://www.linkedin.com/in/pemagrg/