GrammarBuilder.AppendRuleReference 方法

定义

为语法元素的当前顺序追加语法文件或语法规则。Appends a grammar file or a grammar rule to the current sequence of grammar elements.

重载

AppendRuleReference(String)

为语法元素的当前顺序追加语法定义文件。Appends a grammar definition file to the current sequence of grammar elements.

AppendRuleReference(String, String)

为语法元素的当前顺序追加指定的语法定义文件规则。Appends the specified rule of a grammar definition file to the current sequence of grammar elements.

注解

AppendRuleReference方法可以将语法文件或语法规则附加到文件中。The AppendRuleReference methods can append a grammar file or a grammar rule from a file. 这些方法允许应用程序使用预先部署或可公开使用的语法规则。These methods allow applications to make use of pre-deployed or publicly available grammar rules. 应用程序必须对指定语法文件的位置具有读取访问权限。The application must have read access to the location of specified grammar files.

这些方法可以从以下格式读取语音识别语法。These methods can read a speech recognition grammar from the following formats.

使用扩展名为的二进制语法文件将 XML 格式的 SRGS 语法文件编译为二进制语法文件可以减少搜索匹配所用的时间,尤其是当语法要求识别大量字词和短语时。Compiling an XML-format SRGS grammar file to a binary grammar file with the .cfg extension can reduce the time consumed by searches for a match, especially if the grammar requires recognition of a large number of words and phrases. 有关将 SRGS 语法编译为 CFG 二进制格式的信息,请参阅 SrgsGrammarCompilerFor information about compiling SRGS grammars to the CFG binary format, see SrgsGrammarCompiler.

AppendRuleReference(String)

为语法元素的当前顺序追加语法定义文件。Appends a grammar definition file to the current sequence of grammar elements.

public:
 void AppendRuleReference(System::String ^ path);
public void AppendRuleReference (string path);
member this.AppendRuleReference : string -> unit
Public Sub AppendRuleReference (path As String)

参数

path
String

在一个支持的布局描述语音识别语法的路径通用资源标识符(URI)的文件。The path or Universal Resource Identifier (URI) of the file that describes a speech recognition grammar in a supported format.

示例

下面的 c # 示例将创建一个语音识别语法,该语法使用 Cities 本地 SRGS 文件 grxml 中名为的规则。The following C# example creates a speech recognition grammar that uses the rule named Cities in a local SRGS file, cities.grxml. 下面的 c # 代码示例中将显示 grxml 文件的内容。The content of the cities.grxml file appears below the C# code example.

private static Grammar CreateCitiesGrammar1()  
{  
  GrammarBuilder builder = new GrammarBuilder();  
  builder.AppendRuleReference("file://c:/temp/cities.grxml");  

  Grammar citiesGrammar = new Grammar(builder);  
  citiesGrammar.Name = "Cities Grammar 1";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-16" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.grxml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item> Seattle </item>  
      <item> Los Angeles </item>  
      <item> New York </item>  
      <item> Miami </item>  
    </one-of>  
  </rule>  
</grammar>  

注解

参数提供的 URI path 可以是本地的,也可以是远程的。The URI provided by the path argument may be local or remote. 应用程序必须对指定语法文件的位置具有读取访问权限。The application must have read access to the location of specified grammar files.

W3C 语音识别语法规范 (SRGS) 表示形式可以定义根规则。A W3C Speech Recognition Grammar Specification (SRGS) representation can define a root rule. 此方法将语法从其根规则开始追加到语法元素的当前序列。This method appends the grammar, beginning with its root rule, to the current sequence of grammar elements. 若要追加特定语法规则,请使用 AppendRuleReference 方法。To append a specific grammar rule, use the AppendRuleReference method.

另请参阅

适用于

AppendRuleReference(String, String)

为语法元素的当前顺序追加指定的语法定义文件规则。Appends the specified rule of a grammar definition file to the current sequence of grammar elements.

public:
 void AppendRuleReference(System::String ^ path, System::String ^ rule);
public void AppendRuleReference (string path, string rule);
member this.AppendRuleReference : string * string -> unit
Public Sub AppendRuleReference (path As String, rule As String)

参数

path
String

在一个支持的布局描述语音识别语法的路径通用资源标识符(URI)的文件路径。The file path or Universal Resource Identifier (URI) of the file that describes a speech recognition grammar in a supported format.

rule
String

规则的追加、或追加语法文件的默认根规则的标识符 nullThe identifier of the rule to append, or null to append the default root rule of the grammar file.

示例

下面的 c # 示例将创建一个语音识别语法,该语法使用 Cities 本地 SRGS 文件 grxml 中名为的规则。The following C# example creates a speech recognition grammar that uses the rule named Cities in a local SRGS file, cities.grxml. 下面的 c # 代码示例中将显示 grxml 文件的内容。The content of the cities.grxml file appears below the C# code example.

private static Grammar CreateCitiesGrammar2()  
{  
  GrammarBuilder builder = new GrammarBuilder();  
  builder.Append("Does");  
  builder.AppendRuleReference(@"c:\temp\cities.grxml", "Cities");  
  builder.Append("have a shuttle");  

  Grammar citiesGrammar = new Grammar(builder);  
  citiesGrammar.Name = "Cities Grammar 2";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.grxml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item> Seattle </item>  
      <item> Los Angeles </item>  
      <item> New York </item>  
      <item> Miami </item>  
    </one-of>  
  </rule>  
</grammar>  

注解

参数提供的 URI path 可以是本地的,也可以是远程的。The URI provided by the path argument may be local or remote. 应用程序必须对指定语法文件的位置具有读取访问权限。The application must have read access to the location of specified grammar files.

您可以使用 AppendRuleReference 方法来追加语法文件(以其根规则开头)。You can use the use the AppendRuleReference method to append a grammar file beginning with its root rule.

另请参阅

适用于