Search code examples
sapi

How to set OPT programmatically with MS SAPI


Given the following which can be loaded into MS SAPI 5.1:

<GRAMMAR LANGID="409"> 
    <RULE NAME="top rule" TOPLEVEL="ACTIVE"> 
        <OPT>hello</OPT> 
        <P>my name is fred</P> 
    </RULE> 
</GRAMMAR>

How can I do the same programmatically, specifically with regard to the optional element.

I would guess it is done here:

state.AddWordTransition(nextState, "hello", " ", SpeechGrammarWordType.SGLexical, s, id, ref propValue, 1F);

...and it is probably the propValue. But what is the syntax (e.g, propValue="OPT=true" - does not work of course)

Thanks!


Solution

  • Optional words need an epsilon (empty) transition to the next state, so add:

    state.AddWordTransition(nextState, NULL, NULL, SpeechGrammarWordType.SGLexical, s, id, ref propValue, 1F);
    

    to add the epsilon transition.