我目前正在使用AnnyangJS Library构建Siri-Like虚拟助手。目前,我必须在下面演示的代码示例中输入每个命令。由于该项目包含大量的输入/输出命令。是否有一种方法可以使用数据库(或类似的东西)来自动执行此过程,所以我将能够简单地构建一个命令系统,而不必手动输入每个命令。我已经搜索了Google,stackoverflow和其他几个来源,但未找到有关此可能性的任何信息。任何有关如何实现这一目标的信息将不胜感激。

您可以在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>





谢谢。

最佳答案

您不能那样做,如果在if语句中为每个单词放置位置会花费很多时间,但是如果您坚持要那样做,请对数据库进行操作。
为了正确执行此操作,您将需要创建一个算法来解决您的问题。
这是iOs开发人员的文章,但可能会帮助您解决问题:
http://www.raywenderlich.com/60870/building-ios-app-like-siri

09-30 12:59
查看更多