Advertisement
FlyFar

A Simple HTML Chat Bot that Learn Input

Oct 21st, 2021
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 8.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="utf-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <link rel="stylesheet" media="screen" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css">
  8.     <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  9.     <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
  10.  
  11.  
  12.  
  13.     <title>Bot Speak</title>
  14.  
  15.     <!--  -->
  16.  
  17.     <style>
  18.         .title {
  19.             background-color: #c2454c;
  20.             color: #021119;
  21.             text-align: center;
  22.             padding: 100px;
  23.             -webkit-background-size: cover;
  24.   -moz-background-size: cover;
  25.   -o-background-size: cover;
  26.   background-size: cover;
  27.            
  28.            
  29.         }
  30.        
  31.         .letsSpeak {
  32.             background-color: #021119;
  33.             color: white;
  34.             padding-top: 50px;
  35.             padding-bottom: 50px;
  36.             padding-left: 100px;
  37.             padding-right: 100px;
  38.         }
  39.        
  40.         .knowledge {
  41.             background-color: #004e64;
  42.             color: white;
  43.             padding-top: 50px;
  44.             padding-bottom: 50px;
  45.             padding-left: 100px;
  46.             padding-right: 100px;
  47.         }
  48.        
  49.         .teach {
  50.             background-color: #16a8c7;
  51.             color: white;
  52.             padding-top: 50px;
  53.             padding-bottom: 50px;
  54.             padding-left: 100px;
  55.             padding-right: 100px;
  56.         }
  57.        
  58.         a {
  59.             color: black;
  60.            
  61.         }
  62.         button {
  63.             background-color: #c2454c;
  64.         }
  65.     </style>
  66.  
  67. </head>
  68.  
  69. <body>
  70.    
  71.     <div class="title" id="title">
  72.     <h2>Hi, I'm a chat bot.<br/> Say hi below</h2>
  73.     <a id="icon" href="#letsSpeak"><i class="fa fa-angle-double-down" style="font-size:48px"></i></a>
  74.     </div>
  75.  
  76.     <div class="letsSpeak" id="letsSpeak">
  77.         <h2>Talk to me!</h2>
  78.         <p>Click this button and say 'Hello There!'</p>
  79.         <button type="button" class="btn btn-danger" id="send" onclick="annyang.start()" value="Reset Form">
  80.             <!--adds ear icon to button-->
  81.             <i class="material-icons">hearing</i></button>
  82.  
  83.         <!--machine response in output-->
  84.         <div id="clickedChat">
  85.             <div id="output"></div>
  86.         </div>
  87.     </div>
  88.  
  89.     <div class="knowledge">
  90.         <h2>Chat with me!</h2>
  91.  
  92.         <input type="text" class="form-control" placeholder="Talk to me..." id="userInput">
  93.         <br/>
  94.         <button type="button" class="btn btn-danger" id="send" onclick="whatSaid()" value="Reset Form">Chat</button>
  95.         <br/>
  96.         <div class="response" id="botRespo"></div>
  97.     </div>
  98.  
  99.  
  100.  
  101.     <div class="teach">
  102.         <h2>Teach me things!</h2>
  103.         <input type="text" class="form-control" placeholder="When you say..." id="helpLearn">
  104.         <input type="text" class="form-control" placeholder="How should I respond?" id="aboutIt">
  105.         <br/>
  106.         <button type="button" class="btn btn-danger" id="send" onclick="teachMe()">Teach me!</button>
  107.         <br/>
  108.         <div id="learnlog"></div>
  109.  
  110.     </div>
  111.  
  112.  
  113.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  114.     <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
  115.     <script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.3.0/annyang.min.js"></script>
  116.  
  117.     <script>
  118.         function init() {
  119.             //init function, calls local storage which has a stringify'd //array called facts
  120.             //need to combine facts (string) with vocab array
  121.             savedValue = localStorage.getItem('facts');
  122.  
  123.         }
  124.  
  125.  
  126.  
  127.         //annyang, need to figure out how to make multiple commands run
  128.  
  129.         var commands = {
  130.             // annyang will capture anything after a splat (*) and pass it to the function.
  131.             // e.g. saying "Show me Batman and Robin" is the same as calling showFlickr('Batman and Robin');
  132.             'show me *tag': showFlickr,
  133.  
  134.             // A named variable is a one word variable, that can fit anywhere in your command.
  135.             // e.g. saying "calculate October stats" will call calculateStats('October');
  136.             'calculate :month stats': calculateStats,
  137.  
  138.             // By defining a part of the following command as optional, annyang will respond to both:
  139.             // "say hello to my little friend" as well as "say hello friend"
  140.             'say hello (to my little) friend': greeting
  141.         };
  142.  
  143.         var showFlickr = function (tag) {
  144.             var url = 'http://api.flickr.com/services/rest/?tags=' + tag;
  145.             $.getJSON(url);
  146.  
  147.         }
  148.  
  149.         var calculateStats = function (month) {
  150.             $('#stats').text('Statistics for ' + month);
  151.         }
  152.  
  153.         var greeting = function () {
  154.             $('#greeting').text('Hello!');
  155.         }
  156.  
  157.  
  158.         if (annyang) {
  159.             // Let's define our first command.
  160.             //First the text we expect,
  161.             //and then the function it should call
  162.             var commands = {
  163.                 'Hello there': function () {
  164.                     var content = ' '
  165.                     content += '<br/>'
  166.                     content += 'Hi, how are you??'
  167.                     content += '<br/>'
  168.                     content += 'I\'m a young bot called Larry.'
  169.                     content += '<br/>'
  170.                     content += 'Unfortunately, my ears aren\'t great (yet), so until I learn more this is all I can hear/say.'
  171.                     content += '<br/>'
  172.                     content += 'But, you can type in the boxes below to chat with me more!';
  173.  
  174.                     console.log(commands);
  175.                     document.getElementById('output').innerHTML = content;
  176.  
  177.                 }
  178.             }
  179.  
  180.         };
  181.  
  182.         // Add our commands to annyang
  183.         annyang.addCommands(commands);
  184.  
  185.  
  186.         //need to turn off my microphone
  187.     </script>
  188.  
  189.  
  190.  
  191.  
  192.     <script>
  193.         function gettingJSON() {
  194.             document.write("jquery loaded");
  195.             $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=London&APPID=ee6596241130f193adf1ba90e625cc10", function (json) {
  196.                 document.write(JSON.stringify(json));
  197.             });
  198.         }
  199.  
  200.         //Checks for enter press
  201.         $(document).keypress(function (e) {
  202.             if (e.which == 13) {
  203.                 whatSaid();
  204.  
  205.             }
  206.         });
  207.  
  208.  
  209.         //defines date so we can call later
  210.         var now = new Date();
  211.  
  212.         //this is what the bot knows
  213.         var vocabulary = [
  214. ['hi', 'hello'],
  215. ['what is your name', 'My name is Larry, Im a young bot'],
  216. ['how are you', 'Im great, How are you'],
  217. ['where are you', 'Boulder, Colorado'],
  218. ['where is that', 'Near the Rocky Mountains in the United States'],
  219. ['where am i', 'you\'re also in Boulder'],
  220. ['who created you', 'You did!'],
  221. ['what is a bot', 'I\m a bot'],
  222. ['what is your favorite movie', 'Harry Potter perhaps?'],
  223. ['what day is it', now],
  224. ['where am i from', 'Madison Wisconsin'],
  225. ['i love you', 'Aw, thanks. I love you too!'],
  226. ['who is the coolest dog', 'Nora of course!'],
  227. ['would you like to go mountain bicycling', 'I\'m a computer silly'],
  228.  
  229.  
  230. ];
  231.  
  232.  
  233.  
  234.  
  235.         function whatSaid() {
  236.  
  237.  
  238.  
  239.             //this creates a variable input that is the userInput
  240.             var input = document.getElementById('userInput');
  241.  
  242.             //this defines a variable userSaid (which is what the user said) //that gets the value of the Input variable
  243.             var userInput = input.value;
  244.  
  245.  
  246.             //this strips the punctuation and the spaces from user input
  247.             //punctuation marks I want to strip    
  248.             var punctRE = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
  249.             //fixes the spaces after I remove a bunch of punctuation
  250.             var spaceRE = /\s+/g;
  251.  
  252.             var str = userInput;
  253.             //use string replace to strip the text
  254.             var saidStripped = str.replace(punctRE, '').replace(spaceRE, ' ');
  255.             //convert to lower case
  256.             var input = saidStripped.toLowerCase()
  257.  
  258.  
  259.  
  260.             var notUnderstood = "I'm sorry, I don\'t understand";
  261.  
  262.  
  263.             for (var i = 0; i < vocabulary.length; i++) {
  264.                 console.log('I am comparing ' + vocabulary[i][0] + ' with ' + input);
  265.                 if (vocabulary[i][0] == input) {
  266.                     var respo = vocabulary[i][1];
  267.                     document.getElementById('botRespo').innerHTML = respo;
  268.                     return;
  269.                     console.log(vocabulary[i][1]);
  270.  
  271.                 } else {
  272.                     document.getElementById('botRespo').innerHTML = notUnderstood;
  273.                 }
  274.             }
  275.  
  276.             //clears the textbox
  277.             //document.getElementById('userInput').reset;
  278.         }
  279.  
  280.         //try to write this to local storage
  281.         //add a teach me bar
  282.         //this is a function that takes input
  283.         function teachMe() {
  284.  
  285.             var input1 = document.getElementById('helpLearn');
  286.             var input2 = document.getElementById('aboutIt');
  287.  
  288.             var learn_input = input1.value
  289.             console.log(learn_input);
  290.             //strip punctuation and spaces
  291.             //learn_input becomes learnStripped
  292.  
  293.             var punctuateRE = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
  294.             var space_RE = /\s+/g;
  295.             var str2 = learn_input;
  296.             var learnStripped = str2.replace(punctuateRE, '').replace(space_RE, ' ');
  297.             var learned = learnStripped.toLowerCase()
  298.  
  299.  
  300.  
  301.             var about_input = input2.value
  302.             console.log(about_input);
  303.  
  304.             document.getElementById('learnlog').innerHTML = learn_input + ', ' + about_input
  305.  
  306.             var learnArr = [learnStripped, about_input];
  307.             console.log(learnArr);
  308.  
  309.             vocabulary.push(learnArr);
  310.             console.log(vocabulary);
  311.  
  312.  
  313.  
  314.             //var learnArrString = learnArr.toString();
  315.             //console.log(learnArrString);
  316.  
  317.             var vocabString = vocabulary.toString();
  318.             console.log(vocabString);
  319.             //first, need to stringify my array before saving it to local storage
  320.  
  321.             localStorage.setItem('facts', vocabString);
  322.  
  323.  
  324.         }
  325.  
  326.         init();
  327.     </script>
  328.  
  329. </body>
  330.  
  331. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement