Custom Grammar For Speech Recognition In Windows Phone 8

This article explains grammar and speech recognition and how to configure them.

Introduction

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.

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

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

            sampleGrammar.Add("Phone");

            sampleGrammar.Add("Games");

            sampleGrammar.Add("Music");

 

            SpeechRecognizerUI mic = new SpeechRecognizerUI();

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

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

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

 

            SpeechRecognitionUIResult res = await mic.RecognizeWithUIAsync();

            if (res != null)

            {

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

            }

        }

    }

}

 

 

 
 
 

 

Summary

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.