/* 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; synopsis: new ElizaBot( ) ElizaBot.prototype.transform( ) 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; kb[1]) return -1 else if (a[1]b[3]) return 1 else if (a[3]\/\\\t/g, ' '); text=text.replace(/\s+-+\s+/g, '.'); text=text.replace(/\s*[,\.\?!;]+\s*/g, '.'); text=text.replace(/\s*\bbut\b\s*/g, '.'); text=text.replace(/\s{2,}/g, ' '); // split text in part sentences and loop through them var parts=text.split('.'); for (var i=0; i=0) { rpl = this._execRule(k); } if (rpl!='') return rpl; } } } // nothing matched try mem rpl=this._memGet(); // if nothing in mem, so try xnone if (rpl=='') { this.sentence=' '; var k=this._getRuleIndexByKey('xnone'); if (k>=0) rpl=this._execRule(k); } // return reply or default string return (rpl!='')? rpl : 'I am at a loss for words.'; } ElizaBot.prototype._execRule = function(k) { var rule=elizaKeywords[k]; var decomps=rule[2]; var paramre=/\(([0-9]+)\)/; for (var i=0; iri)) || (this.lastchoice[k][i]==ri)) { ri= ++this.lastchoice[k][i]; if (ri>=reasmbs.length) { ri=0; this.lastchoice[k][i]=-1; } } else { this.lastchoice[k][i]=ri; } var rpl=reasmbs[ri]; if (this.debug) alert('match:\nkey: '+elizaKeywords[k][0]+ '\nrank: '+elizaKeywords[k][1]+ '\ndecomp: '+decomps[i][0]+ '\nreasmb: '+rpl+ '\nmemflag: '+memflag); if (rpl.search('^goto ', 'i')==0) { ki=this._getRuleIndexByKey(rpl.substring(5)); if (ki>=0) return this._execRule(ki); } // substitute positional params (v.1.1: work around lambda function) var m1=paramre.exec(rpl); if (m1) { var lp=''; var rp=rpl; while (m1) { var param = m[parseInt(m1[1])]; // postprocess param var m2=this.postExp.exec(param); if (m2) { var lp2=''; var rp2=param; while (m2) { lp2+=rp2.substring(0,m2.index)+this.posts[m2[1]]; rp2=rp2.substring(m2.index+m2[0].length); m2=this.postExp.exec(rp2); } param=lp2+rp2; } lp+=rp.substring(0,m1.index)+param; rp=rp.substring(m1.index+m1[0].length); m1=paramre.exec(rp); } rpl=lp+rp; } rpl=this._postTransform(rpl); if (memflag) this._memSave(rpl) else return rpl; } } return ''; } ElizaBot.prototype._postTransform = function(s) { // final cleanings s=s.replace(/\s{2,}/g, ' '); s=s.replace(/\s+\./g, '.'); if ((this.global.elizaPostTransforms) && (elizaPostTransforms.length)) { for (var i=0; ithis.memSize) this.mem.shift(); } ElizaBot.prototype._memGet = function() { if (this.mem.length) { if (this.noRandom) return this.mem.shift(); else { var n=Math.floor(Math.random()*this.mem.length); var rpl=this.mem[n]; for (var i=n+1; i