How to add words to an already loaded grammar using System.Speech and SAPI 5.3
- by Kim Major
Given the following code,
Choices choices = new Choices();
choices.Add(new GrammarBuilder(new SemanticResultValue("product", "<product/>")));
GrammarBuilder builder = new GrammarBuilder();
builder.Append(new SemanticResultKey("options", choices.ToGrammarBuilder()));
Grammar grammar = new Grammar(builder) { Name = Constants.GrammarNameLanguage};
grammar.Priority = priority;
_recognition.LoadGrammar(grammar);
How can I add additional words to the loaded grammar? I know this can be achieved both in native code and using the SpeechLib interop, but I prefer to use the managed library.
Update: What I want to achieve, is not having to load an entire grammar repeatedly because of individual changes. For small grammars I got good results by calling
_recognition.RequestRecognizerUpdate()
and then doing the unload of the old grammar and loading of a rebuilt grammar in the event:
void Recognition_RecognizerUpdateReached(object sender, RecognizerUpdateReachedEventArgs e)
For large grammars this becomes too expensive.