语音识别中的选择

时间:2013-07-21 08:54:17

标签: c# speech-recognition

我正在基于语音识别从铁人创建类似于JARVIS的程序。在使用之前我做了一个:

case "Open facebook":
    JARVIS.Speak("Opening facebook");
    Process.Start("www.facebook.com");
    break;`

但现在我想创建搜索选项甚至玩。到目前为止,我为youtube搜索做了这个(一个语法,效果很好)但是当我做两个时:

public partial class Form1 : Form
{
    SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
    SpeechSynthesizer JARVIS = new SpeechSynthesizer();
    string QEvent;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Choices artists = new Choices(new string[] { "bullet-for-my-valentine-curses", "black-veil-brides-saviour", "three-days-grace-wake-up" });
        Choices search = new Choices(new string[] { "bill-gates" });

        GrammarBuilder findServices = new GrammarBuilder("Play");
        findServices.Append(artists);
        GrammarBuilder google = new GrammarBuilder("Look");
        google.Append("for");
        google.Append(search);

        // Create a Grammar object from the GrammarBuilder and load it to the recognizer.
        Grammar servicesGrammar = new Grammar(findServices);
        Grammar lookingGrammar = new Grammar(google);
        _recognizer.RequestRecognizerUpdate();
        _recognizer.LoadGrammarAsync(servicesGrammar);
        _recognizer.LoadGrammarAsync(lookingGrammar);

        // Add a handler for the speech recognized event.
        _recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);

        // Configure the input to the speech recognizer.
        _recognizer.SetInputToDefaultAudioDevice();

        // Start asynchronous, continuous speech recognition.
        _recognizer.RecognizeAsync(RecognizeMode.Multiple);
    }
    void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        JARVIS.Speak("Playing" + e.Result.Words[1].Text);
        Process.Start("http://www.youtube.com/results?search_query=" + e.Result.Words[1].Text);
        JARVIS.Speak("Searching" + e.Result.Words[2].Text + " " + e.Result.Words[3].Text);
    }

}

我说“播放”和歌曲的标题,然后打开选择(艺术家) 就我所说的。并且使用一种语法,但是当我创建两个时,就像在上面的代码中一样,当我说出某些内容时,程序会停止并显示错误。

{
    static class Program
    {

        // The main entry point for the application.
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1()); //this line show error
        }
    }
}

那么如何制作多个语法,一个用于谷歌搜索,一个用于维基百科,并保留一个?

1 个答案:

答案 0 :(得分:2)

我要借用Raymond Chen的心理调试天赋(tm)并说你的问题就在这里:

_recognizer.LoadGrammarAsync(servicesGrammar);
_recognizer.LoadGrammarAsync(lookingGrammar);

特别是,我怀疑识别器一次只能有一个异步语法加载。如果您将代码更改为

_recognizer.LoadGrammar(servicesGrammar);
_recognizer.LoadGrammar(lookingGrammar);

或将第二个LoadGrammarAsync调用放入onLoadGrammarCompleted处理程序,您的问题就会消失。

严重,您需要包含错误。