如何在C#中将语法(规则)和听写(言论自由)与SpeechRecognizer混合

Lee*_*one 11 c# speech-recognition sapi voice-recognition

我非常喜欢Microsofts最新的语音识别(和SpeechSynthesis)产品.

http://msdn.microsoft.com/en-us/library/ms554855.aspx

http://estellasays.blogspot.com/2009/04/speech-recognition-in-cnet.html

但是我觉得在使用语法时我有点受限.

不要误解我的语法,语法识别确切地指出了要注意的单词/短语,但是如果我希望它能够识别出一些我没有理解的东西呢?或者我想解析一个半预定命令名和半随机字的短语?

例如..

情景A - 我说"谷歌[漏油事件]",我希望它用括号中的搜索结果打开谷歌,这可能是任何东西.

场景B - 我说"找到[曼彻斯特]",我想让它在谷歌地图或任何其他未预先确定的地方搜索曼彻斯特

我希望它知道'谷歌'和'定位'是命令,它是参数之后的东西(可能是任何东西).

问题:有没有人知道如何混合使用预先确定的语法(语音识别应该识别的单词)和不在预定语法中的单词?

代码片段..

using System.Speech.Recognition;

...
...

SpeechRecognizer rec = new SpeechRecognizer();
rec.SpeechRecognized += rec_SpeechRecognized;

var c = new Choices();
c.Add("search");

var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true; 

...
...

void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    if (e.Result.Text == "search")
    {
        string query = "How can I get a word not defined in Grammar recognised and passed into here!";

        launchGoogle(query);
    }
}

...
...


private void launchGoogle(string term)
{
    Process.Start("IEXPLORE", "google.com?q=" + term);
}
Run Code Online (Sandbox Code Playgroud)

tim*_*ror 5

您可以尝试这样的事情......它指定了已知命令的列表..但是也可以让您在之后使用开放式听写.它希望在开放式听写之前有一个命令..但是你可以反转这个...并追加th然而,通过在命令类型("")中添加一个空格,它也会让你直接听写部分.

Choices commandtype = new Choices();
commandtype.Add("search");
commandtype.Add("print");
commandtype.Add("open");
commandtype.Add("locate");

SemanticResultKey srkComtype = new SemanticResultKey("comtype",commandtype.ToGrammarBuilder());

 GrammarBuilder gb = new GrammarBuilder();
 gb.Culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
 gb.Append(srkComtype);
 gb.AppendDictation();

 Grammar gr = new Grammar(gb);
Run Code Online (Sandbox Code Playgroud)

然后在你的识别器上只使用结果文本等

private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    System.Console.WriteLine(e.Result.Text);

}
Run Code Online (Sandbox Code Playgroud)

You can add more choice options, and SemanticResultKeys to the structure to make more complex patterns if you wish. Also a wildcard (e.g. gb.AppendWildcard(); ).