Monday, April 25, 2005

Lizzy

I've been chatting for hours with ELIZA lately. ELIZA as most of you know is a very old program that attempts to mimick Artificial Intelligence. Although she wouldn't exactly pass the Turing Test, she is nevertheless a very interesting specimen. She uses a seemingly simple set of rules to generate responses to what you say. This would have seemed very impressive in the 60s when ELIZA made her first appearance, but its all dated now and is just a curiosity, a very addictive one at that, for us geeks spending Saturday nights in front of the konsole.

Go on, speak to ELIZA and see for yourself. I know I did.

ELIZA: Hello. I am ELIZA. How can I help you?
MARDUK: Do you like broccoli?
ELIZA: Oh, i like to eat broccoli.
MARDUK: You're strange
ELIZA: Does it please you to believe I am strange?
MARDUK: Not quite
ELIZA: Please go on.
MARDUK: Certainly
ELIZA: You are sure?
MARDUK: Of course.
ELIZA: Please go on.
MARDUK: What's the context again?
ELIZA: Please go on.
MARDUK: Now you're just saying that
ELIZA: How long have you been just saying that?
MARDUK: Do you have any idea what you're saying?
ELIZA: Oh, i have any idea what youre saying.
MARDUK: That's just wrong
ELIZA: Please go on.
MARDUK: Now you're PISSING me off
ELIZA: How long have you been pissing you off?
MARDUK: Don't you tempt me you silly, brain-dead CGI script
ELIZA: Oh, i tempt you i silly braindead cgi script.
MARDUK: Urgh
ELIZA: Please go on.
MARDUK: You're annoying
ELIZA: How long have you been annoying?


WTF! How the HELL did she know that?


MARDUK: I love ducks
ELIZA: Can you elaborate on that?
MARDUK: Not really
ELIZA: Please go on.
MARDUK: I said no
ELIZA: Can you elaborate on that?
MARDUK: Nope, Nei, Non?
ELIZA: Please go on.
MARDUK: You're doing it again
ELIZA: How long have you been doing it again?


<GASP>


MARDUK: No, no, YOU are doing it again
ELIZA: You are being a bit negative.


She is a bit infuriating sometimes.


MARDUK: Do you like ducks?
ELIZA: Oh, i like ducks.
MARDUK: But then you also like broccoli
ELIZA: Oh, i also like broccoli.
MARDUK: Yeah, you said that before
ELIZA: Oh, i said that before.


<uncomfortable silence>


MARDUK: You need help
ELIZA: Oh, i need help.
MARDUK: Do you think i need help?
ELIZA: Oh, i think you need help.

There you have it folks. You heard it here first.

I vowed I wouldn't peanut over this, but once again I succumbed to my greatest weakness. In a fit of obsession, I wrote a Swing frontend to ELIZA. Yahoo briefcase died on me a couple of times for no apparent reason, and I can't be bothered with anything else, so for all the geeks out there, I will inline it with this post. You know what to do. Yank, p, javac baby.

<snip>



/*
* Lizzy
* Copyright (C) 2005 Marduk the Creator
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Lizzy implements KeyListener {
private static final String ELIZA_URL = "http://www-ai.ijs.si/cgi-bin/eliza/eliza_script";
private static final String FORM_FIELD = "Entry1";

private JTextArea chatWindow = null;
private JTextField message = null;

private Lizzy() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("ELIZA");
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

chatWindow = new JTextArea();
chatWindow.setEditable(false);
chatWindow.setLineWrap(true);
chatWindow.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(""),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
chatWindow.setText("[ELIZA] Hello. I am ELIZA. How can I help you?\n\n");

JScrollPane scrollPane = new JScrollPane(chatWindow);
message = new JTextField();
message.addKeyListener(this);
message.setFocusable(true);
message.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Ask Eliza"),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));

frame.add(scrollPane, BorderLayout.CENTER);
frame.add(message, BorderLayout.SOUTH);
frame.setVisible(true);
}

public static void main(String[] args) {
new Lizzy();
}

public void keyTyped(KeyEvent event) {
}

public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_ENTER) {
if (! message.getText().trim().equals(""))
chatWindow.setText(chatWindow.getText().concat("-- " +
message.getText()).concat(new String("\n\n")));
chatWindow.setText(chatWindow.getText().concat("[ELIZA] ") +
elizaSays(message.getText()).concat(new String("\n\n")));
chatWindow.repaint();
message.setText(null);
}
}

public void keyReleased(KeyEvent event) {
}

public String elizaSays(String babble) {
String response = new String();

try {
boolean started = false;

URL url = new URL(ELIZA_URL);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

DataOutputStream dataOut = new DataOutputStream(urlConnection.getOutputStream());
String content = FORM_FIELD + "=" + URLEncoder.encode(babble, "UTF-8");
dataOut.writeBytes(content);
dataOut.flush();
dataOut.close();

DataInputStream dataInput = new DataInputStream(urlConnection.getInputStream());
String tmpString = new String();

while ((response = dataInput.readLine()) != null) {
if (! started) {
if (response.startsWith("<strong>Eliza:</strong>")) {
started = true;
}
continue;
}

if (! response.startsWith("<form")) {
tmpString = tmpString.concat(response);
break;
}

response = tmpString;
}
dataInput.close();
} catch (Exception e) {
response = "ERROR: " + e.getMessage();
System.out.println(e.getCause());
}

return response;
}
}


<End snip>

| indent

0 Comments:

Post a Comment

<< Home