Grammar Switching in Speech Recognition

In this article we will learn how to manage the many grammars in speech recognition for WP8.

Introduction

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.

XAML

<phone:PhoneApplicationPage

    x:Class="Demo.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

 

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

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

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

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

        </StackPanel>

 

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

        </Grid>

    </Grid>

 

</phone:PhoneApplicationPage>

 

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

        {

            InitializeComponent();

        }

 

        private async void strtClick(object sender, RoutedEventArgs e)

        {

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

            sampleGrammar1.Add("Phone");

            sampleGrammar1.Add("Games");

            sampleGrammar1.Add("Music");

            sampleGrammar1.Add("Fun");

 

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

            sampleGrammar2.Add("Hello Phone");

            sampleGrammar2.Add("Hello Game");

            sampleGrammar2.Add("Zone");

            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.

 

 
 
 
 
 
 
 
 
 
 
 
Summary

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.