我正在尝试使某种语法在语音识别中起作用。
我的语法定义如下:
<rule id="showFlight">
<example>Show me Alaska Airlines flight number 2117</example>
<example>Where is US Airways flight 45</example>
<item>
<one-of>
<item>show me</item>
<item>where is</item>
</one-of>
</item>
<item>
<ruleref uri="#airline" />
<tag>out.Carrier = rules.airline;</tag>
</item>
flight
<item repeat="0-1">number</item>
<item repeat="1-">
<ruleref uri="#digit" />
<tag>out.Number = rules.digit;</tag>
</item>
</rule>
我的问题在于最后一个数字。我定义了语法中可以存在1个或多个数字,这是可行的。但是当我去提取OnSpeechRecognized回调中的值时,我只说了最后一位数字。
public override bool OnSpeechRecognized(object sender, Microsoft.Speech.Recognition.SpeechRecognizedEventArgs e)
{
String output = String.Format("Recognition Summary:\n" +
" Recognized phrase: {0}\n" +
" Confidence score {1}\n" +
" Grammar used: {2}\n",
e.Result.Text, e.Result.Confidence, e.Result.Grammar.Name);
Console.WriteLine(output);
// Display the semantic values in the recognition result.
Console.WriteLine(" Semantic results:");
//Console.WriteLine(e.Result.Semantics["Flight"].Value);
foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics["ShowFlight"])
{
Console.WriteLine(" {0} is {1}",
child.Key, child.Value.Value ?? "null");
}
Console.WriteLine();
...
或者,更直接地:
e.Result.Semantics["ShowFlight"]["Number"].Value.ToString()
如果我说“ 2 1 1 7”,那么[[Number]]中的唯一数字是7。同样,如果我说“ 4-5”,则返回的唯一数字是5。
如何提取航班号中所有所说的数字?
另外,是否可以加载一个秘密的内部语法,使我能够轻松识别“四五”和“四十五”?
最佳答案
您可以简单地用以下内容替换最后一个“ item”元素:
<tag>out.Number = ""</tag>
<item repeat="1-">
<ruleref uri="#digit" />
<tag>out.Number += rules.digit;</tag>
</item>
这会将所有识别的数字连接到
out.Number
。关于第二个问题,不幸的是,没有这样的“秘密内部语法”。您将必须自己编写代码。
关于c# - Microsoft Speech-识别和提取可变长度的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12503418/