C#语法和开关通配符

时间:2018-09-16 08:30:54

标签: c# switch-statement speech-recognition wildcard

我想补充一点,只要它识别出“搜索X”,就会去搜索“ X”,但是我不知道我该如何将其添加到语法中,或者如何做这样的事情和我的switch语句。

private void Form1_Load(object sender, EventArgs e)
{
    Choices commands = new Choices();
    commands.Add(new string[] { "hello", "start chrome", "search" });
    GrammarBuilder gBuilder = new GrammarBuilder();
    gBuilder.Append(commands);
    gBuilder.Culture = new System.Globalization.CultureInfo("en-GB");
    Grammar grammar = new Grammar(gBuilder);

    recEngine.LoadGrammarAsync(grammar);
    recEngine.SetInputToDefaultAudioDevice();
    recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
}

private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    switch (e.Result.Text)
    {
        case "hello":
            synthesizer.SpeakAsync("Hello! How are you doing today?");
            break;
        case "start chrome":
            Process.Start("http://www.google.com");
            break;
        case "search":
            SearchChrome("search");
            break;
    }
}

static void SearchChrome(string searchterm)
{
    Process.Start("https://www.google.com/search?q=" + searchterm);
}

因此,我想添加到语法“搜索X”和我的案例“搜索X”中,用它来搜索X是什么。

谢谢!

1 个答案:

答案 0 :(得分:1)

您必须编写更复杂的语法。您选择的语法规则的类型是选择之一。它不完全适合您想要捕获关键字之后的内容。要捕获以下内容,您必须使用SemanticResultKey规则。此处说明:https://docs.microsoft.com/en-us/dotnet/api/system.speech.recognition.semanticresultkey?view=netframework-4.7.2

该示例完全符合您的要求:它从表达式“ My password is .....”中提取密码。