locked
how to load/unload grammar files dynamically at run time? RRS feed

  • Question

  • Hi,

    The speech recognition engine takes at least one grammar to be loaded at start up.

    Is it possible to unload the existing grammar file and load a new file without stopping and restarting the engine.

    sre = new SpeechRecognitionEngine();
    doc = new SrgsDocument(_grammarFilePath);
    sre.LoadGrammar(new Grammar(doc));

    I have a requirement to change the grammar file at run time (through a button click/drop down).

    Can anyone please help in this regard.

    Thanks,

    Bharat.

    Friday, April 12, 2013 10:14 AM

Answers

  • Hi Bharat,

    //LOAD

    var myGrammar = new Grammar(doc);
    sre.LoadGrammar(myGrammar);

    //UNLOAD
    sre.UnloadGrammar(myGrammar);

    If you want unload all grammars you can use the UnloadAllGrammars Method.


    [TIPS]
    Sometime the switch of grammars require minutes! In order to improve the time switch (couple of seconds instead of minutes) please consider this snippet extracted from this URL

    recognizer.RecognizerUpdateReached +=
           delegate(object s, RecognizerUpdateReachedEventArgs args) {
               GrammarRequest request = args.UserToken as GrammarRequest; //cast and check if incoming type is a request
               if (request != null) { // We know this is a Grammar request now
                   if (request.Grammar == null)
                       throw new ArgumentException("Invalid grammar used.");
               }
               switch (request.RequestType) {
    
                   case GrammarRequestType.LoadGrammar:
                       RemoveDuplicateGrammar(request.Grammar);
                       _recognizer.LoadGrammar(request.Grammar);
                        break;
                   case GrammarRequestType.UnloadGrammar:
                       _recognizer.UnloadGrammar(request.Grammar);
                       UpdateGrammarTree(_grammarTreeView, _recognizer);
                       //DisplayGrammarUnload(request.Grammar);
                       break;
               }
    
           };
    
    }

    In nutshell, when the SpeechRecognitionEngine is running, the changes of a grammar need to occur when the engine is ready for the changes.

    • Marked as answer by ykbharat Tuesday, April 30, 2013 4:02 AM
    Friday, April 26, 2013 1:08 AM