Share via


SrgsRuleRef 构造函数

定义

初始化 SrgsRuleRef 类的新实例。

重载

SrgsRuleRef(SrgsRule)

初始化 SrgsRuleRef 类和指定引用的规则的新实例。

SrgsRuleRef(Uri)

初始化 SrgsRuleRef 类的新实例并指定要引用的外部语法文件的位置。

SrgsRuleRef(SrgsRule, String)

初始化 SrgsRuleRef 类的新实例,指定引用规则和包含一语义键的字符串。

SrgsRuleRef(Uri, String)

初始化 SrgsRuleRef 类的新实例,指定要引用的外部语法文件的位置和规则的标识符。

SrgsRuleRef(SrgsRule, String, String)

初始化 SrgsRuleRef 类的新实例,指定要引用的规则,语义字典的字符串别名和初始参数。

SrgsRuleRef(Uri, String, String)

初始化 SrgsRuleRef 类的新实例,指定外部语法文件的位置,规则的标识符和语义字典的字符串别名。

SrgsRuleRef(Uri, String, String, String)

初始化 SrgsRuleRef 类的新实例,指定外部语法文件的位置,规则的标识符,语义字典的字符串别名和初始参数。

SrgsRuleRef(SrgsRule)

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

初始化 SrgsRuleRef 类和指定引用的规则的新实例。

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)

参数

rule
SrgsRule

要引用的对象。

例外

rulenull

示例

以下示例创建一个语法,该语法识别短语“赢得世界杯的国家是”,后跟赢得世界杯的国家的名称。 创建 SrgsRule 对象 winnerRule 并为其提供字符串标识符 WorldCupWinner后,该示例会将字符串“赢得世界杯冠军的国家”追加到规则中。 然后,该示例使用 SrgsOneOf 对象生成一个欧洲国家/地区列表和一个南美国家/地区列表,并将每个列表添加到其各自的规则 ruleEuroperuleSAmerica。 然后,该示例为 ruleEuroperuleSAmerica 创建规则引用,并将其添加到规则。WorldCupWinner

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;
}

创建的语法具有以下形式。

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

注解

此构造函数创建对 SrgsRule 包含语法中的 对象的规则引用。 若要创建对外部语法文件中元素 rule 的规则引用,请使用以下任一构造函数:

另请参阅

适用于

SrgsRuleRef(Uri)

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

初始化 SrgsRuleRef 类的新实例并指定要引用的外部语法文件的位置。

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)

参数

uri
Uri

包含的语法外的语法文件位置。

例外

urinull

示例

以下示例为应用程序创建一个语法,用于返回有关总线穿梭服务的信息。 第一个方法 GrammarUrlForRoute采用指定路由的字符串,并将其追加到指定语法位置的字符串。 这指定该语法中的特定规则。 方法返回 Uri 该规则的 。

第二种方法 CreateGrammarForRoute创建名为 SrgsDocumentgrammar 元素,其中包含由 Uri 传递给它的 GrammarUrlForRoute指定的规则引用。 请注意,名为 _route 的变量是封闭类的成员。

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;
}

注意

在前面的示例中,名为 _route 的变量未声明且未定义。 在编译并运行前面的示例之前, String 它应声明为 并包含特定总线路由的路由号。

注解

此构造函数创建对外部语法文件的规则引用。 URI 还可以包含要引用的规则的标识符,例如 http://www.contoso.com/ExternalGrammar.grxml#targetRuleuri如果 参数未指定规则标识符,则规则引用指向目标语法的根规则。 若要创建对同一 SrgsRule 语法中的对象的规则引用,请使用以下任一构造函数:

另请参阅

适用于

SrgsRuleRef(SrgsRule, String)

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

初始化 SrgsRuleRef 类的新实例,指定引用规则和包含一语义键的字符串。

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)

参数

rule
SrgsRule

要引用的对象。

semanticKey
String

语义键。

示例

以下示例创建一个语法,用于为外部测试版选择城市。 该示例构造两 SrgsRuleRef 个实例,每个实例指定一个语义键。 这两个规则引用都针对同一 SrgsRule 个名为 cities的对象,但使用不同的语义键标记规则引用的识别结果。 语义键将已识别的城市标识为航班的出发城市或到达城市。 事件的处理程序 SpeechRecognized 使用键从识别结果检索使用 SrgsNameValueTag 创建的语义值。

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);
    }
  }
}

注解

此构造函数创建对 SrgsRule 包含语法中的 对象的规则引用。 若要创建对外部语法文件中元素 rule 的规则引用,请使用以下任一构造函数:

另请参阅

适用于

SrgsRuleRef(Uri, String)

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

初始化 SrgsRuleRef 类的新实例,指定要引用的外部语法文件的位置和规则的标识符。

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)

参数

uri
Uri

包含的语法外的语法文件位置。

rule
String

参考的规则的标识符。

例外

urinull

rulenull

注解

此构造函数创建对外部语法文件中元素 rule 的规则引用。 若要创建对同一 SrgsRule 语法中的对象的规则引用,请使用以下任一构造函数:

另请参阅

适用于

SrgsRuleRef(SrgsRule, String, String)

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

初始化 SrgsRuleRef 类的新实例,指定要引用的规则,语义字典的字符串别名和初始参数。

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)

参数

rule
SrgsRule

要引用的对象。

semanticKey
String

语义键。

parameters
String

SrgsRuleRef 对象的初始化参数。

注解

此构造函数创建对 SrgsRule 包含语法中的 对象的规则引用。 若要创建对外部语法文件中元素 rule 的规则引用,请使用以下任一构造函数:

另请参阅

适用于

SrgsRuleRef(Uri, String, String)

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

初始化 SrgsRuleRef 类的新实例,指定外部语法文件的位置,规则的标识符和语义字典的字符串别名。

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)

参数

uri
Uri

包含的语法外的语法文件位置。

rule
String

参考的规则的标识符。

semanticKey
String

获取语义字典的别名字符串。

例外

urinull

semanticKeynull

semanticKey 为空。

注解

此构造函数创建对外部语法文件中元素 rule 的规则引用。 若要创建对同一 SrgsRule 语法中的对象的规则引用,请使用以下任一构造函数:

另请参阅

适用于

SrgsRuleRef(Uri, String, String, String)

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

初始化 SrgsRuleRef 类的新实例,指定外部语法文件的位置,规则的标识符,语义字典的字符串别名和初始参数。

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)

参数

uri
Uri

包含的语法外的语法文件位置。

rule
String

参考的规则的标识符。

semanticKey
String

语义键。

parameters
String

SrgsRuleRef 对象的初始化参数。

注解

此构造函数创建对外部语法文件中元素 rule 的规则引用。 若要创建对同一 SrgsRule 语法中的对象的规则引用,请使用以下任一构造函数:

另请参阅

适用于