3

I'm studying the Microsoft.Speech library. It is similar to the System.Speech library. I'm stuck with a problem 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"));
    }
}

The created xml file is 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's "load grammar form xml" and "load grammar from GrammarBuilder". You can make either as comment to see the difference.

Ian
  • 31
  • 4
  • 1
    How about to share an xml file? ;) – Nikolay Shmyrev Nov 09 '14 at 07:34
  • I shared my xml file. – Ian Nov 09 '14 at 14:00
  • Maybe it doesn't assign tags because you do not make the rules public scope? Like an example here. http://msdn.microsoft.com/en-us/library/hh362824(v=office.14).aspx. They all use rule.Scope = SrgsRuleScope.Public; – Nikolay Shmyrev Nov 09 '14 at 16:32
  • @NikolayShmyrev Thank you for your reply. The thing that I do not understand is that the xml was auto-generated by a working grammar builder. So, technically, it should work if i load grammar from that xml, but it didn't. – Ian Nov 09 '14 at 17:55
  • Well, that's for sure a bug which you can report to Microsoft. – Nikolay Shmyrev Nov 09 '14 at 19:18

1 Answers1

0
<rule id="_1" scope="private">

All your scopes are private in your XML. Set them to public.

Lesley Gushurst
  • 664
  • 4
  • 15