topical media & game development
sample-js-eliza-bot.htm / htm
<HTML>
<HEAD>
<TITLE>Eliza (elizabot.js)</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="lib-present-script-elizabot-elizabot.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="lib-present-script-elizabot-elizadata.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
var eliza = new ElizaBot();
var elizaLines = new Array();
var displayCols = 60;
var displayRows = 20;
function elizaReset() {
eliza.reset();
elizaLines.length = 0;
elizaStep();
}
function elizaStep() {
var f = document.forms.e_form;
var userinput = f.e_input.value;
if (eliza.quit) {
f.e_input.value = '';
if (confirm("This session is over.\nStart over?")) elizaReset();
f.e_input.focus();
return;
}
else if (userinput != '') {
var usr = 'YOU: ' + userinput;
var rpl ='ELIZA: ' + eliza.transform(userinput);
elizaLines.push(usr);
elizaLines.push(rpl);
// display nicely
// (fit to textarea with last line free - reserved for extra line caused by word wrap)
var temp = new Array();
var l = 0;
for (var i=elizaLines.length-1; i>=0; i--) {
l += 1 + Math.floor(elizaLines[i].length/displayCols);
if (l >= displayRows) break
else temp.push(elizaLines[i]);
}
elizaLines = temp.reverse();
f.e_display.value = elizaLines.join('\n');
}
else if (elizaLines.length == 0) {
// no input and no saved lines -> output initial
var initial = 'ELIZA: ' + eliza.getInitial();
elizaLines.push(initial);
f.e_display.value = initial + '\n';
}
f.e_input.value = '';
f.e_input.focus();
}
//-->
</SCRIPT>
</HEAD>
<BODY TOPMARGIN="0" LEFTMARGIN="0" RIGHTMARGIN="0" BOTTOMMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0" STYLE="border:0" onload="window.setTimeout('elizaReset()',100)"><A NAME="top"></A>
<CENTER>
<P> </P>
<!--
<H3>Eliza</H3>
-->
<TABLE BORDER="0" CELLSPACING="10" CELLPADDING="0">
<FORM NAME="e_form" onsubmit="elizaStep();return false">
<TR><TD COLSPAN="2"><TEXTAREA NAME="e_display" COLS="60" ROWS="20"></TEXTAREA></TD></TR>
<TR VALIGN="middle">
<TD><INPUT TYPE="text" NAME="e_input" SIZE="50"></TD>
<TD ALIGN="right"><INPUT TYPE="submit" VALUE=" Talk "> <INPUT TYPE="reset" VALUE="Reset" onClick="window.setTimeout('elizaReset()',100)"></TD>
</TR>
</FORM>
</TABLE>
</CENTER>
<P> </P>
<P ALIGN="center">For a comfortable terminal-like interface see the <A HREF="http://www.masswerk.at/elizabot/eliza.html">ELIZA-Terminal</A>.</P>
<P> </P>
<TABLE BORDER="0" CELLSPACING="12" CELLPADDDING="0">
<TR><TD COLSPAN="2">
ELIZA is a natural language conversation program described by Joseph Weizenbaum in January 1966 <A HREF="#ELIZA">[1]</A>.<BR>
It features the dialog between a human user and a computer program representing a mock Rogerian psychotherapist.<BR>
The original program was implemented on the IBM 7094 of the Project MAC time-sharing system at MIT and was written in MAD-SLIP.<BR><BR>
This is how Joseph Weizenbaum discussed his choice for a conversation model as it would be found in psychotherapist's session:
<BLOCKQUOTE>
At this writing, the only serious ELIZA scripts which exist are some which cause
ELIZA to respond roughly as would certain psychotherapists (Rogerians). ELIZA
performs best when its human correspondent is initially instructed to
"talk" to it, via the typewriter of course, just as one would to a
psychiatrist. This mode of conversation was chosen because the psychiatric
interview is one of the few examples of categorized dyadic natural language
communication in which one of the participating pair is free to assume the pose
of knowing almost nothing of the real world. If, for example, one were to tell a
psychiatrist "I went for a long boat ride" and he responded "Tell
me about boats", one would not assume that he knew nothing about boats, but
that he had some purpose in so directing the subsequent conversation. It is
important to note that this assumption is one made by the speaker. Whether it is
realistic or not is an altogether separate question. In any case, it has a
crucial psychological utility in that it serves the speaker to maintain his
sense of being heard and understood. The speaker furher defends his impression
(which even in real life may be illusory) by attributing to his conversational
partner all sorts of background knowledge, insights and reasoning ability. But
again, these are the <em>speaker's</em> contribution to the conversation.
</BLOCKQUOTE>
For a conversation example given by Joseph Weizenbaum in his article see the <A HREF="eliza_test.html">Eliza Test</A> page.
</TD></TR>
<TR><TD COLSPAN="2"> </TD></TR>
<TR><TD COLSPAN="2">
<H3>About elizabot.js</H3>
<P>"elizabot.js" is an object oriented JavaScript library for [multiple] instances of the Eliza program.</P>
<PRE STYLE="line-height: 120%;">Synopsis:
new ElizaBot( <I><random-choice-disable-flag></I> )
ElizaBot.prototype.transform( <I><inputstring></I> )
ElizaBot.prototype.getInitial()
ElizaBot.prototype.getFinal()
ElizaBot.prototype.reset()
Usage:
var eliza = new ElizaBot();
var initial = eliza.getInitial();
var reply = eliza.transform(inputstring);
if (eliza.quit) {
<SPAN STYLE="color: #303030">// last user input was a quit phrase</SPAN>
}
<SPAN STYLE="color: #303030">// method `transform()' returns a final phrase in case of a quit phrase
// but you can also get a final phrase with:</SPAN>
var final = eliza.getFinal();
<SPAN STYLE="color: #303030">// other methods: reset memory and internal state</SPAN>
eliza.reset();
<SPAN STYLE="color: #303030">// to set the internal memory size override property `memSize':</SPAN>
eliza.memSize = 100; <SPAN STYLE="color: #303030">// (default: 20)</SPAN>
<SPAN STYLE="color: #303030">// to reproduce the example conversation given by J. Weizenbaum
// initialize with the optional random-choice-disable flag</SPAN>
var originalEliza = new ElizaBot(true);
</PRE>
<P>As ElizaBot is a totally self-contained object and instances use their own internal memory it's possible to have multiple instances of the ElizaBot object talking to each other.</P>
<P>ElizaBot is also a general chatbot engine that can be supplied with any rule set.</P>
<PRE STYLE="line-height: 120%;">
Data Structures (cf: "elizadata.js"):
elizaInitials ......... Array of initial phrases
elizaFinals .......... Array of final phrases
elizaQuits ............ Array of quit phrases
elizaPres ............. Array of alternating name value pairs for preprocessing
elizaPosts ............ Array of alternating name value pairs for postprocessing
elizaSynons ........... Object of words and their synonyms (as array)
elizaPostTransforms ... regexp/replacement pairs to be performed as final cleanings
elizaKeywords ......... Array of keywords with decompositions and reasemblies
elizaKeywords elements are of:
["<key>", <rank>,
[
[ <SPAN STYLE="color: #303030">// first decomposition pattern</SPAN>
"<decomp>",
["<reasmb>", "<reasmb>", "<reasmb>"]
],
[ <SPAN STYLE="color: #303030">// second decomposition pattern</SPAN>
"<decomp>",
["<reasmb>", "<reasmb>", "<reasmb>"]
]
]
]
keywords with higher rank take precedence.
decompositions are matched in definition order.
reasemblies are chosen by random or cycled through if the no-random flag ist set.
the special keyword "xnone" holds the rules for default phrases (no match).
decomposition and reasembly syntax follow the "canonical" form:
decomposition:
* ... any words (incl. none)
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.