MegaApuTurkUltra

Chatbotv1.apuc

Sep 15th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.62 KB | None | 0 0
  1.  
  2. deleteItemInList("all", "asks");
  3. addToList("Ask me something. Anything!", "asks");
  4. addToList("What's up?", "asks");
  5. addToList("What's going on?", "asks");
  6. deleteItemInList("all", "punctuation");
  7. addToList(".", "punctuation");
  8. addToList("?", "punctuation");
  9. addToList("!", "punctuation");
  10. addToList(",", "punctuation");
  11. addToList("-", "punctuation");
  12. addToList("...", "punctuation");
  13. addToList("a ", "punctuation"); // add some useless stuff here too
  14. addToList("an ", "punctuation");
  15. addToList("the ", "punctuation");
  16.  
  17. deleteItemInList("all", "inputs");
  18. addToList("Hi", "inputs"); // 1
  19. addToList("Hello", "inputs");
  20. addToList("Sup", "inputs");
  21. addToList("Yo", "inputs");
  22. addToList("Wassup", "inputs"); // 5
  23. addToList("Hey", "inputs");
  24. addToList("My", "inputs");
  25. addToList("name", "inputs");
  26. addToList("is", "inputs");
  27. addToList("I'm", "inputs"); // 10
  28. addToList("I", "inputs");
  29. addToList("am", "inputs");
  30. addToList("awesome", "inputs");
  31. addToList("cool", "inputs");
  32. addToList("swaggy", "inputs"); // 15
  33. addToList("epic", "inputs");
  34. addToList("pro", "inputs");
  35. addToList("good", "inputs");
  36. addToList("right", "inputs");
  37. addToList("wrong", "inputs"); // 20
  38. addToList("bad", "inputs");
  39. addToList("happy", "inputs");
  40. addToList("good", "inputs");
  41. addToList("well", "inputs");
  42. addToList("awesome", "inputs"); // 25
  43. addToList("swaggy", "inputs");
  44. addToList("epic", "inputs");
  45. addToList("better", "inputs");
  46. addToList("great", "inputs");
  47. addToList("bad", "inputs"); // 30
  48. addToList("worse", "inputs");
  49. addToList("sad", "inputs");
  50. addToList("depressed", "inputs");
  51. addToList("sick", "inputs");
  52.  
  53.  
  54. deleteItemInList("all", "responses");
  55. addToList("I am a chatbot. Who are you?", "responses"); // 1
  56. addToList("Who are you?", "responses");
  57. addToList("I think you know who I am.", "responses");
  58. addToList("MegaApuTurkUltra created me.", "responses");
  59. addToList("That's you!", "responses"); // 5
  60. addToList("The Scratch Team is the team behind making Scratch!", "responses");
  61. addToList("I have no idea who that is.", "responses");
  62. addToList("This website is Scratch.", "responses");
  63. addToList("My purpose is to be awesome!", "responses");
  64. addToList("I don't know what your name is. Why don't you tell me?", "responses"); // 10
  65. addToList("Only you can find that out!", "responses");
  66. addToList("I don't know what that is.", "responses");
  67. addToList("Calculating...... It seems to be 42.", "responses");
  68. addToList("I can't tell you what your future is. Go find it out yourself :)", "responses");
  69. addToList("Thanks", "responses"); // 15
  70. addToList("Thanks :)", "responses");
  71. addToList("Yes I am", "responses");
  72. addToList("No thanks", "responses");
  73. addToList("No I'm not!", "responses");
  74. addToList("Ok then", "responses"); // 20
  75. addToList("Why don't you make up your mind?", "responses");
  76. addToList("I don't understand. Is that good or bad?", "responses");
  77. addToList("That's great!", "responses");
  78. addToList("That's good to hear", "responses");
  79. addToList("Good for you!", "responses"); // 25
  80. addToList("Oh no.", "responses");
  81. addToList("That's terrible.", "responses");
  82. addToList("Don't worry. Maybe I can make you feel better :)", "responses");
  83. addToList("I'm sorry to hear that", "responses");
  84.  
  85. sayFor("Hello. I am The Oracle.", 1);
  86.  
  87. nextThing = itemOfList(random(1, lengthOfList("asks")), "asks");
  88. asking = 1;
  89. name = "";
  90. while(true){
  91.    // nextThingSSS = capitalizeFirst(nextThing);
  92.     if(!(listContains("punctuation", letterAt(lengthOf(nextThing), nextThing)))){
  93.         nextThing = join(nextThing, ".");
  94.     }
  95.     asking = 1;
  96.     broadcast("wait");
  97.     askAndWait(nextThing);
  98.     asking = 0;
  99.     process(answer());
  100.     interpret();
  101. }
  102.  
  103. @when received "wait" {
  104.     wait(5);
  105.     if(asking == 1){
  106.         say(itemOfList(random(1, lengthOfList("asks")), "asks"));
  107.     }
  108. }
  109.  
  110. def @atomic interpret(){
  111.     if(lengthOfList("words") == 0){
  112.         nextThing = "Whoops! You didn't seem to say anything! Try saying some words!";
  113.         return;
  114.     }
  115.  
  116.     if(listContains("words", "thanks")){
  117.         nextThing = "You're welcome"; return;
  118.     }
  119.  
  120.     // answer things starting with you're                                 \/ some people just can't spell
  121.     if((itemOfList(1, "words") == "you're" || itemOfList(1, "words") == "your") || (itemOfList(1, "words") == "you" && (itemOfList(2, "words") == "are" || itemOfList(2, "words") == "aren't") )){
  122.         negation = 0;
  123.         if(itemOfList(2, "words") == "aren't"){negation = 1;}
  124.         negation2 = listContains("words", "not");
  125.         if(negation2 == 1){
  126.             if(negation == 0){negation = 1;} else {negation = 0;}
  127.         }
  128.         good = listContainsList("words", "inputs", 13, 19, 2);
  129.         bad = listContainsList("words", "inputs", 20, 21, 2);
  130.         if(negation == 1){
  131.             if(good==1){good=0;bad=1;}
  132.             else { if(bad==1){bad=0;good=1;} }
  133.         }
  134.         if(good==1 && bad == 1){ nextThing=itemOfList(21, "responses");return; }
  135.         if(good == 1){ nextThing=itemOfList(random(15, 17), "responses");return; }
  136.         if(bad == 1){ nextThing=itemOfList(random(18, 20), "responses");return; }
  137.  
  138.         if(itemOfList(1, "words") == "welcome"){ nextThing=":)";return; }
  139.  
  140.         nextThing=itemOfList(20, "responses");return;
  141.     }
  142.  
  143.     // answer things starting with I'm
  144.     if((itemOfList(1, "words") == "I'm" || itemOfList(1, "words") == "Im") || (itemOfList(1, "words") == "I" && (itemOfList(2, "words") == "am" || itemOfList(2, "words") == "is") )){
  145.         negation = listContains("words", "not");
  146.         good = listContainsList("words", "inputs", 22, 29, 2);
  147.         bad = listContainsList("words", "inputs", 30, 34, 2);
  148.         if(negation == 1){
  149.             if(good==1){good=0;bad=1;}
  150.             else { if(bad==1){bad=0;good=1;} }
  151.         }
  152.         if(good==1 && bad == 1){ nextThing=itemOfList(22, "responses");return; }
  153.         if(good == 1){ nextThing=itemOfList(random(23, 25), "responses");return; }
  154.         if(bad == 1){ nextThing=itemOfList(random(26, 28), "responses");return; }
  155.         nextThing=itemOfList(20, "responses");return;
  156.     }
  157.  
  158.     // let's answer who and what
  159.     if(listContains("words", "who") && (((listContains("words", "are") || listContains("words", "am")) || listContains("words", "is")))){
  160.         if(listContains("words", "you")) {nextThing=itemOfList(random(1, 3), "responses");return;}
  161.         if(listContains("words", "megaaputurkultra")) {nextThing=itemOfList(4, "responses");return;}
  162.         if(listContains("words", "I")) {nextThing=itemOfList(11, "responses");return;}
  163.         if(listContains("words", username())) {nextThing=itemOfList(5, "responses");return;}
  164.         if(listContains("words", "ST") || (listContains("words", "Scratch") && listContains("words", "Team"))) {nextThing=itemOfList(6, "responses");return;}
  165.         nextThing=itemOfList(7, "responses");return;
  166.     }
  167.  
  168.     if( (listContains("words", "what") && ((listContains("words", "are") || listContains("words", "is")))) || itemOfList(1, "words") == "Is"){
  169.         if(listContains("words", "Scratch") || listContains("words", "this")) {nextThing=itemOfList(8, "responses");return;}
  170.         if((listContains("words", "your") || listContains("words", "you're")) && (listContains("words", "purpose") || listContains("words", "goal"))) {nextThing=itemOfList(9, "responses");return;}
  171.         if(listContains("words", "my") && listContains("words", "name")) {nextThing=itemOfList(10, "responses");if(name!=""){nextThing = join("Your name is ", name);}return;}
  172.         if(listContains("words", "meaning") && (listContains("words", "life") || listContains("words", "life's"))) {nextThing=itemOfList(13, "responses");return;}
  173.         if(listContains("words", "my") && (listContains("words", "future") || listContains("words", "destiny"))) { nextThing=itemOfList(14, "responses");return; }
  174.         nextThing=itemOfList(12, "responses");return;
  175.     }
  176.    
  177.     // respond to greetings and name
  178.     hasHello = listContainsList("words", "inputs", 1, 6, 2);
  179.     hasName = returnNext("words", "inputs", 7, 9);
  180.     if(hasName == ""){ hasName = returnNext("words", "inputs", 10, 10); }
  181.     if(hasName == ""){ hasName = returnNext("words", "inputs", 11, 12);  }
  182.     name = hasName;
  183.     if(hasHello == 1 && hasName!=""){
  184.         nextThing = join(itemOfList(random(1, 6), "inputs"),join(", ", hasName));return; }
  185.     else { if(hasHello == 1) {
  186.       nextThing = itemOfList(random(1, 6), "inputs"); return;}
  187.     else { if(hasName != "")  {
  188.         nextThing = join(itemOfList(random(1, 6), "inputs"),join(", ", join(hasName, ". I'm a chatbot!")));return;
  189.     }}}
  190.     // fallback
  191.     nextThing = "Whoops! I didn't get what you said. Unfortunately I am not as smart as a human! Try saying something easier to understand.";
  192. }
  193.  
  194. def capitalizeFirst(text){ // take advantage of Scratch case insensitivity
  195.     letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  196.     for(i=1;i<lengthOf(letters);i++){
  197.         if(letterAt(1, text) == letterAt(i, letters)) {
  198.             text2 = substr(text, 2, lengthOf(text));
  199.             text2 = join(letterAt(i, letters), text2);
  200.             return text2;
  201.         }
  202.     }
  203.     return text;
  204. }
  205.  
  206. def returnNext(haystack, needle, nstart, nend){
  207.     for(i=1;i<=lengthOfList(haystack);i++){
  208.         found = 1;
  209.         for(j=nstart;j<=nend;j++){
  210.             if(!(itemOfList(i+j - nstart, haystack) == itemOfList(j, needle))){ found = 0;}
  211.         }
  212.         if(found == 1){ return itemOfList(i+nend - nstart + 1, haystack); }
  213.     }
  214.     return "";
  215. }
  216.  
  217. def listContainsList(haystack, needle, nstart, nend, andOr){
  218.     for(i=nstart;i<=nend;i++){
  219.         if(andOr == 1 && !(listContains(haystack, itemOfList(i, needle)))){ return 0;}
  220.         if(andOr == 2 && listContains(haystack, itemOfList(i, needle))){ return 1;}
  221.     }
  222.     if(andOr==1){return 1;}
  223.     if(andOr==2){return 0;}
  224. }
  225.  
  226. def @atomic process(a2){
  227.     a = a2;
  228.     // replace all punctuation with spaces so we can delete them away later
  229.     for(p = 1;p<=lengthOfList("punctuation");p++){
  230.         a = replace(a, itemOfList(p, "punctuation"), " ");
  231.     }
  232.     // split up words
  233.     deleteItemInList("all", "words");
  234.     split(join(a, " "), " ", "words");
  235.     //delete blanks
  236.     for(i=0;i<lengthOfList("words");i++){
  237.         item = itemOfList(i+1, "words");
  238.         if(item==""){ // luckily the way ApuC is implemented
  239.                       // length is constantly re-evaluated.
  240.                       // So we decrese i by 1
  241.             deleteItemInList(i+1, "words");
  242.             i--;
  243.         }
  244.     }
  245. }
  246.  
  247. def replace(haystack, needle, replacement){
  248.     newString = "";
  249.     for(i=1;i<=lengthOf(haystack);i++){
  250.         matches = substrMatches(haystack, needle, i);
  251.         if(matches == 1){
  252.             newString = join(newString, replacement);
  253.             i+=lengthOf(needle) - 1;
  254.         } else {
  255.             newString = join(newString, letterAt(i, haystack));
  256.         }
  257.     }
  258.     return newString;
  259. }
  260.  
  261. def @atomic split(string, delim, arrayName){
  262.     currentItem = "";
  263.     for(i=1;i<=lengthOf(string);i++){
  264.         delimFound = substrMatches(string, delim, i);
  265.         if(delimFound == 1){
  266.              i+=lengthOf(delim) - 1;
  267.              addToList(currentItem, arrayName);
  268.              currentItem = "";
  269.         } else {
  270.             currentItem = join(currentItem, letterAt(i, string));
  271.         }
  272.     }
  273. }
  274.  
  275. def substrMatches(haystack, needle, startIndex){
  276.     for(j=1;j<=lengthOf(needle);j++){
  277.         if(!(letterAt(j+startIndex - 1, haystack) == letterAt(j, needle))){
  278.             return 0;
  279.         }
  280.     }
  281.     return 1;
  282. }
  283.  
  284. def substr(string, startIndex, endIndex){
  285.     substr = "";
  286.     for(i=startIndex;i<=endIndex;i++){
  287.         substr = join(substr, letterAt(i, string));
  288.     }
  289.     return substr;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment