Speech recognition: Result.Semantic.ContainsKey is always false when reading grammar from xml file
I am studying the Microsoft.Speech library. It is similar to the System.Speech library. I am stuck with the problem and I think it might be a bug.
I tried to use e.Result.Semantic.ContainsKey("DestinationCity")
to determine if it has a key or not. When I download a grammar from GrammarBuilder, I get true
for saying "I want to fly from New York to Chicago". Then I write the grammar to an xml file and load the grammar from that file again. This time when I say “I would like to fly from New York to Chicago,” I get false
. I don't know why this is happening.
Here is my code:
class Program
{
static void Main(string[] args)
{
//Create GrammarBuilder gb
Choices cities = new Choices(new string[] {
"Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append("I would like to fly from");
gb.Append(new SemanticResultKey("DepartureCity", cities));
gb.Append("to");
gb.Append(new SemanticResultKey("DestinationCity", cities));
//Put GrammarBuilder gb into xml file
SrgsDocument doc = new SrgsDocument(gb);
System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(@"C:\Visual Studio Projects\SpeechSemanticsTest\SpeechSemanticsTest\passwordGrammar.xml");
doc.WriteSrgs(writer);
writer.Close();
//Create SpeechRecognitionEngine
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
//Load Grammar from xml
sre.LoadGrammar(new Grammar(@"C:\Visual Studio Projects\SpeechSemanticsTest\SpeechSemanticsTest\passwordGrammar.xml"));
//load Grammar from GrammarBuilder
//sre.LoadGrammar(new Grammar(gb));
//Start Recognizing
sre.RecognizeAsync(RecognizeMode.Multiple);
Console.ReadKey();
}
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
Console.WriteLine(e.Result.Semantics.ContainsKey("DestinationCity"));
}
}
The generated xml file is located here:
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="root" tag-format="properties-ms/1.0" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions">
<rule id="_1" scope="private">
<one-of>
<item>Los Angeles</item>
<item>New York</item>
<item>Chicago</item>
<item>San Francisco</item>
<item>Miami</item>
<item>Dallas</item>
</one-of>
</rule>
<rule id="DepartureCity" scope="private">
<ruleref uri="#_1" />
</rule>
<rule id="DestinationCity" scope="private">
<ruleref uri="#_1" />
</rule>
<rule id="root" scope="private">
I would like to fly from<ruleref uri="#DepartureCity" />to<ruleref uri="#DestinationCity" />
</rule>
</grammar>
Note that there is "load grammar xml" and "load grammar from GrammarBuilder". You can do this as a comment to see the difference.
source to share