使用 .net 语音 API 在 SRGS 语法中引用外部规则时出错
以下程序是一个简单的 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>
The following program is a simple speech recognition console application in C# using the Microsoft managed speech API that just recognizes the words 'yes' and 'no'. When I create the Srgs rule 'MyRule' inline using the Srgs methods everything works Ok. However if I put the rule in an external XML file and create an SrgsRuleRef to reference it I always get the error: "A rule reference to an imported grammar cannot be resolved." I have tried changing various parameters such as the language, media type and semantic tag format but nothing seems to make any difference.
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);
}
}
}
The grammar file "c:\projects.net\speechrecognitionconsole\MyGrammar.xml" contains:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了今天使用 Visual Studio 2013 使其正常工作,我必须添加 root="MyRule" 不确定在 2012 年提出问题时相同的修复是否会有帮助。
To get this working today using Visual Studio 2013 I had to add root="MyRule" not sure if the same fix would have helped when question was asked in 2012.