Speech SDK 5.1--No.3:grammar format overview

本文深入探讨了XML格式在语音应用中作为语法XML元素块的使用,包括提高识别准确性的约束、增强文本语法的维护性、简化翻译识别为应用操作的方法,以及XML语法属性和内容的规范。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Excerpted from  official document--sapi.chm


Text Grammar Format Overview

     The Extensible Markup Language (XML) format inside a GRAMMAR XML element (block), is an "expert–only–readable" declaration of a grammar that a speech application uses to accomplish the following:

  • Improve recognition accuracy by restricting(约束) and indicating to an engine what words it should expect.
  • Improve maintainability(可维护性) of textual grammars, by providing constructs for reusable text components (internal and external rule references), phrase lists, and string and numeric identifiers.
  • Improve translation of recognized speech into application actions. This is made easier by providing "semantic tags," (property name, and value associations) to words/phrases declared inside the grammar.

    A GRAMMAR XML element (block) appears in a XML source code file. The XML source is compiled into a binary grammar format and is the format used by SAPI during application run time.


Extensible Markup Laguage

    The textual grammar format is an application of the XML. Every XML element consists of a start tag (<SOME_TAG>) and an end tag (</SOME_TAG>) with a case-insensitive tag name and contents between these tags. The start tag and the end tag are the same if the element is empty. For example, the tag (<SOME_TAG/>). For more information on the use of XML grammars, please see theGrammar XML Schema section. Additionally, more information about XML and the XML specification is available at:http://www.w3.org/TR/REC-xml.

   For example, all grammars contain the opening tag <GRAMMAR> as follows:

<GRAMMAR>
... grammar content
</GRAMMAR>

   Note that the contents of the grammar is contained between an opening tag and a trailing, closing tag.


Attributes

    Attributes of an XML element appear inside the start tag. Each attribute is in the form of a name followed by an equal sign followed by a string which must be surrounded by either single or double quotation marks. An attribute of a given name may only appear once in a start tag.

    In summary, the literal string cannot contain either < or ', if the string is surrounded by single quotation marks. It may not contain ", if the string is surrounded by double quotation marks. Furthermore, use all ampersand (&) characters only in an entity reference such as &amp; and &gt;. When a literal string is parsed, the resulting replacement text will resolve all entity references such as &gt; into its corresponding text, such as >. In this specification, only the resulting replacement text needs to be defined for attribute value strings. More information about XML and the XML specification is available at:http://www.w3.org/TR/REC-xml.

   For example, the grammar author can specify the language (id) of the grammar as follows. 

<GRAMMAR LANGID="409">
... grammar content
</GRAMMAR>

   The grammar element ( <GRAMMAR>) has an attribute, called LANGID which must be a numeric value. The grammar author specifies the language attribute by placing the attribute inside the brackets of the opening tag, and enclosing the attribute value (e.g. 409) in quotation marks.


Contents

    The contents of an element consists of text or subelements. Formal definitions of valid contents in this specification are provided as regular and "multi-set" expressions. The pseudo-element name "Text" indicates untagged text. With these definitions, the XML specification defines the exact file syntax details.

   For example, the grammar author can place either text or sub-elements inside a phrase tag as follows.

<PHRASE>
   hello
</PHRASE>

<PHRASE>
   <OPT>world</OPT>

</PHRASE>

    The grammar author should review the SAPI 5 Grammar XML Schema to determine the type of content support in each tag (e.g. text and sub-elements, only text, only sub-elements, etc.).


Comments

    The SAPI 5 XML parser treats HTML comment tags as unknown XML tag elements. The engine should provide support for comments and other unknown XML elements.

    It is recommended that grammar authors place comments in their XML files (e.g.mygrammar.xml), similar to commenting source code, since the XML parser will safely parse the comments without affecting the grammar itself. Similarly, there is increase in size of the binary form of the grammar (e.g.mygrammar.cfg) since the SAPI 5 grammar compiler strips out the comments.

    An example of a comment in an XML grammar is as follows.

<!-- the 'travel' rule is the main voice command for our app, so it active by default -->
   <RULE ID="RID_Travel" TOPLEVEL="ACTIVE">
      <PHRASE>travel from</PHRASE>

      <!-- include location grammar component, so we can change the location list at runtime -->
      <RULEREF REFID="RID_Location" PROPID="PID_FromDestination"/>
      <PHRASE>to</PHRASE>

      <!-- include location grammar component, so we can change the location list at runtime -->
      <RULEREF REFID="RID_Location" PROPID="PID_ToDestination"/>
   </RULE>

    Note that the comment blocks always begin with <!-- and end with -->.


How SAPI utilize XML information

     SAPI uses XML content in the following two methods.

  1. The SAPI context-free grammar compiler, compiles the XML grammar into a binary grammar format. The compiled binary grammar is loaded into the SAPI run-time environment from afile, memory, or object (.DLL) resource.By difefrent functions
  2. The speech recognition (SR) engine queries the run-time environment for available grammar information. 


Frequently used definitions
   
    Untagged text declaring a sequence of words that the recognition engine will recognize. Tentatively this text is only the not-necessarily-phonetic representation of words used for reading words whose pronunciation is unknown to the user (for example, for Japanese, kana, not kanji); this form will be called the spelling form. In further definitions in this section, Text will be referenced as though it were a pseudo-element.



Non-empty concatenated(有关联的) recognition contents

   The contents of a number of XML elements in this specification such as, the P element, contain a sequence of grammar constructs which are concatenated together (one grammar construct after another). These grammar elements must be recognized in order for the contents defined to be recognized.

The contents must be one of the following (and not both):

Text and any number of L, P, O, orRULEREF elements in any order with at least oneL,P, or RULEREF.

For more information on the use of XML grammars, please see the Grammar XML Schema section.
















C:\Users\ZSKBOOK>cpanm Perl::LanguageServer --> Working on Perl::LanguageServer Fetching http://www.cpan.org/authors/id/G/GR/GRICHTER/Perl-LanguageServer-2.6.2.tar.gz ... OK Configuring Perl-LanguageServer-v2.6.2 ... OK ==> Found dependencies: Class::Refresh, Coro, AnyEvent::AIO, Hash::SafeKeys, IO::AIO, Compiler::Lexer --> Working on Class::Refresh Fetching http://www.cpan.org/authors/id/D/DO/DOY/Class-Refresh-0.07.tar.gz ... OK Configuring Class-Refresh-0.07 ... OK ==> Found dependencies: Class::Unload, Devel::OverrideGlobalRequire --> Working on Class::Unload Fetching http://www.cpan.org/authors/id/I/IL/ILMARI/Class-Unload-0.11.tar.gz ... OK Configuring Class-Unload-0.11 ... OK Building and testing Class-Unload-0.11 ... OK Successfully installed Class-Unload-0.11 --> Working on Devel::OverrideGlobalRequire Fetching http://www.cpan.org/authors/id/D/DA/DAGOLDEN/Devel-OverrideGlobalRequire-0.001.tar.gz ... OK Configuring Devel-OverrideGlobalRequire-0.001 ... OK Building and testing Devel-OverrideGlobalRequire-0.001 ... OK Successfully installed Devel-OverrideGlobalRequire-0.001 Building and testing Class-Refresh-0.07 ... OK Successfully installed Class-Refresh-0.07 --> Working on Coro Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/Coro-6.57.tar.gz ... OK Configuring Coro-6.57 ... OK ==> Found dependencies: Guard --> Working on Guard Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/Guard-1.023.tar.gz ... OK Configuring Guard-1.023 ... OK Building and testing Guard-1.023 ... OK Successfully installed Guard-1.023 Building and testing Coro-6.57 ... OK Successfully installed Coro-6.57 --> Working on AnyEvent::AIO Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/AnyEvent-AIO-1.1.tar.gz ... OK Configuring AnyEvent-AIO-1.1 ... OK ==> Found dependencies: IO::AIO --> Working on IO::AIO Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/IO-AIO-4.81.tar.gz ... OK Configuring IO-AIO-4.81 ... OK Building and testing IO-AIO-4.81 ... FAIL ! Installing IO::AIO failed. See C:\Users\ZSKBOOK\.cpanm\work\1752592067.8072\build.log for details. Retry with --force to force install it. ! Installing the dependencies failed: Module &#39;IO::AIO&#39; is not installed ! Bailing out the installation for AnyEvent-AIO-1.1. --> Working on Hash::SafeKeys Fetching http://www.cpan.org/authors/id/M/MO/MOB/Hash-SafeKeys-0.04.tar.gz ... OK Configuring Hash-SafeKeys-0.04 ... OK Building and testing Hash-SafeKeys-0.04 ... OK Successfully installed Hash-SafeKeys-0.04 --> Working on Compiler::Lexer Fetching http://www.cpan.org/authors/id/G/GO/GOCCY/Compiler-Lexer-0.23.tar.gz ... OK ==> Found dependencies: Module::Build::XSUtil --> Working on Module::Build::XSUtil Fetching http://www.cpan.org/authors/id/H/HI/HIDEAKIO/Module-Build-XSUtil-0.19.tar.gz ... OK Configuring Module-Build-XSUtil-0.19 ... OK ==> Found dependencies: Cwd::Guard, Devel::CheckCompiler, File::Copy::Recursive::Reduced --> Working on Cwd::Guard Fetching http://www.cpan.org/authors/id/K/KA/KAZEBURO/Cwd-Guard-0.05.tar.gz ... OK Configuring Cwd-Guard-0.05 ... OK Building and testing Cwd-Guard-0.05 ... OK Successfully installed Cwd-Guard-0.05 --> Working on Devel::CheckCompiler Fetching http://www.cpan.org/authors/id/S/SY/SYOHEX/Devel-CheckCompiler-0.07.tar.gz ... OK Configuring Devel-CheckCompiler-0.07 ... OK Building and testing Devel-CheckCompiler-0.07 ... OK Successfully installed Devel-CheckCompiler-0.07 --> Working on File::Copy::Recursive::Reduced Fetching http://www.cpan.org/authors/id/J/JK/JKEENAN/File-Copy-Recursive-Reduced-0.008.tar.gz ... OK Configuring File-Copy-Recursive-Reduced-0.008 ... OK Building and testing File-Copy-Recursive-Reduced-0.008 ... OK Successfully installed File-Copy-Recursive-Reduced-0.008 Building and testing Module-Build-XSUtil-0.19 ... OK Successfully installed Module-Build-XSUtil-0.19 Configuring Compiler-Lexer-0.23 ... OK Building and testing Compiler-Lexer-0.23 ... OK Successfully installed Compiler-Lexer-0.23 ! Installing the dependencies failed: Module &#39;IO::AIO&#39; is not installed, Module &#39;AnyEvent::AIO&#39; is not installed ! Bailing out the installation for Perl-LanguageServer-v2.6.2. 11 distributions installed C:\Users\ZSKBOOK>cpanm PadWalker PPI PadWalker is up to date. (2.5) --> Working on PPI Fetching http://www.cpan.org/authors/id/M/MI/MITHALDU/PPI-1.283.tar.gz ... OK Configuring PPI-1.283 ... OK ==> Found dependencies: Test::Object, YAML::PP, Test::SubCalls, Safe::Isa --> Working on Test::Object Fetching http://www.cpan.org/authors/id/E/ET/ETHER/Test-Object-0.08.tar.gz ... OK Configuring Test-Object-0.08 ... OK Building and testing Test-Object-0.08 ... OK Successfully installed Test-Object-0.08 --> Working on YAML::PP Fetching http://www.cpan.org/authors/id/T/TI/TINITA/YAML-PP-v0.39.0.tar.gz ... OK Configuring YAML-PP-v0.39.0 ... OK Building and testing YAML-PP-v0.39.0 ... OK Successfully installed YAML-PP-v0.39.0 --> Working on Test::SubCalls Fetching http://www.cpan.org/authors/id/E/ET/ETHER/Test-SubCalls-1.10.tar.gz ... OK Configuring Test-SubCalls-1.10 ... OK ==> Found dependencies: Hook::LexWrap --> Working on Hook::LexWrap Fetching http://www.cpan.org/authors/id/E/ET/ETHER/Hook-LexWrap-0.26.tar.gz ... OK Configuring Hook-LexWrap-0.26 ... OK Building and testing Hook-LexWrap-0.26 ... OK Successfully installed Hook-LexWrap-0.26 Building and testing Test-SubCalls-1.10 ... OK Successfully installed Test-SubCalls-1.10 --> Working on Safe::Isa Fetching http://www.cpan.org/authors/id/E/ET/ETHER/Safe-Isa-1.000010.tar.gz ... OK Configuring Safe-Isa-1.000010 ... OK Building and testing Safe-Isa-1.000010 ... OK Successfully installed Safe-Isa-1.000010 Building and testing PPI-1.283 ... OK Successfully installed PPI-1.283 6 distributions installed C:\Users\ZSKBOOK>cpanm Perl::LanguageServer --> Working on Perl::LanguageServer Fetching http://www.cpan.org/authors/id/G/GR/GRICHTER/Perl-LanguageServer-2.6.2.tar.gz ... OK Configuring Perl-LanguageServer-v2.6.2 ... OK ==> Found dependencies: AnyEvent::AIO, IO::AIO --> Working on AnyEvent::AIO Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/AnyEvent-AIO-1.1.tar.gz ... OK Configuring AnyEvent-AIO-1.1 ... OK ==> Found dependencies: IO::AIO --> Working on IO::AIO Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/IO-AIO-4.81.tar.gz ... OK Configuring IO-AIO-4.81 ... OK Building and testing IO-AIO-4.81 ... FAIL ! Installing IO::AIO failed. See C:\Users\ZSKBOOK\.cpanm\work\1752592572.25820\build.log for details. Retry with --force to force install it. ! Installing the dependencies failed: Module &#39;IO::AIO&#39; is not installed ! Bailing out the installation for AnyEvent-AIO-1.1. ! Installing the dependencies failed: Module &#39;IO::AIO&#39; is not installed, Module &#39;AnyEvent::AIO&#39; is not installed ! Bailing out the installation for Perl-LanguageServer-v2.6.2.
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值