Share via


Authoring Grammars

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

In the Microsoft Visual Studio 2008 development system, use Grammar class members to create grammars in code for Unified Communications Managed API applications.

Grammars are used by communications workflow applications to recognize user inputs in three modes: speech, instant message (IM), and DTMF (Dual-Tone Multiple-Frequency). Grammars authored for speech recognition will recognize text input in IM mode. Recognizing DTMF inputs requires a separate grammar.

The following activities all use grammars, and the process for adding a grammar to these activities is identical.

  • SpeechQuestionAnswerActivity

  • SpeechHelpCommandActivity

  • SpeechCommandActivity

  • SpeechRepeatCommandActivity

  • InstantMessagingQuestionAnswerActivity

  • InstantMessagingHelpCommandActivity

  • InstantMessagingCommandActivity

Library Grammars

A library grammar contains a collection of preconstructed grammar rulesets. Each ruleset contains a number of publicly scoped rules that are designed to recognize spoken input for a specific type of data such as a date, a time, or numbers. A library grammar is provided as a convenience, obviating the need for developers to write their own rules for collecting commonly used data and enabling developers to easily develop more complex grammars based on these types of common data. In addition, many of the individual rules provide built-in validation.

By default, find library grammars at the path UCMA SDK 2.0\Speech\Sample Applications\Speech\Grammar Files. Add grammar files by setting the appropriate property value in the Properties window. Paths for multiple files are semi-colon delimited. Grammar rules are indicated by appending a pound sign and the rule name to the grammar file path, for example c:/Library.grxml#Ordinal. For more information, see the following procedure.

The following procedure uses a SpeechQuestionAnswerActivity activity and works similarly with other activities that use grammars.

Add .grxml grammars using library grammars

  1. In Visual Studio 2008, create a new communications workflow project.

  2. In the Toolbox, expand Unified Communications Workflow to display speech activities.

  3. Drag and drop a SpeechQuestionAnswer activity from the Toolbox onto the design surface in communicationsSequenceActivity1 above disconnectCallActivity1.

  4. In the Properties window, double-click the Grammars property and enter the path to the library grammar. Multiple paths are semi-colon delimited.

Create a Grammar

The following procedure uses a SpeechQuestionAnswerActivity activity and works equally well with an InstantMessagingQuestionAnswerActivity activity.

To create a grammar in code

  1. In Visual Studio 2008, create a new communications workflow project.

  2. In the Toolbox, expand Unified Communications Workflow to display speech activities.

  3. Drag and drop a SpeechQuestionAnswer activity from the Toolbox onto the design surface in communicationsSequenceActivity1 above disconnectCallActivity1.

  4. In the Properties window, expand Prompts and set What color? as the value for the MainPrompt property.

  5. In Solution Explorer, right-click Workflow1.xoml.cs and then click View Code.

  6. In the Code window, add the following using statements.

    using Microsoft.Speech.Recognition;
    using Microsoft.Speech.Recognition.SrgsGrammar;
    using System.Xml;
    
  7. Add the following procedure at the bottom of the Workflow class.

    private Grammar CreateGrammar()
    {
      string[] colors = new string[3];
      colors[0] = "red";
      colors[1] = "blue";
      colors[2] = "green";
    
      SrgsDocument colorsGrammar = new SrgsDocument();
      colorsGrammar.Mode = SrgsGrammarMode.Voice;
      SrgsRule rule = new SrgsRule("Items");
      SrgsOneOf oneOf = new SrgsOneOf(colors);
      rule.Elements.Add(oneOf);
      colorsGrammar.Rules.Add(rule);
      colorsGrammar.Root = rule;
    
    Grammar newGrammar = new Grammar(colorsGrammar);
    
      return newGrammar;
    }
    
  8. On the design surface, right-click speechQuestionAnswerActivity1 and then click Generate Handlers.

  9. In the event handler, add the following statements.

    this.speechQuestionAnswerActivity1.Grammars.Clear();
    Grammar colorsGrammar = CreateGrammar();
    this.speechQuestionAnswerActivity1.Grammars.Add(colorsGrammar);
    

To build and debug the application

  1. Create a debugging environment. For information about creating a debugging environment, see Establish Accounts on Office Communications Server.

  2. Debug the application. For information about debugging applications, see Walkthrough: Debugging a Communications Workflow Application.

Specify Inputs

Instead of attaching a grammar file, you can also specify the expected user inputs directly in the Properties window. The following table lists the properties and activities that support recognition of user input.

Activity

Property Name

Description

SpeechQuestionAnswerActivity

ExpectedSpeechInputs

An array of speech keywords that this activity will recognize.

SpeechQuestionAnswerActivity

ExpectedDtmfInputs

An array of DTMF keywords that this activity will recognize.

SpeechCommandActivity

ExpectedSpeechInputs

An array of speech keywords that will trigger this command.

SpeechCommandActivity

ExpectedDtmfInputs

An array of DTMF keywords that will trigger this command.

SpeechHelpCommandActivity

ExpectedSpeechInputs

An array of speech keywords that will trigger this command.

SpeechHelpCommandActivity

ExpectedDtmfInputs

An array of DTMF keywords that will trigger this command.

SpeechRepeatCommandActivity

ExpectedSpeechInputs

An array of speech keywords that will trigger this command.

SpeechRepeatCommandActivity

ExpectedDtmfInputs

An array of DTMF keywords that will trigger this command.

InstantMessagingQuestionAnswerActivity

ExpectedInputs

An array of IM keywords that will trigger this command.

InstantMessagingCommandActivity

ExpectedInputs

An array of IM keywords that will trigger this command.

InstantMessagingHelpCommandActivity

ExpectedInputs

An array of IM keywords that will trigger this command.

The following procedure uses the SpeechQuestionAnswerActivity.ExpectedSpeechInputs property and works equally well with the other properties listed in the previous table.

To insert expected inputs

  1. In Visual Studio 2008, create a new communications workflow project.

  2. In the Toolbox, expand Unified Communications Workflow to display workflow activities.

  3. Drag and drop a SpeechQuestionAnswer activity from the Toolbox onto the design surface in communicationsSequenceActivity1 above disconnectCallActivity1.

  4. In the Properties window, on the ExpectedSpeechInputs property, click the Browse button.

  5. In String Collection Editor, enter the words red, green, and blue (situate each word on a separate line), and then click OK.

See Also

Other Resources

Unified Communications Managed API 2.0 Workflow SDK Documentation