Grammar Switching in Speech Recognition


In this article we will learn how to manage the many grammars in speech recognition. In your app you can add more then one grammar with different keys so that we can use a different grammar for each game mode. This article covers this. We will see how we can switch among various grammar modes.

Enabling or Disabling The Grammar

After setting the grammar our next task is to make it active and inactive for the rest of the grammars. To enable or disable a grammar the syntax is as follows:

[SpeechRecognition Object Name].Grammars["Grammar Key"].Enabled = True or False

Code Example

The following code will help you in learning the preceding explained concept.











    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"/>

            <CheckBox Name="grm1CB" Content="Grammar 1" HorizontalAlignment="Left" Height="75" Margin="10,258,0,0" VerticalAlignment="Top" Width="189"/>

            <CheckBox Name="grm2CB" Content="Grammar 2" HorizontalAlignment="Left" Height="75" Margin="245,258,0,0" VerticalAlignment="Top" Width="201"/>






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> sampleGrammar1 = new List<string>();






            List<string> sampleGrammar2 = new List<string>();

            sampleGrammar2.Add("Hello Phone");

            sampleGrammar2.Add("Hello Game");


            sampleGrammar2.Add("Hello Music");


            SpeechRecognizerUI mic = new SpeechRecognizerUI();

            mic.Recognizer.Grammars.AddGrammarFromList("Grammar1", sampleGrammar1);

            mic.Recognizer.Grammars.AddGrammarFromList("Grammar2", sampleGrammar2);

            if (grm1CB.IsChecked.Value && grm2CB.IsChecked.Value)


                mic.Recognizer.Grammars["Grammar1"].Enabled = true;

                mic.Recognizer.Grammars["Grammar2"].Enabled = true;


            else if (grm2CB.IsChecked.Value)


                mic.Recognizer.Grammars["Grammar2"].Enabled = true;

                mic.Recognizer.Grammars["Grammar1"].Enabled = false;

                mic.Recognizer.Grammars["Grammar2"].Probability = SpeechGrammarProbability.High;


            else if (grm1CB.IsChecked.Value)


                mic.Recognizer.Grammars["Grammar1"].Enabled = true;

                mic.Recognizer.Grammars["Grammar1"].Probability = SpeechGrammarProbability.High;

                mic.Recognizer.Grammars["Grammar2"].Enabled = false;



            SpeechRecognitionUIResult res = await mic.RecognizeWithUIAsync();

            if (res != null)


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






In the code above, I first created two checkboxes to enable a selected grammar. In the code behind I'm checking which check box is checked and enabling the selected grammar accordingly. The high probability helps in recognizing the speech and offers better suggestions for the user.



That's all for this article. I hope you have found it useful. Now you can manage multiple grammars within your app. In case of any doubt feel free to ask in the comments section.