Wednesday, January 23, 2008

A Pythonic evening with Eliza

I have completed one of my dreams today. A day in which I will be able to integrate one of the greatest AI program that is known to the world ‘eliza’ in Python.
Now I can ‘Chat’ with Eliza using this tiny  Python program written by none other than me (Thanks to Unni for his ruby implementation of the same) Cool

####################################################################
# Copied the idea from Unni without shame :)                                                                  #
# This is just a proof of concept that anything in working in Ruby                                     #
# can be implemented in Python with elegance .. :) Python RuleZ..                                     #
# This program will talk with eliza, the psychiatrist AI program                                         #
# By Maxin B. John <maxinbjohn@gmail.com>                                                                  #
####################################################################

import httplib
import urllib

#Function that POSTS the string to eliza website and formats the reply
# my_dialog is the string that we POST to the www-ai.ijs.si website
# obtains the reply from eliza scripts and prints it on the screen

def eliza(my_dialog):
        params = urllib.urlencode({’Entry1′: my_dialog})
        headers = {”Content-type”: “application/x-www-form-urlencoded”,”Accept”: “text/plain”}
        conn = httplib.HTTPConnection(“www-ai.ijs.si”)
        conn.request(“POST”, “/eliza-cgi-bin/eliza_script”, params, headers)
        response = conn.getresponse()
        data = response.read()
        print str(data.split(’</strong>’)[2]).split(’\n’)[1]
        conn.close()

# invoking the eliza function to test it’s functionality in pythonic way
if __name__== ‘__main__’:
        eliza(’I love python’)

What else to say , Python RuleZ

Posted by maxinbjohn at 05:49:16 | Permalink | No Comments »