1

我目前正在使用AnnyangJS Library构建类似Siri 的虚拟助手。目前,我必须在下面演示的代码示例中输入每个命令。由于这个项目包含相当数量的输入/输出命令,是否有一种方法可以使用数据库(或类似的东西)来自动化这个过程,所以我可以简单地构建一个命令系统,而无需手动输入每个单人。

我搜索了 Google、SO 和其他几个来源,但没有找到任何关于这种可能性的信息。任何关于如何实现这一点的信息将不胜感激。

可以在此处的 JS Fiddle查看项目的当前阶段。

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

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>

4

1 回答 1

0

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

于 2016-02-21T12:34:38.017 回答