我想补充一点,每当它识别出“搜索X”时它就会搜索“X”,但我不知道如何将其添加到语法中,或者如何用我的开关来做这样的事情声明。
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是什么。
提前致谢!
你必须制作一个更复杂的语法。您选择的语法规则类型是选择规则。它并不完全适合您想要捕获关键字后面的内容。要捕获以下内容,必须使用SemanticResultKey规则。这里解释:https://docs.microsoft.com/en-us/dotnet/api/system.speech.recognition.semanticresultkey?view=netframework-4.7.2
该示例完全匹配您想要的内容:它从表达式“我的密码是......”中提取密码