Поділитися через


SrgsRuleRef Constructors

Definition

Initializes a new instance of the SrgsRuleRef class.

Overloads

SrgsRuleRef(SrgsRule)

Initializes a new instance of the SrgsRuleRef class and specifies the rule to reference.

SrgsRuleRef(Uri)

Initializes a new instance of the SrgsRuleRef class and specifies the location of the external grammar file to reference.

SrgsRuleRef(SrgsRule, String)

Initializes a new instance of the SrgsRuleRef class, specifying the rule to reference and a string that contains a semantic key.

SrgsRuleRef(Uri, String)

Initializes a new instance of the SrgsRuleRef class, specifying the location of the external grammar file and the identifier of the rule to reference.

SrgsRuleRef(SrgsRule, String, String)

Initializes a new instance of the SrgsRuleRef class, specifying the rule to reference, the string alias of the semantic dictionary, and initialization parameters.

SrgsRuleRef(Uri, String, String)

Initializes a new instance of the SrgsRuleRef class, specifying the location of the external grammar file, the identifier of the rule, and the string alias of the semantic dictionary.

SrgsRuleRef(Uri, String, String, String)

Initializes a new instance of the SrgsRuleRef class, specifying the location of the external grammar file, the identifier of the rule, the string alias of the semantic dictionary, and initialization parameters.

SrgsRuleRef(SrgsRule)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class and specifies the rule to reference.

public:
 SrgsRuleRef(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ rule);
public SrgsRuleRef (System.Speech.Recognition.SrgsGrammar.SrgsRule rule);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : System.Speech.Recognition.SrgsGrammar.SrgsRule -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (rule As SrgsRule)

Parameters

rule
SrgsRule

The object to reference.

Exceptions

rule is null.

Examples

The following example creates a grammar that recognizes the phrase "A nation that has won the World Cup is" followed by the name of a country that has won the World Cup. After creating the SrgsRule object winnerRule and giving it the string identifier WorldCupWinner, the example appends the string "A nation that has won the World Cup is" to the rule. Using SrgsOneOf objects, the example then builds a list of European countries/regions and a list of South American countries/regions, and adds each list to its respective rule, ruleEurope or ruleSAmerica. The example then creates rule references for ruleEurope and ruleSAmerica and adds them to the WorldCupWinner rule.

public void WorldSoccerWinners ()
{

  // Create an SrgsDocument, create a new rule
  // and set its scope to public.
  SrgsDocument document = new SrgsDocument();
  SrgsRule winnerRule = new SrgsRule("WorldCupWinner");
  winnerRule.Scope = SrgsRuleScope.Public;

  // Add the introduction.
  winnerRule.Elements.Add(new SrgsItem("A nation that has won the world cup is: "));

  // Create the rule for the European nations.
  SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"),
    new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});
  SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));

  // Create the rule for the South American nations.
  SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"),
    new SrgsItem("Brazil"), new SrgsItem("Uruguay")});
  SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));

  // Add references to winnerRule for ruleEurope and ruleSAmerica.
  winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem
    (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));

  // Add all the rules to the document and make winnerRule
  // the root rule of the document.
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});
  document.Root = winnerRule;
}

The created grammar has the following form.

<grammar version="1.0" xml:lang="en-US" xmlns="http://www.w3.org/2001/06/grammar" root="WorldCupWinner">
    <rule id="WorldCupWinner" scope="public">
        <item> A nation that has won the world cup is </item>
        <one-of>
            <item>
                <ruleref uri="#EuropeanNations" />
            </item>
            <item>
                <ruleref uri="#SouthAmericanNations" />
            </item>
        </one-of>
    </rule>
    <rule id="EuropeanNations">
        <one-of>
            <item> England </item>
            <item> France </item>
            <item> Germany </item>
            <item> Italy </item>
        </one-of>
    </rule>
    <rule id="SouthAmericanNations">
        <one-of>
            <item> Argentina </item>
            <item> Brazil </item>
            <item> Uruguay </item>
        </one-of>
    </rule>
</grammar>

Remarks

This constructor creates a rule reference to an SrgsRule object within the containing grammar. To create a rule reference to a rule element in an external grammar file, use any of the following constructors:

See also

Applies to

SrgsRuleRef(Uri)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class and specifies the location of the external grammar file to reference.

public:
 SrgsRuleRef(Uri ^ uri);
public SrgsRuleRef (Uri uri);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri)

Parameters

uri
Uri

The location of a grammar file outside the containing grammar.

Exceptions

uri is null.

Examples

The following example creates a grammar for an application that returns information about bus shuttle service. The first method, GrammarUrlForRoute, takes a string that specifies a route and appends it to a string specifying the location of a grammar. This specifies a particular rule in that grammar. The method returns a Uri for that rule.

The second method, CreateGrammarForRoute, creates an SrgsDocument element named grammar with a rule reference specified by the Uri passed to it by GrammarUrlForRoute. Note that the variable named _route is a member of an enclosing class.

private Uri GrammarUrlForRoute(string route)
{
  return new Uri("http://localhost/MyBus/MyBusLocations.grxml#LocationsForRoute" + route);
}

private SrgsDocument CreateGrammarForRoute()
{
  SrgsDocument grammar = new SrgsDocument();
  grammar.Mode = SrgsGrammarMode.Voice;
  SrgsRule rule = new SrgsRule("LocationsForRoute" + _route);
  SrgsRuleRef ruleref = new SrgsRuleRef(GrammarUrlForRoute(_route));

  SrgsSemanticInterpretationTag tag = new SrgsSemanticInterpretationTag ("$.Location = $$");

  rule.Elements.Add(ruleref);
  rule.Elements.Add(tag);
  grammar.Rules.Add(rule);
  grammar.Root = rule;
  return grammar;
}

Note

The variable named _route is undeclared and undefined in the preceding sample. It should be declared as a String and contain the route number for a particular bus route before the preceding sample is compiled and run.

Remarks

This constructor creates a rule reference to an external grammar file. The URI may also include the identifier of a rule to reference, for example http://www.contoso.com/ExternalGrammar.grxml#targetRule. If the uri parameter does not specify a rule identifier, the rule reference points to the root rule of the target grammar. To create a rule reference to an SrgsRule object within the same grammar, use any of the following constructors:

See also

Applies to

SrgsRuleRef(SrgsRule, String)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class, specifying the rule to reference and a string that contains a semantic key.

public:
 SrgsRuleRef(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ rule, System::String ^ semanticKey);
public SrgsRuleRef (System.Speech.Recognition.SrgsGrammar.SrgsRule rule, string semanticKey);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : System.Speech.Recognition.SrgsGrammar.SrgsRule * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (rule As SrgsRule, semanticKey As String)

Parameters

rule
SrgsRule

The object to reference.

semanticKey
String

The semantic key.

Examples

The following example creates a grammar for choosing the cities for a flight. The example constructs two SrgsRuleRef instances, each of which specifies a semantic key. Both rule references target the same SrgsRule object, named cities, but tag the recognition result from the rule reference with a different semantic key. The semantic key identifies a recognized city as the departure city or the arrival city for the flight. The handler for the SpeechRecognized event uses the keys to retrieve the semantics values created using SrgsNameValueTag from the recognition result.

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

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

    // Initialize a SpeechRecognitionEngine object.
    {
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
      {

        // Build a rule with a list of cities, assign a semantic value to each city.
        SrgsItem chi = new SrgsItem("Chicago");
        chi.Add(new SrgsNameValueTag("ORD"));
        SrgsItem bos = new SrgsItem("Boston");
        bos.Add(new SrgsNameValueTag("BOS"));
        SrgsItem mia = new SrgsItem("Miami");
        mia.Add(new SrgsNameValueTag("MIA"));
        SrgsItem dal = new SrgsItem("Dallas");
        dal.Add(new SrgsNameValueTag("DFW"));

        SrgsOneOf cities = new SrgsOneOf(new SrgsItem[] { chi, bos, mia, dal });
        SrgsRule citiesRule = new SrgsRule("flightCities");
        citiesRule.Add(cities);

        // Build the root rule, add rule references to the cities rule.
        SrgsRule flightBooker = new SrgsRule("bookFlight");
        flightBooker.Add(new SrgsItem("I want to fly from"));
        flightBooker.Add(new SrgsRuleRef(citiesRule, "departureCity"));
        flightBooker.Add(new SrgsItem("to"));
        flightBooker.Add(new SrgsRuleRef(citiesRule, "arrivalCity"));

        // Build an SrgsDocument object from the flightBooker rule and add the cities rule.
        SrgsDocument cityChooser = new SrgsDocument(flightBooker);
        cityChooser.Rules.Add(citiesRule);

        // Create a Grammar object from the SrgsDocument and load it to the recognizer.
        Grammar departArrive = new Grammar(cityChooser);
        departArrive.Name = ("Cities Grammar");
        recognizer.LoadGrammarAsync(departArrive);

        // Configure recognizer input.
        recognizer.SetInputToDefaultAudioDevice();

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

        // Start asynchronous recognition.
        recognizer.RecognizeAsync();
        Console.WriteLine("Starting asynchronous 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);
      Console.WriteLine();
      Console.WriteLine("Semantic results:");
      Console.WriteLine("  The departure city is: " + e.Result.Semantics["departureCity"].Value);
      Console.WriteLine("  The destination city is: " + e.Result.Semantics["arrivalCity"].Value);
    }
  }
}

Remarks

This constructor creates a rule reference to an SrgsRule object within the containing grammar. To create a rule reference to a rule element in an external grammar file, use any of the following constructors:

See also

Applies to

SrgsRuleRef(Uri, String)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class, specifying the location of the external grammar file and the identifier of the rule to reference.

public:
 SrgsRuleRef(Uri ^ uri, System::String ^ rule);
public SrgsRuleRef (Uri uri, string rule);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri, rule As String)

Parameters

uri
Uri

The location of a grammar file outside the containing grammar.

rule
String

The identifier of the rule to reference.

Exceptions

uri is null.

rule is null.

Remarks

This constructor creates a rule reference to a rule element in an external grammar file. To create a rule reference to an SrgsRule object within the same grammar, use any of the following constructors:

See also

Applies to

SrgsRuleRef(SrgsRule, String, String)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class, specifying the rule to reference, the string alias of the semantic dictionary, and initialization parameters.

public:
 SrgsRuleRef(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ rule, System::String ^ semanticKey, System::String ^ parameters);
public SrgsRuleRef (System.Speech.Recognition.SrgsGrammar.SrgsRule rule, string semanticKey, string parameters);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : System.Speech.Recognition.SrgsGrammar.SrgsRule * string * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (rule As SrgsRule, semanticKey As String, parameters As String)

Parameters

rule
SrgsRule

The object to reference.

semanticKey
String

The semantic key.

parameters
String

The initialization parameters for a SrgsRuleRef object.

Remarks

This constructor creates a rule reference to an SrgsRule object within the containing grammar. To create a rule reference to a rule element in an external grammar file, use any of the following constructors:

See also

Applies to

SrgsRuleRef(Uri, String, String)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class, specifying the location of the external grammar file, the identifier of the rule, and the string alias of the semantic dictionary.

public:
 SrgsRuleRef(Uri ^ uri, System::String ^ rule, System::String ^ semanticKey);
public SrgsRuleRef (Uri uri, string rule, string semanticKey);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri * string * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri, rule As String, semanticKey As String)

Parameters

uri
Uri

The location of a grammar file outside the containing grammar.

rule
String

The identifier of the rule to reference.

semanticKey
String

An alias string for the semantic dictionary.

Exceptions

uri is null.

semanticKey is null.

semanticKey is empty.

Remarks

This constructor creates a rule reference to a rule element in an external grammar file. To create a rule reference to an SrgsRule object within the same grammar, use any of the following constructors:

See also

Applies to

SrgsRuleRef(Uri, String, String, String)

Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs
Source:
SrgsRuleRef.cs

Initializes a new instance of the SrgsRuleRef class, specifying the location of the external grammar file, the identifier of the rule, the string alias of the semantic dictionary, and initialization parameters.

public:
 SrgsRuleRef(Uri ^ uri, System::String ^ rule, System::String ^ semanticKey, System::String ^ parameters);
public SrgsRuleRef (Uri uri, string rule, string semanticKey, string parameters);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri * string * string * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri, rule As String, semanticKey As String, parameters As String)

Parameters

uri
Uri

The location of a grammar file outside the containing grammar.

rule
String

The identifier of the rule to reference.

semanticKey
String

The semantic key.

parameters
String

The initialization parameters for a SrgsRuleRef object.

Remarks

This constructor creates a rule reference to a rule element in an external grammar file. To create a rule reference to an SrgsRule object within the same grammar, use any of the following constructors:

See also

Applies to