I'm studying Microsoft.Speech library now. It is similar to System.Speech library. I'm stuck with a problem here and I think this might be a bug.
I was trying to use "e.Result.Semantic.ContainsKey("DestinationCity") to determine if it has the key or not. When I load grammar from GrammarBuilder, I get a "true" for saying "I would like to fly from New York to Chicago". Then I write the grammar into an xml file and load grammar from that file again. This time, when i say "I would like to fly from New York to Chicago", I get a "false". I don't know why this happens.
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"));
}
}
Note that, there's "load grammar form xml" and "load grammar from GrammarBuilder". You can make either as comment to see the difference.
Please help me with this! I'll be really appreciated!
No comments:
Post a Comment