topical media & game development 
  
  
    
    
  
 lib-present-script-elizabot-elizabot.js / js
  /*
    elizabot.js v.1.1 - ELIZA JS library (N.Landsteiner 2005)
    Eliza is a mock Rogerian psychotherapist.
    Original program by Joseph Weizenbaum in MAD-SLIP for "Project MAC" at MIT.
    cf: Weizenbaum, Joseph "ELIZA - A Computer Program For the Study of Natural Language
        Communication Between Man and Machine"
        in: Communications of the ACM; Volume 9 , Issue 1 (January 1966): p 36-45.
    JavaScript implementation by Norbert Landsteiner 2005; <http://www.masserk.at>
  
    synopsis:
  
           new ElizaBot( <random-choice-disable-flag> )
           ElizaBot.prototype.transform( <inputstring> )
           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) {
               // last user input was a quit phrase
           }
  
           // method `transform()' returns a final phrase in case of a quit phrase
           // but you can also get a final phrase with:
           var final = eliza.getFinal();
  
           // other methods: reset memory and internal state
           eliza.reset();
  
           // to set the internal memory size override property `memSize':
           eliza.memSize = 100; // (default: 20)
  
           // to reproduce the example conversation given by J. Weizenbaum
           // initialize with the optional random-choice-disable flag
           var originalEliza = new ElizaBot(true);
  
    `ElizaBot' is also a general chatbot engine that can be supplied with any rule set.
    (for required data structures cf. "elizadata.js" and/or see the documentation.)
    data is parsed and transformed for internal use at the creation time of the
    first instance of the `ElizaBot' constructor.
  
    vers 1.1: lambda functions in RegExps are currently a problem with too many browsers.
              changed code to work around.
  */
  
  function ElizaBot(noRandomFlag) {
          this.noRandom= (noRandomFlag)? true:false;
          this.capitalizeFirstLetter=true;
          this.debug=false;
          this.memSize=20;
          this.version="1.1 (original)";
          if (!this._dataParsed) this._init();
          this.reset();
  }
  
  ElizaBot.prototype.reset = function() {
          this.quit=false;
          this.mem=[];
          this.lastchoice=[];
          for (var k=0; k<elizaKeywords.length; k++) {
                  this.lastchoice[k]=[];
                  var rules=elizaKeywords[k][2];
                  for (var i=0; i<rules.length; i++) this.lastchoice[k][i]=-1;
          }
  }
  
  ElizaBot.prototype._dataParsed = false;
  
  ElizaBot.prototype._init = function() {
          // install ref to global object
          var global=ElizaBot.prototype.global=self;
          // parse data and convert it from canonical form to internal use
          // prodoce synonym list
          var synPatterns={};
          if ((global.elizaSynons) && (typeof elizaSynons == 'object')) {
                  for (var i in elizaSynons) synPatterns[i]='('+i+'|'+elizaSynons[i].join('|')+')';
          }
          // check for keywords or install empty structure to prevent any errors
          if ((!global.elizaKeywords) || (typeof elizaKeywords.length == 'undefined')) {
                  elizaKeywords=[[' ',0,[[' ',[]]]]];
          }
          // 1st convert rules to regexps
          // expand synonyms and insert asterisk expressions for backtracking
          var sre=/@(\S+)/;
          var are=/(\S)\s*\*\s*(\S)/;
          var are1=/^\s*\*\s*(\S)/;
          var are2=/(\S)\s*\*\s*/;
          var wsre=/\s+/g;
          for (var k=0; k<elizaKeywords.length; k++) {
                  var rules=elizaKeywords[k][2];
                  elizaKeywords[k][3]=k; // save original index for sorting
                  for (var i=0; i<rules.length; i++) {
                          var r=rules[i];
                          // check mem flag and store it as decomp's element 2
                          if (r[0].charAt(0)=='
  
  
(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.