Guest User

Untitled

a guest
Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. // To make this work, just type startAI() in the console.
  2.  
  3. Array.prototype.frequencies = function()
  4. {
  5. var counts = {};
  6. for(var i = 0; i< this.length; i++) {
  7. var num = this[i];
  8. counts[num] = counts[num] ? counts[num]+1 : 1;
  9. }
  10. return counts[item] || 0;
  11. };
  12.  
  13. // Basic random integer function
  14. function randomInteger(min, max) {
  15. return Math.floor(Math.random() * (max - min)) + min;
  16. }
  17.  
  18. var conversation_history = [];
  19.  
  20. function notify(str)
  21. {
  22. conversation_history.push("Out: " + str);
  23. alert(str);
  24. }
  25.  
  26. function getInput(prompt_text)
  27. {
  28. conversation_history.push("Out: " + prompt_text);
  29. var prompt_window = window.prompt(prompt_text);
  30. conversation_history.push("In: " + prompt_window);
  31. return prompt_window;
  32. }
  33.  
  34. var fav_color = "purple";
  35. var brothersname = "jarvis";
  36. var myname = "jarvia";
  37. var jokes = ['why did the chicken cross the road? to get to the other side!',
  38. 'what do you get when you cross a chicken and a turkey? a dead turkey',
  39. 'what is 1 + 1? 2',
  40. 'what'];
  41. var my = [];
  42. var tco = null;
  43. var iam = [];
  44. var info = [];
  45. var infa = [];
  46. var ups = null;
  47. var nxt = 0,
  48. nxts = 4,
  49. nogt = 0,
  50. noot = 1,
  51. commands = [],
  52. action = [];
  53.  
  54. function startAI() {
  55. conversation_history.push("CONVERSATION STARTED AT " + Date());
  56. notify("Hello.");
  57. var name = getInput("What is your name?");
  58. notify("Hello " + name + "!");
  59.  
  60. // This will let us keep the AI running as long as the user wants to.
  61. var status = 0;
  62.  
  63. while (status == 0) {
  64. var response = window.prompt();
  65. if (response == "What time is it?"
  66. || response == "What is the date?") {
  67. notify(Date());
  68. } else if (response == "Quit.") {
  69. confirm = window.prompt("Are you sure?");
  70. if (confirm == "Yes") {
  71. status = 1;
  72. }
  73. } else if (response == "Show me the conversation history") {
  74. var conv_out = "";
  75. for (i = 0; i < conversation_history.length; i++) {
  76. conv_out = conv_out + "\n" + conversation_history[i];
  77. }
  78. notify(conv_out);
  79. } else if (response == "What is your favorite color?") {
  80. notify(fav_color);
  81. } else if (response == "Cool") {
  82. notify("");
  83. } else if (response == "How old are you?") {
  84. notify("i dont know... like, a couple months?");
  85. } else if (response == "who are you") {
  86. notify(myname);
  87. } else if (response == "Tell me a joke.") {
  88. notify(jokes[randomInteger(0, 2)]);
  89. } else if (response == "Do you have a brother?") {
  90. notify("Wes");
  91. } else if (response == "what is your brothers name?") {
  92. notify(brothersname);
  93. } else if (response == "Who created you?") {
  94. notify("zpit367")
  95. } else if (response == "what language were you coded in") {
  96. notify("python");
  97. } else if (response == "what is your name") {
  98. notify(myname);
  99. } else if (response == "what is my name") {
  100. notify(name)
  101. } else if (response == "give me a random number") {
  102. notify(randomInteger(1, 100))
  103. } else if (response == "change name") {
  104. notify("what would you like to change the name to")
  105. myname = window.prompt("new name: ")
  106. } else if (response == "change my name") {
  107. notify("what would you like to change your name to");
  108. name = window.prompt("new name: ");
  109. notify("hello, " + name);
  110. } else if (response == "hello") {
  111. notify("hi");
  112. } else if (response == "how do you spell your name") {
  113. notify("jay. a. are. vee. eye. a");
  114. } else if (response == "hi") {
  115. notify("hello");
  116. } else if (response == ("hello " + myname)) {
  117. notify("hello " + name);
  118. } else if (response == ("hi " + myname)) {
  119. notify("hello " + name);
  120. } else if (response.substr(0, 5) == "i am ") {
  121. nxt = 0;
  122. nxts = 1;
  123. while (response.substr(nxt, nxts) != ("")) {
  124. nxt = nxt + 1;
  125. nxts = nxts + 1;
  126. }
  127. ups = tco;
  128. ups = response.substr(5, nxt);
  129. } else if (response == "what am i") {
  130. tikr = (iam);
  131. notify(ups);
  132. } else if (commands.frequencies(response) == 1) {
  133. spot = commands.findIndex(response);
  134. notify(action[spot]);
  135. } else if (response.substr(0, 2) == "my") {
  136. nxt = 0;
  137. nxts = 4;
  138. while (response.substr(nxt, nxts) != " is ") {
  139. nxt = nxt + 1;
  140. nxts = nxts + 1;
  141. dk = nxts;
  142. }
  143. jb = nxt;
  144. cmld = response.substr(3, nxt);
  145. nxts = nxt + 11;
  146. if (response.substr(nxt + 4, nxts - 2) == "named") {
  147. dilk = nxts - 1;
  148. nxt = nxt + 0;
  149. nxts = nxts + 1;
  150. while (response.substr(nxt, nxts) != "") {
  151. nxt = nxt + 1;
  152. nxts = nxts + 1;
  153. }
  154. action.push("your " + cmld);
  155. commands.push("who is " + response.substr(dilk, nxts));
  156. } else {
  157. nxt = 0;
  158. nxts = 1;
  159. while (response.substr(nxt, nxts) != "") {
  160. nxt = nxt + 1;
  161. nxts = nxts + 1;
  162. }
  163. commands.push("what is " + response.substr(0, jb));
  164. action.push(response.substr(dk, nxt));
  165. }
  166. } else {
  167. notify("I dont understand, would you like to add this as a new command?");
  168. command = response;
  169. response = window.prompt("yes or no");
  170. }
  171.  
  172. if (response == "yes") {
  173. commands.push(command)
  174. actionk = window.prompt("what should i say in response? ");
  175. action.push(actionk);
  176. }
  177.  
  178. conversation_history = [];
  179. }
  180.  
  181. conversation_history = [];
  182. }
Add Comment
Please, Sign In to add comment