以下程序是C#中使用Microsoft托管语音API的简单语音识别控制台应用程序,它只识别“是”和“否”。当我使用Srgs方法内联创建Srgs规则'MyRule'时,一切正常。但是,如果我将规则放在外部XML文件中并创建一个SrgsRuleRef来引用它,我总会得到错误:“无法解析导入语法的规则引用”。我尝试过更改各种参数,例如语言,媒体类型和语义标签格式,但似乎没有任何区别。
using System;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
namespace SpeechRecognitionConsole
{
class Program
{
static void Main(string[] args)
{
try
{
System.Globalization.CultureInfo culture =
new System.Globalization.CultureInfo("en-US");
SrgsRule RootRule = new SrgsRule("RootRule");
RootRule.Add(new SrgsRuleRef(
new Uri("c:\\projects.net\\speechrecognitionconsole\\MyGrammar.xml"), "MyRule"));
RootRule.Scope = SrgsRuleScope.Public;
SrgsDocument MyDocument = new SrgsDocument(RootRule);
MyDocument.Culture = culture;
Grammar g = new Grammar(MyDocument);
g.Name = ("MyGrammar");
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(culture);
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>
(recognizer_SpeechRecognized);
recognizer.LoadGrammar(g);
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine("Starting asynchronous recognition...");
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
}
}
}
语法文件“c:\ projects.net \ speechrecognitionconsole \ MyGrammar.xml”包含:
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" tag-format="semantics/1.0" version="1.0" xmlns="http://www.w3.org/2001/06/grammar">
<rule id="MyRule" scope="public">
<one-of>
<item>yes</item>
<item>no</item>
</one-of>
</rule>
</grammar>
答案 0 :(得分:0)
为了让今天使用Visual Studio 2013工作,我必须添加root =&#34; MyRule&#34;在2012年提出问题时,不确定相同的修复是否会有所帮助。
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" tag-format="semantics/1.0"
version="1.0" xmlns="http://www.w3.org/2001/06/grammar" root="MyRule">
<rule id="MyRule" scope="public">
<one-of>
<item>yes</item>
<item>no</item>
</one-of>
</rule>
</grammar>