Kim Major
Kim Major

Reputation: 3711

How to add words to an already loaded grammar using System.Speech and SAPI 5.3

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.

Upvotes: 4

Views: 6052

Answers (3)

Eric Brown
Eric Brown

Reputation: 13932

In native SAPI, I'd use ISpGrammarBuilder2::AddTextSubset().

Upvotes: 1

Conor OG
Conor OG

Reputation: 543

An alternative, if you have very large grammars, would be to use the dictation grammar option. There is a standard dictation grammar, but you could also specify your own. See http://msdn.microsoft.com/en-us/library/system.speech.recognition.dictationgrammar.aspx, and it's constructor.

You wouldn't update this. It contains all possible words.

Upvotes: 1

Conor OG
Conor OG

Reputation: 543

It sounds like you need to use some indirection, via the a grammar rule reference. This can be done with the GrammarBuilder.AppendRuleReference method. It might be easier to test out your grammars first with some SRGS grammar files.

The principle is that you load a main large grammar which has some references in it, to smaller user specific word lists grammars, which you would dynamically load.

See http://www.w3.org/TR/speech-grammar/#S2.2 for the srgs format, and http://msdn.microsoft.com/en-us/library/system.speech.recognition.grammarbuilder.appendrulereference.aspx for the programmatic version.

Upvotes: 1

Related Questions