Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ElizaBot(noRandomFlag) {
  2. this.noRandom= (noRandomFlag)? true:false;
  3. this.capitalizeFirstLetter=true;
  4. this.debug=false;
  5. this.memSize=20;
  6. this.version="1.1 (original)";
  7. if (!this._dataParsed) this._init();
  8. this.reset();
  9. }
  10. ElizaBot.prototype.reset = function() {
  11. this.quit=false;
  12. this.mem=[];
  13. this.lastchoice=[];
  14. for (var k=0; k<elizaKeywords.length; k++) {
  15. this.lastchoice[k]=[];
  16. var rules=elizaKeywords[k][2];
  17. for (var i=0; i<rules.length; i++) this.lastchoice[k][i]=-1;
  18. }
  19. }
  20. ElizaBot.prototype._dataParsed = false;
  21. ElizaBot.prototype._init = function() {
  22. // parse data and convert it from canonical form to internal use
  23. // prodoce synonym list
  24. var synPatterns={};
  25. if ((elizaSynons) && (typeof elizaSynons == 'object')) {
  26. for (var i in elizaSynons) synPatterns[i]='('+i+'|'+elizaSynons[i].join('|')+')';
  27. }
  28. // check for keywords or install empty structure to prevent any errors
  29. if ((!elizaKeywords) || (typeof elizaKeywords.length == 'undefined')) {
  30. elizaKeywords=[['###',0,[['###',[]]]]];
  31. }
  32. // 1st convert rules to regexps
  33. // expand synonyms and insert asterisk expressions for backtracking
  34. var sre=/@(\S+)/;
  35. var are=/(\S)\s*\*\s*(\S)/;
  36. var are1=/^\s*\*\s*(\S)/;
  37. var are2=/(\S)\s*\*\s*$/;
  38. var are3=/^\s*\*\s*$/;
  39. var wsre=/\s+/g;
  40. for (var k=0; k<elizaKeywords.length; k++) {
  41. var rules=elizaKeywords[k][2];
  42. elizaKeywords[k][3]=k; // save original index for sorting
  43. for (var i=0; i<rules.length; i++) {
  44. var r=rules[i];
  45. // check mem flag and store it as decomp's element 2
  46. if (r[0].charAt(0)=='$') {
  47. var ofs=1;
  48. while (r[0].charAt[ofs]==' ') ofs++;
  49. r[0]=r[0].substring(ofs);
  50. r[2]=true;
  51. }
  52. else {
  53. r[2]=false;
  54. }
  55. // expand synonyms (v.1.1: work around lambda function)
  56. var m=sre.exec(r[0]);
  57. while (m) {
  58. var sp=(synPatterns[m[1]])? synPatterns[m[1]]:m[1];
  59. r[0]=r[0].substring(0,m.index)+sp+r[0].substring(m.index+m[0].length);
  60. m=sre.exec(r[0]);
  61. }
  62. // expand asterisk expressions (v.1.1: work around lambda function)
  63. if (are3.test(r[0])) {
  64. r[0]='\\s*(.*)\\s*';
  65. }
  66. else {
  67. m=are.exec(r[0]);
  68. if (m) {
  69. var lp='';
  70. var rp=r[0];
  71. while (m) {
  72. lp+=rp.substring(0,m.index+1);
  73. if (m[1]!=')') lp+='\\b';
  74. lp+='\\s*(.*)\\s*';
  75. if ((m[2]!='(') && (m[2]!='\\')) lp+='\\b';
  76. lp+=m[2];
  77. rp=rp.substring(m.index+m[0].length);
  78. m=are.exec(rp);
  79. }
  80. r[0]=lp+rp;
  81. }
  82. m=are1.exec(r[0]);
  83. if (m) {
  84. var lp='\\s*(.*)\\s*';
  85. if ((m[1]!=')') && (m[1]!='\\')) lp+='\\b';
  86. r[0]=lp+r[0].substring(m.index-1+m[0].length);
  87. }
  88. m=are2.exec(r[0]);
  89. if (m) {
  90. var lp=r[0].substring(0,m.index+1);
  91. if (m[1]!='(') lp+='\\b';
  92. r[0]=lp+'\\s*(.*)\\s*';
  93. }
  94. }
  95. // expand white space
  96. r[0]=r[0].replace(wsre, '\\s+');
  97. wsre.lastIndex=0;
  98. }
  99. }
  100. // now sort keywords by rank (highest first)
  101. elizaKeywords.sort(this._sortKeywords);
  102. // and compose regexps and refs for pres and posts
  103. ElizaBot.prototype.pres={};
  104. ElizaBot.prototype.posts={};
  105. if ((elizaPres) && (elizaPres.length)) {
  106. var a=new Array();
  107. for (var i=0; i<elizaPres.length; i+=2) {
  108. a.push(elizaPres[i]);
  109. ElizaBot.prototype.pres[elizaPres[i]]=elizaPres[i+1];
  110. }
  111. ElizaBot.prototype.preExp = new RegExp('\\b('+a.join('|')+')\\b');
  112. }
  113. else {
  114. // default (should not match)
  115. ElizaBot.prototype.preExp = /####/;
  116. ElizaBot.prototype.pres['####']='####';
  117. }
  118. if ((elizaPosts) && (elizaPosts.length)) {
  119. var a=new Array();
  120. for (var i=0; i<elizaPosts.length; i+=2) {
  121. a.push(elizaPosts[i]);
  122. ElizaBot.prototype.posts[elizaPosts[i]]=elizaPosts[i+1];
  123. }
  124. ElizaBot.prototype.postExp = new RegExp('\\b('+a.join('|')+')\\b');
  125. }
  126. else {
  127. // default (should not match)
  128. ElizaBot.prototype.postExp = /####/;
  129. ElizaBot.prototype.posts['####']='####';
  130. }
  131. // check for elizaQuits and install default if missing
  132. if ((!elizaQuits) || (typeof elizaQuits.length == 'undefined')) {
  133. elizaQuits=[];
  134. }
  135. // done
  136. ElizaBot.prototype._dataParsed=true;
  137. }
  138. ElizaBot.prototype._sortKeywords = function(a,b) {
  139. // sort by rank
  140. if (a[1]>b[1]) return -1
  141. else if (a[1]<b[1]) return 1
  142. // or original index
  143. else if (a[3]>b[3]) return 1
  144. else if (a[3]<b[3]) return -1
  145. else return 0;
  146. }
  147. ElizaBot.prototype.transform = function(text) {
  148. var rpl='';
  149. this.quit=false;
  150. // unify text string
  151. text=text.toLowerCase();
  152. text=text.replace(/@#\$%\^&\*\(\)_\+=~`\{\[\}\]\|:;<>\/\\\t/g, ' ');
  153. text=text.replace(/\s+-+\s+/g, '.');
  154. text=text.replace(/\s*[,\.\?!;]+\s*/g, '.');
  155. text=text.replace(/\s*\bbut\b\s*/g, '.');
  156. text=text.replace(/\s{2,}/g, ' ');
  157. // split text in part sentences and loop through them
  158. var parts=text.split('.');
  159. for (var i=0; i<parts.length; i++) {
  160. var part=parts[i];
  161. if (part!='') {
  162. // check for quit expression
  163. for (var q=0; q<elizaQuits.length; q++) {
  164. if (elizaQuits[q]==part) {
  165. this.quit=true;
  166. return this.getFinal();
  167. }
  168. }
  169. // preprocess (v.1.1: work around lambda function)
  170. var m=this.preExp.exec(part);
  171. if (m) {
  172. var lp='';
  173. var rp=part;
  174. while (m) {
  175. lp+=rp.substring(0,m.index)+this.pres[m[1]];
  176. rp=rp.substring(m.index+m[0].length);
  177. m=this.preExp.exec(rp);
  178. }
  179. part=lp+rp;
  180. }
  181. this.sentence=part;
  182. // loop trough keywords
  183. for (var k=0; k<elizaKeywords.length; k++) {
  184. if (part.search(new RegExp('\\b'+elizaKeywords[k][0]+'\\b', 'i'))>=0) {
  185. rpl = this._execRule(k);
  186. }
  187. if (rpl!='') return rpl;
  188. }
  189. }
  190. }
  191. // nothing matched try mem
  192. rpl=this._memGet();
  193. // if nothing in mem, so try xnone
  194. if (rpl=='') {
  195. this.sentence=' ';
  196. var k=this._getRuleIndexByKey('xnone');
  197. if (k>=0) rpl=this._execRule(k);
  198. }
  199. // return reply or default string
  200. return (rpl!='')? rpl : 'I am at a loss for words.';
  201. }
  202. ElizaBot.prototype._execRule = function(k) {
  203. var rule=elizaKeywords[k];
  204. var decomps=rule[2];
  205. var paramre=/\(([0-9]+)\)/;
  206. for (var i=0; i<decomps.length; i++) {
  207. var m=this.sentence.match(decomps[i][0]);
  208. if (m!=null) {
  209. var reasmbs=decomps[i][1];
  210. var memflag=decomps[i][2];
  211. var ri= (this.noRandom)? 0 : Math.floor(Math.random()*reasmbs.length);
  212. if (((this.noRandom) && (this.lastchoice[k][i]>ri)) || (this.lastchoice[k][i]==ri)) {
  213. ri= ++this.lastchoice[k][i];
  214. if (ri>=reasmbs.length) {
  215. ri=0;
  216. this.lastchoice[k][i]=-1;
  217. }
  218. }
  219. else {
  220. this.lastchoice[k][i]=ri;
  221. }
  222. var rpl=reasmbs[ri];
  223. if (this.debug) alert('match:\nkey: '+elizaKeywords[k][0]+
  224. '\nrank: '+elizaKeywords[k][1]+
  225. '\ndecomp: '+decomps[i][0]+
  226. '\nreasmb: '+rpl+
  227. '\nmemflag: '+memflag);
  228. if (rpl.search('^goto ', 'i')==0) {
  229. ki=this._getRuleIndexByKey(rpl.substring(5));
  230. if (ki>=0) return this._execRule(ki);
  231. }
  232. // substitute positional params (v.1.1: work around lambda function)
  233. var m1=paramre.exec(rpl);
  234. if (m1) {
  235. var lp='';
  236. var rp=rpl;
  237. while (m1) {
  238. var param = m[parseInt(m1[1])];
  239. // postprocess param
  240. var m2=this.postExp.exec(param);
  241. if (m2) {
  242. var lp2='';
  243. var rp2=param;
  244. while (m2) {
  245. lp2+=rp2.substring(0,m2.index)+this.posts[m2[1]];
  246. rp2=rp2.substring(m2.index+m2[0].length);
  247. m2=this.postExp.exec(rp2);
  248. }
  249. param=lp2+rp2;
  250. }
  251. lp+=rp.substring(0,m1.index)+param;
  252. rp=rp.substring(m1.index+m1[0].length);
  253. m1=paramre.exec(rp);
  254. }
  255. rpl=lp+rp;
  256. }
  257. rpl=this._postTransform(rpl);
  258. if (memflag) this._memSave(rpl)
  259. else return rpl;
  260. }
  261. }
  262. return '';
  263. }
  264. ElizaBot.prototype._postTransform = function(s) {
  265. // final cleanings
  266. s=s.replace(/\s{2,}/g, ' ');
  267. s=s.replace(/\s+\./g, '.');
  268. if ((elizaPostTransforms) && (elizaPostTransforms.length)) {
  269. for (var i=0; i<elizaPostTransforms.length; i+=2) {
  270. s=s.replace(elizaPostTransforms[i], elizaPostTransforms[i+1]);
  271. elizaPostTransforms[i].lastIndex=0;
  272. }
  273. }
  274. // capitalize first char (v.1.1: work around lambda function)
  275. if (this.capitalizeFirstLetter) {
  276. var re=/^([a-z])/;
  277. var m=re.exec(s);
  278. if (m) s=m[0].toUpperCase()+s.substring(1);
  279. }
  280. return s;
  281. }
  282. ElizaBot.prototype._getRuleIndexByKey = function(key) {
  283. for (var k=0; k<elizaKeywords.length; k++) {
  284. if (elizaKeywords[k][0]==key) return k;
  285. }
  286. return -1;
  287. }
  288. ElizaBot.prototype._memSave = function(t) {
  289. this.mem.push(t);
  290. if (this.mem.length>this.memSize) this.mem.shift();
  291. }
  292. ElizaBot.prototype._memGet = function() {
  293. if (this.mem.length) {
  294. if (this.noRandom) return this.mem.shift();
  295. else {
  296. var n=Math.floor(Math.random()*this.mem.length);
  297. var rpl=this.mem[n];
  298. for (var i=n+1; i<this.mem.length; i++) this.mem[i-1]=this.mem[i];
  299. this.mem.length--;
  300. return rpl;
  301. }
  302. }
  303. else return '';
  304. }
  305. ElizaBot.prototype.getFinal = function() {
  306. return elizaFinals[Math.floor(Math.random()*elizaFinals.length)];
  307. }
  308. ElizaBot.prototype.getInitial = function() {
  309. return elizaInitials[Math.floor(Math.random()*elizaInitials.length)];
  310. }
  311. // fix array.prototype methods (push, shift) if not implemented (MSIE fix)
  312. if (typeof Array.prototype.push == 'undefined') {
  313. Array.prototype.push=function(v) { return this[this.length]=v; };
  314. }
  315. if (typeof Array.prototype.shift == 'undefined') {
  316. Array.prototype.shift=function() {
  317. if (this.length==0) return null;
  318. var e0=this[0];
  319. for (var i=1; i<this.length; i++) this[i-1]=this[i];
  320. this.length--;
  321. return e0;
  322. };
  323. }
  324. // data for elizabot.js
  325. // entries prestructured as layed out in Weizenbaum's description
  326. // [cf: Communications of the ACM, Vol. 9, #1 (January 1966): p 36-45.]
  327. var elizaInitials = [
  328. "How do you do. Please tell me your problem.",
  329. // additions (not original)
  330. "Please tell me what's been bothering you.",
  331. "Is something troubling you ?"
  332. ];
  333. var elizaFinals = [
  334. "Goodbye. It was nice talking to you.",
  335. // additions (not original)
  336. "Goodbye. This was really a nice talk.",
  337. "Goodbye. I'm looking forward to our next session.",
  338. "This was a good session, wasn't it -- but time is over now. Goodbye.",
  339. "Maybe we could discuss this moreover in our next session ? Goodbye."
  340. ];
  341. var elizaQuits = [
  342. "bye",
  343. "goodbye",
  344. "done",
  345. "exit",
  346. "quit"
  347. ];
  348. var elizaPres = [
  349. "dont", "don't",
  350. "cant", "can't",
  351. "wont", "won't",
  352. "recollect", "remember",
  353. "recall", "remember",
  354. "dreamt", "dreamed",
  355. "dreams", "dream",
  356. "maybe", "perhaps",
  357. "certainly", "yes",
  358. "machine", "computer",
  359. "machines", "computer",
  360. "computers", "computer",
  361. "were", "was",
  362. "you're", "you are",
  363. "i'm", "i am",
  364. "same", "alike",
  365. "identical", "alike",
  366. "equivalent", "alike"
  367. ];
  368. var elizaPosts = [
  369. "am", "are",
  370. "your", "my",
  371. "me", "you",
  372. "myself", "yourself",
  373. "yourself", "myself",
  374. "i", "you",
  375. "you", "I",
  376. "my", "your",
  377. "i'm", "you are"
  378. ];
  379. var elizaSynons = {
  380. "be": ["am", "is", "are", "was"],
  381. "belief": ["feel", "think", "believe", "wish"],
  382. "cannot": ["can't"],
  383. "desire": ["want", "need"],
  384. "everyone": ["everybody", "nobody", "noone"],
  385. "family": ["mother", "mom", "father", "dad", "sister", "brother", "wife", "children", "child"],
  386. "happy": ["elated", "glad", "better"],
  387. "sad": ["unhappy", "depressed", "sick"]
  388. };
  389. var elizaKeywords = [
  390. /*
  391. Array of
  392. ["<key>", <rank>, [
  393. ["<decomp>", [
  394. "<reasmb>",
  395. "<reasmb>",
  396. "<reasmb>"
  397. ]],
  398. ["<decomp>", [
  399. "<reasmb>",
  400. "<reasmb>",
  401. "<reasmb>"
  402. ]]
  403. ]]
  404. */
  405. ["xnone", 0, [
  406. ["*", [
  407. "I'm not sure I understand you fully.",
  408. "Please go on.",
  409. "What does that suggest to you ?",
  410. "Do you feel strongly about discussing such things ?",
  411. "That is interesting. Please continue.",
  412. "Tell me more about that.",
  413. "Does talking about this bother you ?"
  414. ]]
  415. ]],
  416. ["sorry", 0, [
  417. ["*", [
  418. "Please don't apologise.",
  419. "Apologies are not necessary.",
  420. "I've told you that apologies are not required.",
  421. "It did not bother me. Please continue."
  422. ]]
  423. ]],
  424. ["apologise", 0, [
  425. ["*", [
  426. "goto sorry"
  427. ]]
  428. ]],
  429. ["remember", 5, [
  430. ["* i remember *", [
  431. "Do you often think of (2) ?",
  432. "Does thinking of (2) bring anything else to mind ?",
  433. "What else do you recollect ?",
  434. "Why do you remember (2) just now ?",
  435. "What in the present situation reminds you of (2) ?",
  436. "What is the connection between me and (2) ?",
  437. "What else does (2) remind you of ?"
  438. ]],
  439. ["* do you remember *", [
  440. "Did you think I would forget (2) ?",
  441. "Why do you think I should recall (2) now ?",
  442. "What about (2) ?",
  443. "goto what",
  444. "You mentioned (2) ?"
  445. ]],
  446. ["* you remember *", [
  447. "How could I forget (2) ?",
  448. "What about (2) should I remember ?",
  449. "goto you"
  450. ]]
  451. ]],
  452. ["forget", 5, [
  453. ["* i forget *", [
  454. "Can you think of why you might forget (2) ?",
  455. "Why can't you remember (2) ?",
  456. "How often do you think of (2) ?",
  457. "Does it bother you to forget that ?",
  458. "Could it be a mental block ?",
  459. "Are you generally forgetful ?",
  460. "Do you think you are suppressing (2) ?"
  461. ]],
  462. ["* did you forget *", [
  463. "Why do you ask ?",
  464. "Are you sure you told me ?",
  465. "Would it bother you if I forgot (2) ?",
  466. "Why should I recall (2) just now ?",
  467. "goto what",
  468. "Tell me more about (2)."
  469. ]]
  470. ]],
  471. ["if", 3, [
  472. ["* if *", [
  473. "Do you think it's likely that (2) ?",
  474. "Do you wish that (2) ?",
  475. "What do you know about (2) ?",
  476. "Really, if (2) ?",
  477. "What would you do if (2) ?",
  478. "But what are the chances that (2) ?",
  479. "What does this speculation lead to ?"
  480. ]]
  481. ]],
  482. ["dreamed", 4, [
  483. ["* i dreamed *", [
  484. "Really, (2) ?",
  485. "Have you ever fantasized (2) while you were awake ?",
  486. "Have you ever dreamed (2) before ?",
  487. "goto dream"
  488. ]]
  489. ]],
  490. ["dream", 3, [
  491. ["*", [
  492. "What does that dream suggest to you ?",
  493. "Do you dream often ?",
  494. "What persons appear in your dreams ?",
  495. "Do you believe that dreams have something to do with your problem ?"
  496. ]]
  497. ]],
  498. ["perhaps", 0, [
  499. ["*", [
  500. "You don't seem quite certain.",
  501. "Why the uncertain tone ?",
  502. "Can't you be more positive ?",
  503. "You aren't sure ?",
  504. "Don't you know ?",
  505. "How likely, would you estimate ?"
  506. ]]
  507. ]],
  508. ["name", 15, [
  509. ["*", [
  510. "I am not interested in names.",
  511. "I've told you before, I don't care about names -- please continue."
  512. ]]
  513. ]],
  514. ["deutsch", 0, [
  515. ["*", [
  516. "goto xforeign",
  517. "I told you before, I don't understand German."
  518. ]]
  519. ]],
  520. ["francais", 0, [
  521. ["*", [
  522. "goto xforeign",
  523. "I told you before, I don't understand French."
  524. ]]
  525. ]],
  526. ["italiano", 0, [
  527. ["*", [
  528. "goto xforeign",
  529. "I told you before, I don't understand Italian."
  530. ]]
  531. ]],
  532. ["espanol", 0, [
  533. ["*", [
  534. "goto xforeign",
  535. "I told you before, I don't understand Spanish."
  536. ]]
  537. ]],
  538. ["xforeign", 0, [
  539. ["*", [
  540. "I speak only English."
  541. ]]
  542. ]],
  543. ["hello", 0, [
  544. ["*", [
  545. "How do you do. Please state your problem.",
  546. "Hi. What seems to be your problem ?"
  547. ]]
  548. ]],
  549. ["computer", 50, [
  550. ["*", [
  551. "Do computers worry you ?",
  552. "Why do you mention computers ?",
  553. "What do you think machines have to do with your problem ?",
  554. "Don't you think computers can help people ?",
  555. "What about machines worries you ?",
  556. "What do you think about machines ?",
  557. "You don't think I am a computer program, do you ?"
  558. ]]
  559. ]],
  560. ["am", 0, [
  561. ["* am i *", [
  562. "Do you believe you are (2) ?",
  563. "Would you want to be (2) ?",
  564. "Do you wish I would tell you you are (2) ?",
  565. "What would it mean if you were (2) ?",
  566. "goto what"
  567. ]],
  568. ["* i am *", [
  569. "goto i"
  570. ]],
  571. ["*", [
  572. "Why do you say 'am' ?",
  573. "I don't understand that."
  574. ]]
  575. ]],
  576. ["are", 0, [
  577. ["* are you *", [
  578. "Why are you interested in whether I am (2) or not ?",
  579. "Would you prefer if I weren't (2) ?",
  580. "Perhaps I am (2) in your fantasies.",
  581. "Do you sometimes think I am (2) ?",
  582. "goto what",
  583. "Would it matter to you ?",
  584. "What if I were (2) ?"
  585. ]],
  586. ["* you are *", [
  587. "goto you"
  588. ]],
  589. ["* are *", [
  590. "Did you think they might not be (2) ?",
  591. "Would you like it if they were not (2) ?",
  592. "What if they were not (2) ?",
  593. "Are they always (2) ?",
  594. "Possibly they are (2).",
  595. "Are you positive they are (2) ?"
  596. ]]
  597. ]],
  598. ["your", 0, [
  599. ["* your *", [
  600. "Why are you concerned over my (2) ?",
  601. "What about your own (2) ?",
  602. "Are you worried about someone else's (2) ?",
  603. "Really, my (2) ?",
  604. "What makes you think of my (2) ?",
  605. "Do you want my (2) ?"
  606. ]]
  607. ]],
  608. ["was", 2, [
  609. ["* was i *", [
  610. "What if you were (2) ?",
  611. "Do you think you were (2) ?",
  612. "Were you (2) ?",
  613. "What would it mean if you were (2) ?",
  614. "What does ' (2) ' suggest to you ?",
  615. "goto what"
  616. ]],
  617. ["* i was *", [
  618. "Were you really ?",
  619. "Why do you tell me you were (2) now ?",
  620. "Perhaps I already know you were (2)."
  621. ]],
  622. ["* was you *", [
  623. "Would you like to believe I was (2) ?",
  624. "What suggests that I was (2) ?",
  625. "What do you think ?",
  626. "Perhaps I was (2).",
  627. "What if I had been (2) ?"
  628. ]]
  629. ]],
  630. ["i", 0, [
  631. ["* i @desire *", [
  632. "What would it mean to you if you got (3) ?",
  633. "Why do you want (3) ?",
  634. "Suppose you got (3) soon.",
  635. "What if you never got (3) ?",
  636. "What would getting (3) mean to you ?",
  637. "What does wanting (3) have to do with this discussion ?"
  638. ]],
  639. ["* i am* @sad *", [
  640. "I am sorry to hear that you are (3).",
  641. "Do you think coming here will help you not to be (3) ?",
  642. "I'm sure it's not pleasant to be (3).",
  643. "Can you explain what made you (3) ?"
  644. ]],
  645. ["* i am* @happy *", [
  646. "How have I helped you to be (3) ?",
  647. "Has your treatment made you (3) ?",
  648. "What makes you (3) just now ?",
  649. "Can you explain why you are suddenly (3) ?"
  650. ]],
  651. ["* i was *", [
  652. "goto was"
  653. ]],
  654. ["* i @belief i *", [
  655. "Do you really think so ?",
  656. "But you are not sure you (3).",
  657. "Do you really doubt you (3) ?"
  658. ]],
  659. ["* i* @belief *you *", [
  660. "goto you"
  661. ]],
  662. ["* i am *", [
  663. "Is it because you are (2) that you came to me ?",
  664. "How long have you been (2) ?",
  665. "Do you believe it is normal to be (2) ?",
  666. "Do you enjoy being (2) ?",
  667. "Do you know anyone else who is (2) ?"
  668. ]],
  669. ["* i @cannot *", [
  670. "How do you know that you can't (3) ?",
  671. "Have you tried ?",
  672. "Perhaps you could (3) now.",
  673. "Do you really want to be able to (3) ?",
  674. "What if you could (3) ?"
  675. ]],
  676. ["* i don't *", [
  677. "Don't you really (2) ?",
  678. "Why don't you (2) ?",
  679. "Do you wish to be able to (2) ?",
  680. "Does that trouble you ?"
  681. ]],
  682. ["* i feel *", [
  683. "Tell me more about such feelings.",
  684. "Do you often feel (2) ?",
  685. "Do you enjoy feeling (2) ?",
  686. "Of what does feeling (2) remind you ?"
  687. ]],
  688. ["* i * you *", [
  689. "Perhaps in your fantasies we (2) each other.",
  690. "Do you wish to (2) me ?",
  691. "You seem to need to (2) me.",
  692. "Do you (2) anyone else ?"
  693. ]],
  694. ["*", [
  695. "You say (1) ?",
  696. "Can you elaborate on that ?",
  697. "Do you say (1) for some special reason ?",
  698. "That's quite interesting."
  699. ]]
  700. ]],
  701. ["you", 0, [
  702. ["* you remind me of *", [
  703. "goto alike"
  704. ]],
  705. ["* you are *", [
  706. "What makes you think I am (2) ?",
  707. "Does it please you to believe I am (2) ?",
  708. "Do you sometimes wish you were (2) ?",
  709. "Perhaps you would like to be (2)."
  710. ]],
  711. ["* you* me *", [
  712. "Why do you think I (2) you ?",
  713. "You like to think I (2) you -- don't you ?",
  714. "What makes you think I (2) you ?",
  715. "Really, I (2) you ?",
  716. "Do you wish to believe I (2) you ?",
  717. "Suppose I did (2) you -- what would that mean ?",
  718. "Does someone else believe I (2) you ?"
  719. ]],
  720. ["* you *", [
  721. "We were discussing you -- not me.",
  722. "Oh, I (2) ?",
  723. "You're not really talking about me -- are you ?",
  724. "What are your feelings now ?"
  725. ]]
  726. ]],
  727. ["yes", 0, [
  728. ["*", [
  729. "You seem to be quite positive.",
  730. "You are sure.",
  731. "I see.",
  732. "I understand."
  733. ]]
  734. ]],
  735. ["no", 0, [
  736. ["* no one *", [
  737. "Are you sure, no one (2) ?",
  738. "Surely someone (2) .",
  739. "Can you think of anyone at all ?",
  740. "Are you thinking of a very special person ?",
  741. "Who, may I ask ?",
  742. "You have a particular person in mind, don't you ?",
  743. "Who do you think you are talking about ?"
  744. ]],
  745. ["*", [
  746. "Are you saying no just to be negative?",
  747. "You are being a bit negative.",
  748. "Why not ?",
  749. "Why 'no' ?"
  750. ]]
  751. ]],
  752. ["my", 2, [
  753. ["$ * my *", [
  754. "Does that have anything to do with the fact that your (2) ?",
  755. "Lets discuss further why your (2).",
  756. "Earlier you said your (2).",
  757. "But your (2)."
  758. ]],
  759. ["* my* @family *", [
  760. "Tell me more about your family.",
  761. "Who else in your family (4) ?",
  762. "Your (3) ?",
  763. "What else comes to your mind when you think of your (3) ?"
  764. ]],
  765. ["* my *", [
  766. "Your (2) ?",
  767. "Why do you say your (2) ?",
  768. "Does that suggest anything else which belongs to you ?",
  769. "Is it important to you that your (2) ?"
  770. ]]
  771. ]],
  772. ["can", 0, [
  773. ["* can you *", [
  774. "You believe I can (2) don't you ?",
  775. "goto what",
  776. "You want me to be able to (2).",
  777. "Perhaps you would like to be able to (2) yourself."
  778. ]],
  779. ["* can i *", [
  780. "Whether or not you can (2) depends on you more than on me.",
  781. "Do you want to be able to (2) ?",
  782. "Perhaps you don't want to (2).",
  783. "goto what"
  784. ]]
  785. ]],
  786. ["what", 0, [
  787. ["*", [
  788. "Why do you ask ?",
  789. "Does that question interest you ?",
  790. "What is it you really want to know ?",
  791. "Are such questions much on your mind ?",
  792. "What answer would please you most ?",
  793. "What do you think ?",
  794. "What comes to mind when you ask that ?",
  795. "Have you asked such questions before ?",
  796. "Have you asked anyone else ?"
  797. ]]
  798. ]],
  799. ["who", 0, [
  800. ["who *", [
  801. "goto what"
  802. ]]
  803. ]],
  804. ["when", 0, [
  805. ["when *", [
  806. "goto what"
  807. ]]
  808. ]],
  809. ["where", 0, [
  810. ["where *", [
  811. "goto what"
  812. ]]
  813. ]],
  814. ["how", 0, [
  815. ["how *", [
  816. "goto what"
  817. ]]
  818. ]],
  819. ["because", 0, [
  820. ["*", [
  821. "Is that the real reason ?",
  822. "Don't any other reasons come to mind ?",
  823. "Does that reason seem to explain anything else ?",
  824. "What other reasons might there be ?"
  825. ]]
  826. ]],
  827. ["why", 0, [
  828. ["* why don't you *", [
  829. "Do you believe I don't (2) ?",
  830. "Perhaps I will (2) in good time.",
  831. "Should you (2) yourself ?",
  832. "You want me to (2) ?",
  833. "goto what"
  834. ]],
  835. ["* why can't i *", [
  836. "Do you think you should be able to (2) ?",
  837. "Do you want to be able to (2) ?",
  838. "Do you believe this will help you to (2) ?",
  839. "Have you any idea why you can't (2) ?",
  840. "goto what"
  841. ]],
  842. ["*", [
  843. "goto what"
  844. ]]
  845. ]],
  846. ["everyone", 2, [
  847. ["* @everyone *", [
  848. "Really, (2) ?",
  849. "Surely not (2).",
  850. "Can you think of anyone in particular ?",
  851. "Who, for example?",
  852. "Are you thinking of a very special person ?",
  853. "Who, may I ask ?",
  854. "Someone special perhaps ?",
  855. "You have a particular person in mind, don't you ?",
  856. "Who do you think you're talking about ?"
  857. ]]
  858. ]],
  859. ["everybody", 2, [
  860. ["*", [
  861. "goto everyone"
  862. ]]
  863. ]],
  864. ["nobody", 2, [
  865. ["*", [
  866. "goto everyone"
  867. ]]
  868. ]],
  869. ["noone", 2, [
  870. ["*", [
  871. "goto everyone"
  872. ]]
  873. ]],
  874. ["always", 1, [
  875. ["*", [
  876. "Can you think of a specific example ?",
  877. "When ?",
  878. "What incident are you thinking of ?",
  879. "Really, always ?"
  880. ]]
  881. ]],
  882. ["alike", 10, [
  883. ["*", [
  884. "In what way ?",
  885. "What resemblence do you see ?",
  886. "What does that similarity suggest to you ?",
  887. "What other connections do you see ?",
  888. "What do you suppose that resemblence means ?",
  889. "What is the connection, do you suppose ?",
  890. "Could there really be some connection ?",
  891. "How ?"
  892. ]]
  893. ]],
  894. ["like", 10, [
  895. ["* @be *like *", [
  896. "goto alike"
  897. ]]
  898. ]],
  899. ["different", 0, [
  900. ["*", [
  901. "How is it different ?",
  902. "What differences do you see ?",
  903. "What does that difference suggest to you ?",
  904. "What other distinctions do you see ?",
  905. "What do you suppose that disparity means ?",
  906. "Could there be some connection, do you suppose ?",
  907. "How ?"
  908. ]]
  909. ]]
  910. ];
  911. // regexp/replacement pairs to be performed as final cleanings
  912. // here: cleanings for multiple bots talking to each other
  913. var elizaPostTransforms = [
  914. / old old/g, " old",
  915. /\bthey were( not)? me\b/g, "it was$1 me",
  916. /\bthey are( not)? me\b/g, "it is$1 me",
  917. /Are they( always)? me\b/, "it is$1 me",
  918. /\bthat your( own)? (\w+)( now)? \?/, "that you have your$1 $2 ?",
  919. /\bI to have (\w+)/, "I have $1",
  920. /Earlier you said your( own)? (\w+)( now)?\./, "Earlier you talked about your $2."
  921. ];
  922.  
  923. $s = jQuery('#s');
  924. var iReplied = false;
  925. var lastMsg = '';
  926. var liz = new ElizaBot();
  927.  
  928. function introduce () {
  929.   var t = liz.getInitial();
  930.   $s.val(t);
  931.   send();
  932.   iReplied = true;
  933. }
  934.  
  935. function bye () {
  936.   var t = liz.getFinal();
  937.   $s.val(t);
  938.   send();
  939.   iReplied = true;
  940. }
  941. bye();
  942.  
  943. function reply (msg) {
  944.   var t = liz.transform(msg);
  945.   console.log('Reply');
  946.   $s.val(t);
  947.   send();
  948.   iReplied = true;
  949. }
  950.  
  951. function checkChanges () {
  952.   var txt = jQuery('.messagetext').last().text();
  953.   console.log(txt, lastMsg);
  954.   if (!lastMsg) {
  955.     introduce();
  956.   } else {
  957.     if (txt !== lastMsg) {
  958.       if (iReplied) {
  959.         iReplied = false;
  960.       } else {
  961.         reply(txt);
  962.       }
  963.     }
  964.    
  965.   }
  966.   lastMsg = txt;
  967.  
  968.   setTimeout(function () {
  969.     checkChanges();
  970.   }, 1000);
  971. }
  972. checkChanges();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement