AnnyangJS的输入/输出数据库

时间:2016-02-17 00:02:45

标签: javascript database html5 voice-recognition annyang

我目前正在使用AnnyangJS Library构建 Siri-Like 虚拟助手。目前我必须在下面演示的代码示例中输入每个命令。由于该项目包含大量的输入/输出命令。是否有办法使用数据库(或类似的东西)自动执行此过程,因此我只需构建一个命令系统,而无需手动输入每一个命令。我搜索了Google,堆叠溢出以及其他几个来源,但没有找到任何有关此可能性的信息。关于如何实现这一目标的任何信息都将受到极大的赞赏。

可以在JS Fiddle here.

查看项目的当前阶段

您也可以在此处直接运行该应用:

annyang.setLanguage('en-US');

function speakEnglish() {
  syeVoice = "US English Male";
  if (annyang) {
    // Let's define our first command. First the text we expect, and then the function it should call
    var hello = {
      'Hello': function() {

        document.getElementById("reply").innerHTML = "Hello!";
        responsiveVoice.speak('Hello!', syeVoice);

      }
    };
    var hi = {
      'hi': function() {

        document.getElementById("reply").innerHTML = "Hey There!";
        responsiveVoice.speak('Hey There!', syeVoice);
      }
    };
    var hey = {
      'hey': function() {

        document.getElementById("reply").innerHTML = "Hey There!";
        responsiveVoice.speak('Hey There!', syeVoice);
      }
    };
    var who_are_you = {

      'Who are you': function() {

        document.getElementById("reply").innerHTML = "I am Sye, an artificial intelligence.";
        responsiveVoice.speak('I am Sai. An artificial intelligence.', syeVoice);

      }
    };

    var how_are_you = {

      'how are you': function() {

        document.getElementById("reply").innerHTML = "I feel great! What about you? ";
        responsiveVoice.speak("I feel great! What about you? ", syeVoice);
      }
    };
    var i_feel_good = {

      'I feel (great) (good) (happy)': function() {

        document.getElementById("reply").innerHTML = "That's Wonderful!";
        responsiveVoice.speak("That's Wonderful!", syeVoice);
      }
    };

    var i_feel_bad = {

      'I feel (sad) (bad) (horrible)': function() {

        document.getElementById("reply").innerHTML = "I would empathize, but I dont have feelings.";
        responsiveVoice.speak("I would empathize ... but I dont have feelings. ", syeVoice);
      }
    };
    var switch_to_chinese = {

      'Switch to Chinese Mode': function() {

        document.getElementById("reply").innerHTML = "你好。";
        responsiveVoice.speak('你好', 'Chinese Female');
        speakChinese();


      }
    };
    var i_feel_good = {

      'I feel (great) (good) (happy)': function() {

        document.getElementById("reply").innerHTML = "That's Wonderful!";
        responsiveVoice.speak("That's Wonderful!", syeVoice);
      }
    };
    var bye = {

      '(good)bye': function() {

        document.getElementById("reply").innerHTML = "Goodbye!";
        responsiveVoice.speak("Goodbye", syeVoice);
      }
    };
    var play_classical_music = {

      'play classical music': function() {

        document.getElementById("reply").innerHTML = "Playing Classical Music..";
        document.getElementById('classical-music').play();
      }
    };
    var stop_classical_music = {

      'stop playing classical music': function() {

        document.getElementById("reply").innerHTML = " Music Stopped";
        document.getElementById('classical-music').pause();
      }
    };
    var play_jazz_music = {

      'play jazz music': function() {

        document.getElementById("reply").innerHTML = "Playing Jazz Music..";
        document.getElementById('jazz-music').play();
      }
    };
    var stop_jazz_music = {

      'stop playing jazz music': function() {

        document.getElementById("reply").innerHTML = " Music Stopped";
        document.getElementById('jazz-music').pause();
      }
    };
    var good_job = {

      'good job': function() {

        document.getElementById("reply").innerHTML = "Thanks!";
        responsiveVoice.speak("Thanks", syeVoice);
      }
    };
    annyang.addCallback('resultNoMatch', function() {
      document.getElementById("reply").innerHTML = "I did'nt seem to catch that. Could you please repeat.";
      responsiveVoice.speak("I did not seem to catch that. Could you please repeat.", syeVoice);
    });
    var thankyou = {

      'Thank you (sai)': function() {

        document.getElementById("reply").innerHTML = "You're Welcome!";
        responsiveVoice.speak("Your Welcome!", syeVoice); // It must be spelled incorrectly in order to Sye to be able to say it correctly. 
      }
    };
    var thanks = {

      'Thanks (sai)': function() {

        document.getElementById("reply").innerHTML = "You're Welcome!";
        responsiveVoice.speak("Your Welcome!", syeVoice); // It must be spelled incorrectly in order to Sye to be able to say it correctly. 
      }
    };

    // Add our commands to annyang
    annyang.addCommands(good_job);
    annyang.addCommands(stop_jazz_music);
    annyang.addCommands(play_jazz_music);
    annyang.addCommands(stop_classical_music);
    annyang.addCommands(play_classical_music);
    annyang.addCommands(hello);
    annyang.addCommands(hi);
    annyang.addCommands(hey);
    annyang.addCommands(who_are_you);
    annyang.addCommands(how_are_you);
    annyang.addCommands(switch_to_chinese);
    annyang.addCommands(i_feel_good);
    annyang.addCommands(i_feel_bad);
    annyang.addCommands(bye);
    annyang.addCommands(thankyou);
    annyang.addCommands(thanks);




    // Start listening. You can call this here, or attach this call to an event, button, etc.
    annyang.start();
  }
};

speakEnglish();
/* Resources */

@import url(https://fonts.googleapis.com/css?family=Raleway:300,200);
@import url(https://fonts.googleapis.com/css?family=Lato:300);

/* Styling */

body {
  text-align: center;
  background-color: #ecf0f1;
  color: #34495e;
  background-size: cover;
  background-repeat: no-repeat;
  font-weight: lighter;
}
#reply {
  font-family: 'Raleway', sans-serif;
  padding: 20px;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.0.0/annyang.min.js"></script>
<script src='https://code.responsivevoice.org/responsivevoice.js'></script>

<audio id="jazz-music">
  <source src="http://www.justjazznyc.com/sound/dreamer.mp3">
</audio>
<audio id="classical-music">
  <source src="http://www.forelise.com/media/fur_elise_valentina_lisitsa.mp3">
</audio>

<h1 id="reply"> Welcome </h1>

谢谢。

1 个答案:

答案 0 :(得分:0)

你不能这样做,if语句中的每个单词都需要很长时间。但如果你坚持以这种方式去做,那就用数据库来做。 为了正确地完成它,您需要创建一个算法来解决您的问题。 这是一篇文章,它是iOs dev,但可能会帮助你解决你的问题: http://www.raywenderlich.com/60870/building-ios-app-like-siri