次の方法で共有


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Grammar Constructor (SrgsDocument, String)

Initializes a new instance of a Grammar class from an SrgsDocument object and specifies a root rule.

Namespace:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

'Declaration
Public Sub New ( _
    srgsDocument As SrgsDocument, _
    ruleName As String _
)
'Usage
Dim srgsDocument As SrgsDocument
Dim ruleName As String

Dim instance As New Grammar(srgsDocument, _
    ruleName)
public Grammar(
    SrgsDocument srgsDocument,
    string ruleName
)

Parameters

  • ruleName
    Type: System.String
    The identifier of the rule to use as the entry point of the speech recognition grammar, or a null reference (Nothing in Visual Basic) to use the default root rule of the SrgsDocument.
    This parameter may be a null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentException

ruleName cannot be resolved or is not public, or ruleName is a null reference (Nothing in Visual Basic) and srgsDocument does not contain a root rule.

ArgumentNullException

srgsDocument is a null reference (Nothing in Visual Basic).

FormatException

srgsDocument contains a rule reference that cannot be resolved.

Remarks

The srgsDocument argument must:

  • Never be a null reference (Nothing in Visual Basic).

  • Contain the rule specified by ruleName, if ruleName is not a null reference (Nothing in Visual Basic), and a root rule if ruleName is a null reference (Nothing in Visual Basic).

The ruleName argument:

  • May be a null reference (Nothing in Visual Basic) or Empty.

  • If ruleName is not a null reference (Nothing in Visual Basic) and the rule specified is not found in the grammar being loaded, an exception is generated.

  • If ruleName is a null reference (Nothing in Visual Basic), and the grammar contained in the file specified does not declare a root rule, an exception is generated.

As there is no Base URI specified, any rule references must:

  • Use absolute URIs.

  • Target rules within the grammar being loaded.

  • Use any paths defined in the grammar object being loaded. If the stream was created from a file, this typically includes the directory where that file was located.

    To create a Grammar object from an SrgsDocument and specify a base URI to use to resolve relative rule references, use the Grammar(SrgsDocument, String, Uri) constructor.

Examples

The following example creates a speech recognition grammar in an SrgsDocument instance and specifies a rule to use as the root rule of the grammar. The example constructs a Grammar object from the SrgsDocument instance and loads it into the speech recognition engine.

using System;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Recognition.SrgsGrammar;

namespace SampleRecognition
{
  class Program
  {
    static void Main(string[] args)

    // Initialize a SpeechRecognitionEngine object.
    {
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine())
      {

        // Create the SrgsDocument.
        SrgsDocument document = new SrgsDocument();

        // Create the Cities rule and add it to the document.
        SrgsRule citiesRule = new SrgsRule("Cities");
        citiesRule.Scope = SrgsRuleScope.Public;

        SrgsOneOf cityChoice = new SrgsOneOf();
        cityChoice.Add(new SrgsItem("Seattle"));
        cityChoice.Add(new SrgsItem("Los Angeles"));
        cityChoice.Add(new SrgsItem("New York"));
        cityChoice.Add(new SrgsItem("Miami"));

        citiesRule.Add(cityChoice);
        document.Rules.Add(citiesRule);

        // Create the Main rule and add it to the document.
        SrgsRule mainRule = new SrgsRule("Main");
        mainRule.Scope = SrgsRuleScope.Public;

        mainRule.Add(new SrgsItem("I would like to fly from"));
        mainRule.Add(new SrgsRuleRef(citiesRule));
        mainRule.Add(new SrgsItem("to"));
        mainRule.Add(new SrgsRuleRef(citiesRule));

        document.Rules.Add(mainRule);

        // Create the Grammar object and specify which rule to use as the root.
        Grammar citiesGrammar = new Grammar(document,"Main");

        // Load the grammar object to the recognizer.
        recognizer.LoadGrammarAsync(citiesGrammar);

        // Attach a handler for the SpeechRecognized event.
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

        // Set the input to the recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Start recognition.
        recognizer.RecognizeAsync();
        Console.WriteLine("Starting aynchronous recognition...");

        // Keep the console window open.
        Console.ReadLine();
      }
    }

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("  Speech recognized: " + e.Result.Text);
    }
  }
}

See Also

Reference

Grammar Class

Grammar Members

Grammar Overload

Microsoft.Speech.Recognition Namespace

Microsoft.Speech.Recognition.SrgsGrammar

SrgsDocument

SpeechRecognitionEngine

Grammar

Other Resources

Speech Recognition Grammar Specification Version 1.0