Loading Grammar From URI For Speech Recognition In WP8


This article explains how to load the grammar from a URI for our speech recognition engine. The speech recognition engine uses its grammar to recognize the speech. We have various ways to add the grammar in our speech recognition engine. One way was to use a list of strings. In that method we created the grammar in a list of strings. That list contained all our words and phrases that we wanted to recognize. In case of lists, everything was static and we did by it using the method "AddGrammarFromList()" . I already explained that way of adding grammar in my previous article so I'll not explain it more here. The following is the code snippet from that article.

 private async void strtClick(object sender, RoutedEventArgs e)


      -->   List<string> sampleGrammar = new List<string>();





            SpeechRecognizerUI mic = new SpeechRecognizerUI();

      -->   mic.Recognizer.Grammars.AddGrammarFromList("demo",sampleGrammar);

      -->   mic.Recognizer.Grammars["demo"].Enabled = true;



            SpeechRecognitionUIResult res = await mic.RecognizeWithUIAsync();

            if (res != null)


                MessageBox.Show("Text Input: "+res.RecognitionResult.Text+"\nGrammer Used: "+res.RecognitionResult.RuleName);




Now moving ahead let's discuss another method of adding grammar to our speech engine.

This article exlains the following:

  1. Adding Grammar from a URI
  2. Working with an online grammar

Adding Grammar From URI

Before we proceed in detail let's consider the need for this and when it will be useful. Imagine a situation in which you want to power your app by a speech recognition feature but the problem is you don't know the grammar completely or it's not complete yet or it continues to change or you may want to change it in the future. In all the preceding cases if we use any of the static methods of adding grammar then our app will be stuck whenever we change the grammar. To overcome this we can provide an update for our app but imagine if you changed your grammar ten times a week. Now we can see that it's not feasible to rely on static grammar methods. What else we can do?

The answer for that situation is to load a new updated grammar every time our speech engine is accessed. This is what the speech recognition method "LoadGrammarFromUri" does. It always loads a grammar from a URI. So to update the grammar we just need to change the grammar file at the server and the rest of our application will be done automatically.

Working With Online Grammar

Now we know the need for loading a grammar from a server or from a URI. Let's see it in action.

C# Code Behind

        private async void strtClick(object sender, RoutedEventArgs e)


            SpeechRecognizerUI mic = new SpeechRecognizerUI();

            Uri grammarUri = new Uri(@"ms-appx:///OrderPizza.grxml", UriKind.Absolute);  // file is included in project

            mic.Recognizer.Grammars.AddGrammarFromUri("demo", grammarUri);

            SpeechRecognitionUIResult res = await mic.RecognizeWithUIAsync();

            if (res != null)


                MessageBox.Show("Text Input: " + res.RecognitionResult.Text + "\nGrammer Used: " + res.RecognitionResult.RuleName);




Also an important point to note is that the method accepts the URI of a valid SRGS file. The file can be included in an app or loaded from a server. In the case of a wrong file type or syntax errors in the file, the method will throw a InvalidType exception.


This is how to load a grammar file from a server but one thing that one should always remember is it's not possible to use multiple grammars at a time. So if you are loading your own grammar then the dictation grammars and web search grammars are disabled. The same is true for a SRGS grammar. Only one SRGS grammar can be activated at a time. But multiple grammar lists can be added to a grammar of speech recognition engine. 

Up Next
    Ebook Download
    View all
    View all