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>