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.
#based on code from https://www.packtpub.com/application-development/automate-it
# Create the kernel and learn AIML files
kernel = aiml.Kernel()
kernel.respond(“load aiml b”)
# Press CTRL-C to break this loop
print(“Welcome to geektechstuff’s chat bot”)
print(kernel.respond(input(“Type to talk >>> “)))
One thought on “Chat Bot (Python)”
Comments are closed.