topical media & game development

talk show tell print

sample-js-eliza-bot-plain.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>&nbsp;</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="&nbsp;Talk&nbsp;"> <INPUT TYPE="reset" VALUE="Reset" onClick="window.setTimeout('elizaReset()',100)"></TD>
  </TR>
  </FORM>
  </TABLE>
  </CENTER>
  
  <P>&nbsp;</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>&nbsp;</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
  &quot;talk&quot; 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 &quot;I went for a long boat ride&quot; and he responded &quot;Tell
  me about boats&quot;, 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">&nbsp;</TD></TR>
  <TR><TD COLSPAN="2">
          <H3>About elizabot.js</H3>
          <P>&quot;elizabot.js&quot; is an object oriented JavaScript library for [multiple] instances of the Eliza program.</P>
          <PRE STYLE="line-height: 120%;">Synopsis:
  
           new ElizaBot( <I>&lt;random-choice-disable-flag&gt;</I> )
           ElizaBot.prototype.transform( <I>&lt;inputstring&gt;</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: &quot;elizadata.js&quot;):
  
           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:
         
           [&quot;&lt;key&gt;&quot;, &lt;rank&gt;,
             [
               [ <SPAN STYLE="color: #303030">// first decomposition pattern</SPAN>
                 &quot;&lt;decomp&gt;&quot;,
                 [&quot;&lt;reasmb&gt;&quot;, &quot;&lt;reasmb&gt;&quot;, &quot;&lt;reasmb&gt;&quot;]
               ],
               [ <SPAN STYLE="color: #303030">// second decomposition pattern</SPAN>
                 &quot;&lt;decomp&gt;&quot;,
                 [&quot;&lt;reasmb&gt;&quot;, &quot;&lt;reasmb&gt;&quot;, &quot;&lt;reasmb&gt;&quot;]
               ]
             ]
           ]
  
           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 &quot;xnone&quot; holds the rules for default phrases (no match).
           
           decomposition and reasembly syntax follow the &quot;canonical&quot; form:
  
           decomposition:
             * ... any words (incl. none)
              ... (in first position) reassemble for memory only
             @synon ... substitute entry with synononym expression
  
           reassembly:
             (n) ... insert param of position n (first is &quot;1&quot;)
                     positions are any matchings of &quot;*&quot; or &quot;@synon&quot;
  
           pres, posts, synonyms, keywords, decompositions all in lower case.
           all definitions are optional but at least elizaKeywords should be supplied.
           if no keywords are found `transform()' returns the default string:
           &quot;I am at a loss for words.&quot;.
  
  Note:   &quot;elizaPostTransforms&quot; is not a standard ELIZA feature and was included to provide
           a smoothing mechanism for any productions of a bot-to-bot conversation.
  
  </PRE>
          <P>Data representations and syntax follow the &quot;canonical&quot; form.<BR>
          (Since all data is transformed to and matched as regular expressions, you could also use regexps in the keywords and decompositions. Do not use any &quot;*&quot; expressions in decomposition patterns as &quot;*&quot; will be transformed to a regexp-pattern itself.)<BR>
          The structure of &quot;elizaKeywords&quot; follows the internal data model as described by J. Weizenbaum in his article <A HREF="#ELIZA">[1]</A>.</P>
   
          <P>&nbsp;<BR>
          &quot;elizabot.js&quot; by &copy; Norbert Landsteiner 2005;<BR>
          mass:werk &#150; media environments &lt;<A HREF="http://www.masswerk.at/" TARGET="_blank">http://www.masswerk.at>&gt;.</P>
          <P>Distribution:<BR>
          &quot;elizabot.js&quot; is free software and provided &quot;as is&quot;.
          It 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.</P>
      <P>Compatibility:<BR>
      &quot;elizabot.js&quot; should be compatible to most browsers with support of RegExp (that is all standard browsers).<BR>
     (Version 1.1 doesn't require the support of lambda functions in RegExps any more.)</P>
          
  </TD></TR>
  <TR><TD COLSPAN="2">&nbsp;</TD></TR>
  <TR><TD COLSPAN="2"><B>Download</B>: <A HREF="elizabot.zip">elizabot.zip</A> &nbsp;<SMALL>(ZIP file, 15 KB incl. documentation)</SMALL></TD></TR>
  <TR><TD COLSPAN="2">&nbsp;</TD></TR>
  <TR><TD COLSPAN="2">References:</TD></TR>
  <TR VALIGN="top">
  <TD><A NAME="ELIZA"></A>[1]</TD>
  <TD>
      Weizenbaum, Joseph &quot;ELIZA &#150; A Computer Program For the Study of Natural Language
      Communication Between Man and Machine&quot;<BR>
      in: Communications of the ACM; Volume 9 , Issue 1 (January 1966): p 36-45.
  </TD>
  </TR>
  <TR><TD COLSPAN="2">&nbsp;</TD></TR>
  <TR><TD COLSPAN="2"><SMALL>&gt; <A HREF="#top">top of page</A></SMALL></TD></TR>
  <TR><TD COLSPAN="2">&nbsp;</TD></TR>
  <TR><TD COLSPAN="2" STYLE="font-family: arial,helvetica,sans-serif; font-size: 12px;">N. Landsteiner 2005; &lt;<A HREF="http://www.masswerk.at/" TARGET="_blank">http://www.masswerk.at>&gt;</SMALL></TD></TR>
  <TR><TD COLSPAN="2">&nbsp;</TD></TR>
  </TABLE>
  
  </BODY>
  </HTML>
  


(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.