The Viamentis Blog

Curious About Everything

The Viamentis Blog header image 2

Some more fun with Ruby and Instant messaging

January 22nd, 2008 · Posted by unni · 2 Comments

AI used to be one of my favorite subjects right from college days. I was searching for some AI algorithms and happened to find a ‘chat-bot’ called Eliza in between. ‘She’ was good enough, even though she doesn’t perfectly simulate a human user, she does a pretty good job. The next moment i wanted to use Eliza to talk to my friends on behalf of me through my Instant messenger.

Since i already had some ruby programs which could talk to IM servers, an interface to Eliza was just what i needed. I visted the Eliza’s webpage, one of the simplest webpages ever. It just had a plain html form, with a textfield and a submit button. The page source said that textfield’s name was “Entry1″. That was all i needed !

Here’s my Ruby interface for Eliza :

def eliza(str)
response = Net::HTTP.post_form(URI.parse("http://www-ai.ijs.si/eliza-cgi-bin/eliza_script"),{'Entry1'=>str})
return response.body.split("</strong>\n").last.split("\n").first
end

Hats off to Ruby…!

Now whenever a chat message arrives on my IM program, i simply call this ‘eliza’ method, get a response string from Eliza, and send it as reply in the IM program.

Here’s the full Ruby code for the Eliza chat program.

Tags:

2 responses so far ↓

  • 1 Maxin B. John // Jan 23, 2008 at 5:50 am

    Great work Unni..
    Now got to reimplement this in Python :)

  • 2 Maxin B. John // Jan 23, 2008 at 6:38 am

    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(’‘)[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’)

Leave a Comment