Custom Grammar For Speech Recognition In Windows Phone 8


In this article we will learn about the grammar settings of speech recognition. It's not a good idea to use the default grammar of the speech recognizer in your app if you want to recognize only predefined text. To make a speech recognizer to follow your app rules you need to configure it's grammar. The grammar is something from where the speech recognition engine retrieves the spoken text. There are many ways to set the grammar but I'm focusing only on the list method of adding grammar since it is simple and easy to use.

Creating A grammar

Grammar is nothing but a collection of words or phrases that we want to recognize. So to create a grammar first we will create a list of words or phrases. The next step is to add it in a grammar dictionary of speech recognition. For this we need to request the grammar object from a speech recognition object. On that object we will call the appropriate method to add our grammar list to it. Now the last step is to mark this grammar as the active grammar so that the voice recognition engine can use it.

Setting up the Grammar

The following procedure will help you in setting up your grammar.

  1. Prepare your grammar list.
  2. Create a key for it.
  3. Grab the Grammar object from speech recognizer.
  4. Call the "AddGrammarFromList" method on that object with key and list as the parameters.
  5. Finally mark this grammar active.

Grammar Demo

The following code summarizes all the steps I described above.











    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"



    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">


            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>



        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock Text="Grammar Demo" Margin="9,-7,0,0" FontSize="40"/>



        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Button Name="startBtn" Content="Start" Click="strtClick" HorizontalAlignment="Left" Margin="10,0,0,10" Width="446" Height="72" VerticalAlignment="Bottom"/>





C# Code Behind

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Navigation;

using Microsoft.Phone.Controls;

using Microsoft.Phone.Shell;

using Demo.Resources;

using System.Threading;

using Windows.Phone.Speech.Recognition;


namespace Demo


    public partial class MainPage : PhoneApplicationPage


        // Constructor

        public MainPage()





        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);










That's all for this article. Now you can customize the Speech recognition UI with your own grammar to suite your app's needs. In case of any doubt feel free to ask in the comments.