GotSpeech.NET

The online community for Microsoft Speech Server developers
Welcome to GotSpeech.NET Sign in | Join | Help
in Search
CBrekeke Software

help creating a grammar with semantics

Last post 12-09-2008, 9:36 PM by kcchesnut. 7 replies.
Sort Posts: Previous Next
  •  11-16-2008, 11:19 AM 7475

    help creating a grammar with semantics

    I'm dynamically creating a grammar to perform basic math.

    The manually created version follows, it loads in VS.NET and works fine using the grammar test tool.

    <grammar xml:lang="en-US" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions" tag-format="semantics-ms/1.0">
      <lexicon uri=""/>
      <rule id="Rule1" scope="public">
       <example> Hey, what is 2 plus 2 </example>
       Hey, what is
       <item>
        <ruleref uri="LibraryV1.grxml#integer" />
        <tag> $._x = $$._value </tag>
       </item>
       <one-of>
          <item>
           plus <tag> $._op = '+' </tag>
          </item>
          <item>
            minus <tag> $._op = '-' </tag>
          </item>
          <item>
           times <tag> $._op = '*' </tag>
          </item>
          <item>
           divided by <tag> $._op = '/' </tag>
          </item>
        </one-of>
       <item>
        <ruleref uri="LibraryV1.grxml#integer" />
        <tag> $._y = $$._value </tag>
       </item>
      </rule>
    </grammar>

    Now the hard part is trying to recreate it using System.Speech.

    First, i tried using GrammarBuilder :

    GrammarBuilder gb = new GrammarBuilder(SettingsImpl.GetInstance().Settings.TriggerName);

    gb.Append("What is ");

     

    GrammarBuilder gb1 = new GrammarBuilder();

    gb1.AppendRuleReference(strUri, "integer");

    SemanticResultKey srkX = new SemanticResultKey("x", gb1);

    gb.Append(srkX);

    Choices choices = new Choices();

    choices.Add("plus");

    choices.Add("minus");

    choices.Add("times");

    choices.Add("multiplied by");

    choices.Add("divided by");

    SemanticResultKey srk = new SemanticResultKey("operand", choices);

    gb.Append(srk);

    GrammarBuilder gb2 = new GrammarBuilder();

    gb2.AppendRuleReference(strUri, "integer");

    SemanticResultKey srkY = new SemanticResultKey("y", gb2);

    gb.Append(srkY);

    Grammar grammar = new Grammar(gb);

    return grammar;

    That gives the exception 'a rule reference to an imported grammar cannot be resolved'. Ok, I have never been able to successfully get AppendRuleReference to work ... and I have no clue what I could be doing wrong.

    Since there is no other method to combine the SRGS and GrammarBuilder world, the next step is to try and create a SrgsDocument.

    string codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;

    string strUri = Path.GetDirectoryName(codeBase);

    strUri = strUri.Replace("file:\\", String.Empty);

    strUri = strUri + @"\LibraryV1.grxml";

    Uri uri = new Uri(strUri);

    SrgsDocument sDoc = new SrgsDocument();

    sDoc.Language = "en-US";

    sDoc.Mode = SrgsGrammarMode.Voice;

    SrgsRule sRule = new SrgsRule("default");

    sRule.Scope = SrgsRuleScope.Public;

    sDoc.Rules.Add(sRule);

    sDoc.Root = sRule;

    SrgsText text1 = new SrgsText(SettingsImpl.GetInstance().Settings.TriggerName + " What is");

    SrgsItem itemX = new SrgsItem();

    SrgsRuleRef ruleRefX = new SrgsRuleRef(uri, "integer");

    itemX.Elements.Add(ruleRefX);

    SrgsSemanticInterpretationTag tagX = new SrgsSemanticInterpretationTag("$.x = $$._value");

    itemX.Elements.Add(tagX);

    SrgsOneOf oneOf = new SrgsOneOf();

    oneOf.Items.Add(new SrgsItem("plus"));

    oneOf.Items.Add(new SrgsItem("minus"));

    oneOf.Items.Add(new SrgsItem("times"));

    oneOf.Items.Add(new SrgsItem("multiplied by"));

    oneOf.Items.Add(new SrgsItem("divided by"));

    oneOf.Items.Add(new SrgsItem("to the power of"));

    SrgsItem itemY = new SrgsItem();

    SrgsRuleRef ruleRefY = new SrgsRuleRef(uri, "integer");

    itemY.Elements.Add(ruleRefY);

    SrgsSemanticInterpretationTag tagY = new SrgsSemanticInterpretationTag("$.y = $$._value");

    itemY.Elements.Add(tagY);

    sRule.Add(text1);

    sRule.Add(itemX);

    sRule.Add(oneOf);

    sRule.Add(itemY);

    XmlTextWriter xtw = new XmlTextWriter("srgs.xml", Encoding.Default);

    xtw.Formatting = Formatting.Indented;

    sDoc.WriteSrgs(xtw);

    xtw.Close();

    Grammar grammar= new Grammar(sDoc, "default");

    return grammar; 

    It works fine until the semantic tags are added in, then that fails with 'the tag-format' for grammars that include NET semantics must be tag-format=properties-ms/1.0' ... and there is no property to set TagFormat.

    Is there no way to get either of the scenarios above to work?

    The hack I'm looking at right now is storing what works at XML, loading it in an XmlDocument, and adding the dynamic bits using XML (not the SRGS classes), and then creating a Grammar from that.

    Thanks, casey

  •  11-16-2008, 3:44 PM 7477 in reply to 7475

    Re: help creating a grammar with semantics

    My hack for working with the GRXML as XML instead of SRGS didnt work either. It failed because of the RuleRef. So I thought that maybe it had problems with the referenced grammar, so I loaded it and the rule name directly into a Grammar ... it worked. The grammar library.grxml is the original grammar library from Speech Server.

    And both grammars loaded and worked fine in Speech Servers grammar tool. i.e. the ruleRef wasnt a problem.

    So the only way I could get it to work is to work with the GRXML as XML, and combine all the libraries into a single library, so that no external references were made. ugly.

    I need to make more complex grammars, so I really need to get grammarBuilder.AppendRuleRef or SrgsDocument and SrgsRuleRef (above) to work. Anybody see what i'm doing wrong?

    Thanks, casey

  •  11-18-2008, 3:21 PM 7489 in reply to 7477

    Re: help creating a grammar with semantics

    hmm ... has anybody been able to successfully use System.Speech.GrammarBuilder.AppendRuleReference()?

    i can barely find any references on google or forums, and haven't found anything useful yet. maybe it works with .cfg but not .grxml? any other ideas?

    Thanks, casey

  •  11-19-2008, 4:32 PM 7503 in reply to 7489

    Re: help creating a grammar with semantics

    GrammarBuilder.AppendRuleReference with a .cfg grammar, instead of .grxml, did not work either.

    Also tried loading the referenced grxml into an SrgsDocument first, then getting the public rule (SrgsRule sRuleInt = srgsDocLib["integer"];), then adding that rule using the overload for SrgsRuleRef(sRuleInt) ... still failed. Was hoping getting access to the rule as an object first would work where the uri reference was failing.

    I'm entirely out of ideas how to get this to work ...

    Thanks, casey

     

  •  11-19-2008, 4:59 PM 7505 in reply to 7503

    Re: help creating a grammar with semantics

    just thought of a backwards workaround.

    if i load the external grammar into an SrgsDocument, then i can dynamically add a rule and reference a public rule that already exists in that grammar. the opposite failed : dynamically creating a new SrgsDocument, and then referencing a rule from an external grammar.

    casey

     

  •  11-26-2008, 1:11 PM 7606 in reply to 7505

    Re: help creating a grammar with semantics

    Hi Casey!

    I don't have an answer (and if you can't figure it out then nobody can) but I did want to say hi.


    Marshall Harrison
    MVP, Office Communications Server
    GotSpeech Consulting LLC
    Phone: (904) 222-8880
    the gotspeech guy
  •  11-26-2008, 4:24 PM 7619 in reply to 7606

    Re: help creating a grammar with semantics

    Hey Marshall!

    I updated to the latest MSDN Library to find a sample. Couple things different is they were passing a remote URL (not local file path), and they were also appending the rule name to the URL (#ruleName), instead of using the overload that separates the grammarl URL and the ruleName. But I haven't tried either of those yet ... instead i've been using the workaround from my previous comment, and that has been working fine.

    Cheers, casey

  •  12-09-2008, 9:36 PM 7694 in reply to 7619

    Re: help creating a grammar with semantics

    Finally retried AppendRuleReference and it worked.

    The changes I made were to use the full path to the grammar (e.g. c:\dev\...\grammar.cfg) and then prepend the rule name with the pound symbol.

     EDIT: spoke too soon. Once my grammar got more complex (e.g. SemanticItems), the above stopped working. Not sure why.

    casey

View as RSS news feed in XML