Chat Bot (Python)

A while back I started looking at creating a chat bot in node.js (see here) but got distracted with having fun in Python. Today I picked up the Humble Bundle Dev Ops bundle. Humble Bundle is a great site that produces bundles of books / games / videos for very prices with donations going to various charities – I highly recommend the site to anyone looking for a great deal. Anyways, back to the Dev Ops bundle; I picked it up and it contains a book called “Automate it!” (which can also be purchased via the publisher’s website – https://www.packtpub.com/application-development/automate-it ) which caught my eye as it is about automating tasks using Python (which regular readers will now is my current favourite language to play with).

Flicking through the contents of the book and there is a section on chat bots, with I decided to give a shot. However, I hit a few snags as the code didn’t work – could be it was written for Python2 but after a few minor adjustments it does now work. The print command should have been print(), and raw_input() should have been input().

I’ve uploaded the working files to Github at https://github.com/geektechdude/python_chat_bot and I’m going to look at seeing if the AIML file can be made easier (currently I have entries for hello and hi, wondering if I could create a list of all greetings so that the bot knows they are all greetings), and expand with some more Python commands (e.g. typing “bye” or “good bye” to close the program cleanly).

Note: make sure the xml and aiml files are in the same directory as the py file in order for the bot to work correctly.

—–

#!/bin/python3
#geektechstuff
#based on code from https://www.packtpub.com/application-development/automate-it

 

import aiml

# Create the kernel and learn AIML files
kernel = aiml.Kernel()
kernel.learn(“startup.xml”)
kernel.respond(“load aiml b”)

# Press CTRL-C to break this loop
print(“”)
print(“Welcome to geektechstuff’s chat bot”)
print(“”)
while True:
print(kernel.respond(input(“Type to talk >>> “)))

One thought on “Chat Bot (Python)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.