[BangPypers] You <-> Eliza Google Talk program: Fun with Python, AI and google talk

Maxin B John maxin_john at yahoo.co.uk
Wed Jan 23 14:03:30 CET 2008


Hello Friends,

Do you think it is cool if you are able to handover the overhead of chatting with friends to an Artificial intelligence program called Eliza .. For those who don't know Eliza, it is a computer program by Joseph Weizenbaum which simulates a therapist. 

Now let's do that using Python. To use the Gchat which uses XMPP protocol, we need to install a python module called xmpppy . It is available at http://xmpppy.sourceforge.net/. The standard 'python setup.py install ' will perform the installation of module for us .

This program has two files
1) eliza.py 
2) eliza_googletalk.py


Now the eliza.py program which sends the chat string send by our friends to the Eliza program and returns the reply of Eliza AI program.

####################################################################
# This program will talk with eliza, the psychiatrist AI program   #
# By Maxin B. John <maxinbjohn at 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()
    reply_from_eliza= str(data.split('</strong>')[2]).split('\n')[1]
    return "%s\r\n" % (reply_from_eliza,)
    conn.close()

# invoking the eliza function to test it's functionality in pythonic way
if __name__== '__main__':
    str =eliza('I am maxin')
    print str
############################################################

Now the main part of the program ... eliza_googletalk.py


##################################################################################
#             You <-> Eliza Google chat  program             #
# What:                                         #
#                                          #
# This program will login as you in the google chat and will automagically reply #
# to those who are 'thinking' that they are chatting with you using the eliza    # 
# AI program.                                      #
# How:                                         #
#                                         #
# The google chat handling/reply part is done using the xmpppy python module     #
# Chats from your friends will be passed to the eliza program and the resulting  #
# output will be returned back to your friends as though you have typed it.      # 
# Who:                                         #
#                                          #
# Maxin B. John <maxinbjohn at gmail.com> This code is small and self explanatory   #
# So I haven't added a single comment in this program.                 #
##################################################################################

import xmpp
import time
from eliza import *

def messageCB(sess,mess):
    nick=mess.getFrom().getResource()
    text=mess.getBody()
    reply = eliza(text)
    sess.send(xmpp.Message(mess.getFrom(),reply))

def LoopyFn(conn):
    try:
        conn.Process(1)
    except KeyboardInterrupt: 
    return 0
    return 1
    
def main_process():
    jid = xmpp.protocol.JID('maxinbjohn at gmail.com')
    cl = xmpp.Client('gmail.com')
    cl.connect(('talk.google.com',5223))
    cl.RegisterHandler('message',messageCB)
    cl.auth(jid.getNode(),'my_secret_password')
    cl.sendInitPresence()
    while LoopyFn(cl): 
        pass
if __name__ == '__main__':
    main_process()

###################################################################

Run this program as 

python eliza_googletalk.py

Now let your friends to be confused :) 


       
---------------------------------
 Sent from Yahoo! &#45; a smarter inbox.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/bangpypers/attachments/20080123/6c256a2b/attachment.htm 


More information about the BangPypers mailing list