dimanche 20 mars 2016

My Annyang Calculator

so I am currently developing a Siri or JARVIS like web app. One of the functions I would like it to have is to search Google, and bring back the results. I am using annyang to handle the voice recognition. But that is besides the point. Every other function that I have written has worked, except this one. To get this, I searched around on Google's API pages, and found this HTML document that does exactly that, even though Google has depreciated it. Here is the document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://ift.tt/mOIMeg">
<html xmlns="http://ift.tt/lH0Osb">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Hello World - Google  Web Search API Sample</title>
    <script src="http://ift.tt/JuZcy0"
        type="text/javascript"></script>
    <script language="Javascript" type="text/javascript">
    //<!
    google.load('search', '1');

    function OnLoad() {
      // Create a search control
      var searchControl = new google.search.SearchControl();

      // Add in a full set of searchers
      var localSearch = new google.search.LocalSearch();
      searchControl.addSearcher(localSearch);
      searchControl.addSearcher(new google.search.WebSearch());
      searchControl.addSearcher(new google.search.VideoSearch());
      searchControl.addSearcher(new google.search.BlogSearch());
      searchControl.addSearcher(new google.search.NewsSearch());
      searchControl.addSearcher(new google.search.ImageSearch());
      searchControl.addSearcher(new google.search.BookSearch());
      searchControl.addSearcher(new google.search.PatentSearch());

      // Set the Local Search center point
      localSearch.setCenterPoint("New York, NY");

      // tell the searcher to draw itself and tell it where to attach
      searchControl.draw(document.getElementById("searchcontrol"));

      // execute an inital search
      searchControl.execute("Google");
    }
    google.setOnLoadCallback(OnLoad);

    //]]>
    </script>
  </head>
  <body>
    <div id="searchcontrol">Loading</div>
  </body>
</html>

But then there is my document. What happens is, it hears the request, then deletes the , and I have absolutely no idea why. All I changed from the document is where the Google result is put and what the item searches for. Please help me try and figure out what is wrong. Here is all the code that you need. Note: This for some reason does not work in the Stack Overflow window, at least for me. Copy and paste this into Cloud9 or something. It works there.

  var commands = {
    'hello': greet,
    'hello there': greet,
    'hi': greet,
    'hi chad': greet,
    'hello chad': greet,
    'hey': greet,

    'goodbye': goodbye,

    'what is *number1 + *number2': addition,
    "what's *number1 + *number2": addition,

    'what is *number1 - *number2': subtraction,
    "what's *number1 - *number2": subtraction,

    'what is *number1 times *number2': multiplication,
    "what's *number1 times *number2": multiplication,
    'what is *number1 multiplied by *number2': multiplication,
    "what's *number1 multiplied by *number2": multiplication,
    'what is *number1 x *number2': multiplication,
    "what's *number1 x *number2": multiplication,

    'what is *number1 divided by *number2': division,
    "what's *number1 divided by *number2": division,
    'what is *number1 over *number2': division,
    "what's *number1 over *number2": division,

    'what is the time': getTime,
    "what's the time": getTime,
    'what time is it': getTime,

    "get me *tag": googleSearch,

  };

  var onOrOff = commands;

  function greet() {
    var greetings = [
      "Hello There!",
      "Chad's my name, helping you is my game!",
      "Hello!",
      "Hi!",
      "Hola!"
    ];
    chadRespond(greetings[Math.floor(Math.random() * greetings.length)]);
  }

  function goodbye() {
    chadRespond("Goodbye! Chad signin' out. Peace!");
    setTimeout(function() {
      window.close();
    }, 2000);
  }

  function addition(number1, number2) {
    chadRespond("The answer is " + (Number(number1) + Number(number2)));
  }

  function subtraction(number1, number2) {
    chadRespond("The answer is " + (Number(number1) - Number(number2)));
  }

  function multiplication(number1, number2) {
    chadRespond("The answer is " + (Number(number1) * Number(number2)));
  }

  function division(number1, number2) {
    chadRespond("The answer is " + (Number(number1) / Number(number2)));
  }

  function getTime() {
    var date = new Date();
    var m = "AM";
    var hour = date.getHours();
    if (hour > 12) {
      hour = hour - 12;
      m = "PM";
    } else if (hour == 12) {
      hour = hour;
      m = 'PM';
    }
    var minute = date.getMinutes();
    if (minute < 10) {
      minute = "0" + minute;
    }
    chadRespond("It is currently " + hour + ":" + minute + " " + m);
  }

  function googleSearch(tag) {
      //<!
      makeGoogleFrame();

      google.load('search', '1');

      function OnLoad() {
        // Create a search control
        var searchControl = new google.search.SearchControl();

        // Add in a full set of searchers
        var localSearch = new google.search.LocalSearch();
        searchControl.addSearcher(localSearch);
        searchControl.addSearcher(new google.search.WebSearch());
        searchControl.addSearcher(new google.search.VideoSearch());
        searchControl.addSearcher(new google.search.BlogSearch());
        searchControl.addSearcher(new google.search.NewsSearch());
        searchControl.addSearcher(new google.search.ImageSearch());
        searchControl.addSearcher(new google.search.BookSearch());
        searchControl.addSearcher(new google.search.PatentSearch());

        // Set the Local Search center point
        localSearch.setCenterPoint("New York, NY");

        // tell the searcher to draw itself and tell it where to attach
        searchControl.draw(document.getElementsByClassName("googleSearchFrame")[0]);

        // execute an inital search
        searchControl.execute(tag);
      }

      google.setOnLoadCallback(OnLoad);

      var googleFrameExists = false;

      function makeGoogleFrame() {
        var border = $("<div>").addClass("googleSearchBorder");
        var searchFrame = $("<div>").addClass("googleSearchFrame");
        var exit = $("<a>").text("X").addClass("googleFrameX").attr("href", "#");
        var container = $("<div>").addClass("container");
        googleFrameExists = true;

        $(exit).click(function() {
          $(border).remove();
          $(searchFrame).remove();
          $(container).remove();
          $(exit).remove();
          googleFrameExists = false;
        });
      }

      function chadRespond(response) {
        $("#chadResponse p").text(response);
      }


      annyang.addCommands(commands);

      $("#micLink").click(function() {
        annyang.start();
        $("#micLink > div").addClass("microphoneActive");
      })
#microphone {
  width: 100px;
  height: 100px;
  background-color: #8AB3FF;
  position: relative;
  top: 0px;
  text-align: center;
}
#microphone img {
  height: 90px;
  position: relative;
  top: 5px;
}
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.2.1/annyang.min.js"></script>
<script src="http://ift.tt/JuZcy0" type="text/javascript"></script>
<script type="text/javascript" src="app.js"></script>
<a id="micLink" href="#">
  <div class="button" id="microphone">
    <img src="http://ift.tt/1VrKSZx" />
  </div>
</a>



Aucun commentaire:

Enregistrer un commentaire