C#获取SpeechRecognitionEngine识别句子

C# Get SpeechRecognitionEngine to recognize sentences

如果我给微软的 SpeechRecognitionEngine 一个 Grammar,它只会识别该语法中的单个选项,而不是选项的组合。

是否有任何内置方法让它一次识别选项组合而不是单个选项,而不将该组合添加到选项中。

----例如让它识别"Open Firefox",而不是"Open",然后"FIrefox"

下面的代码

namespace SpeachTest
{
    public class MainClass
    {
        static void Main()
        {
        MainClass main = new MainClass(); 
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));

            Choices choiceList = new Choices();

            choiceList.Add(new string[]{"Hello", "Open", "Close", "Firefox"} );
            Grammar grammar = new Grammar(choiceList);

            sre.SpeechRecognized += main.sreEvent;
            sre.SetInputToDefaultAudioDevice();
            sre.LoadGrammar(grammar);

            sre.RecognizeAsync(RecognizeMode.Multiple);
            while(true){
            Console.ReadLine();
            }
        }

        void sreEvent(Object sender, SpeechRecognizedEventArgs e){
            Console.WriteLine(e.Result.Text);
    }
}

}

我知道 DictationGrammar 会这样做,但我想避免使用,因为它的准确性较低。

GrammarBuilder builder = new GrammarBuilder();
Choices choice = new Choices (new string[] {"Open", "Close"});
builder.Append(choice);
builder.Append("Firefox");
Grammar grammar = new Grammar(builder);

创建一个 Grammar 来处理 "Open Firefox" 和 "Close Firefox."

现在如果你在想

Well that means I need to create tons of boiler plate code to create all my Grammars since I want to be able to recognize hundreds of phrases like "Open Notepad," "Close Chrome," and "Minimize Visual Studio."

您可以创建某种类型的工厂,其中 returns Grammar 具有某些默认选项,例如打开、关闭、最小化、最大化、聚焦等...