

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 文法定義
<a name="grammar-srgs-spec"></a>

本主題顯示 Amazon Lex V2 支援的 SRGS 規格部分。所有規則都在 SRGS 規格中定義。如需詳細資訊，請參閱[語音辨識文法規格 1.0 W3C 版建議。](https://www.w3.org/TR/speech-grammar/) W3C 

**Topics**
+ [標頭宣告](srgs-header.md)
+ [支援的 XML 元素](srgs-supported-xml.md)
+ [權杖](srgs-tokens.md)
+ [規則參考](srgs-rule-reference.md)
+ [序列和封裝](srgs-sequence.md)
+ [重複](srgs-repeats.md)
+ [Language](srgs-language.md)
+ [Tags (標籤)](srgs-tags.md)
+ [權重](grammar-weights.md)

本文件包含從 W3C 語音辨識文法規格 1.0 版複製和衍生的資料 （請參閱 https：//[https://www.w3.org/TR/speech-grammar/](https://www.w3.org/TR/speech-grammar/))。引文資訊如下：

[Copyright](http://www.w3.org/Consortium/Legal/ipr-notice#Copyright) © 2004 [W3C®](http://www.w3.org/) ([MIT](http://www.csail.mit.edu/)、[ERCIM](http://www.ercim.org/)、[Keio](http://www.keio.ac.jp/)、保留所有權利。適用 W3C [責任](http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer)、[商標](http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks)、[文件使用](http://www.w3.org/Consortium/Legal/copyright-documents)和[軟體授權](http://www.w3.org/Consortium/Legal/copyright-software)規則。

SRGS 規格文件為 [W3C 建議](https://www.w3.org/2004/02/Process-20040205/tr.html#RecsW3C)，可根據下列授權從 W3C 取得。

## 授權文字
<a name="license-text"></a>

授權

透過使用和/或複製本文件或連結此陳述式的 W3C 文件，您 （被授權方） 同意您已閱讀、了解並將遵守下列條款與條件：

以任何媒介複製和分發本文件內容的許可，或將此陳述式連結的 W3C 文件，以用於任何目的，並在此授予免費或權利金，但前提是您在您使用的所有文件副本或其中部分中包含下列內容：
+ 原始 W3C 文件的連結或 URL。
+ 原始作者的預先存在著作權聲明，或者如果不存在，則格式為「著作權 © 【date-of-document】 [全球資訊網協會](http://www.w3.org/) ([MIT](http://www.csail.mit.edu/)、[ERCIM](http://www.ercim.org/)、[Keio](http://www.keio.ac.jp/)、[Beihang](http://ev.buaa.edu.cn/)) 的通知 （偏好使用超文字，但允許使用文字表示）。[http://www.w3.org/Consortium/Legal/2015/doc-license](http://www.w3.org/Consortium/Legal/2015/doc-license)：//」
+ *如果存在*，則為 W3C 文件的 STATUS。

當空間允許時，應提供包含此**通知**的全文。我們請求在您根據本文件內容或其任何部分建立的任何軟體、文件或其他項目或產品中提供作者身分歸屬。

依據本授權，不授予建立 W3C 文件修改或衍生項目的權利，但下列除外：為了協助實作本文件所述的技術規格，任何人都可以在軟體中準備和分發衍生著作和本文件的一部分、支援隨附軟體的資料，以及在軟體的文件中，前提是所有此類著作都包含以下通知。不過，明確禁止發佈本文件的衍生著作做為技術規格使用。

此外，「程式碼元件」 — 區段中的 Web IDL 已清楚標示為 Web IDL；以及 W3C-defined標記 (HTML、CSS 等），以及清楚標示為程式碼範例的電腦程式設計語言程式碼，均依據 [W3C 軟體授權](http://www.w3.org/Consortium/Legal/copyright-software)進行授權。

通知為：

「Copyright © 2015 W3C® (MIT、ERCIM、Keio、Beihang)。此軟體或文件包含從 【W3C 文件的標題和 URI】 複製或衍生的資料。」

免責聲明

本文件「原狀」提供，著作權持有者不做任何明示或暗示的陳述或保證，包括但不限於對適銷性、符合特定用途、未侵權或所有權的保證；文件內容適用於任何用途；執行此類內容也不會侵犯任何第三方的專利、著作權、商標或其他權利。

著作權持有人對於因使用文件或其內容的效能或實作而產生的任何直接、間接、特殊或衍生性損害，概不負責。

未經特定事先書面許可，不得在與本文件或其內容相關的廣告或宣傳中使用著作權持有者的名稱和商標。本文件中的著作權所有權將隨時保留給著作權持有者。

# 標頭宣告
<a name="srgs-header"></a>

下表顯示文法槽類型支援的標頭宣告。如需詳細資訊，請參閱*語音辨識*[文法規格第 1 版 W3C 建議中的文法標頭宣告](https://www.w3.org/TR/speech-grammar/#S4.1)。 W3C 


| 宣告 | 規格要求 | XML 表單 | Amazon Lex 支援 | 規格 | 
| --- | --- | --- | --- | --- | 
| 文法版本 | 必要 | [4.3](https://www.w3.org/TR/speech-grammar/#S4.3)： grammar 元素上的version屬性 | 必要 | SRGS | 
| XML 命名空間 | 必要 （僅限 XML) | [4.3](https://www.w3.org/TR/speech-grammar/#S4.3)： grammar 元素上的xmlns屬性 | 必要 | SRGS | 
| 文件類型 | 必要 （僅限 XML) | [4.3](https://www.w3.org/TR/speech-grammar/#S4.3)：XML DOCTYPE | 建議 | SRGS | 
| 字元編碼 | 建議 | [4.4](https://www.w3.org/TR/speech-grammar/#S4.4)：XML 宣告中的encoding屬性 | 建議 | SRGS | 
| Language | 在語音模式下為必要 在 DTMF 模式中忽略 | [4.5](https://www.w3.org/TR/speech-grammar/#S4.5)： grammar 元素上的xml:lang屬性 | 在語音模式下為必要 在 DTMF 模式中忽略 | SRGS | 
| Mode | 選用 | [4.6](https://www.w3.org/TR/speech-grammar/#S4.6)： grammar 元素上的mode屬性 | 選用 | SRGS | 
| 根規則 | 選用 | [4.7](https://www.w3.org/TR/speech-grammar/#S4.7)： grammar 元素上的root屬性 | 必要 | SRGS | 
| 標籤格式 | 選用 | [4.8](https://www.w3.org/TR/speech-grammar/#S4.8)： grammar 元素上的tag-format屬性 | 支援字串常值和 ECMAScript  | SRGS、SISR | 
| 基礎 URI | 選用 | [4.9](https://www.w3.org/TR/speech-grammar/#S4.9)： grammar 元素上的xml:base屬性 | 選用 | SRGS | 
| 發音語彙 | 選用，允許多個 | [4.10](https://www.w3.org/TR/speech-grammar/#S4.`0)： lexicon 元素 | 不支援 | SRGS、PLS | 
| 中繼資料 | 選用，允許多個 | [4.11.1](https://www.w3.org/TR/speech-grammar/#S4.11.1)： meta 元素 | 必要 | SRGS | 
| XML 中繼資料 | 選用，僅限 XML | [4.11.2](https://www.w3.org/TR/speech-grammar/#S4.11.2)： metadata 元素 | 選用 | SRGS | 
| Tag | 選用，允許多個 | [4.12](https://www.w3.org/TR/speech-grammar/#S4.12)： tag 元素 | 不支援全域標籤 | SRGS | 

**範例**

```
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0//EN"
                  "http://www.w3.org/TR/speech-grammar/grammar.dtd">

<grammar xmlns="http://www.w3.org/2001/06/grammar"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xml:base="http://www.example.com/base-file-path"
         xsi:schemaLocation="http://www.w3.org/2001/06/grammar
                             http://www.w3.org/TR/speech-grammar/grammar.xsd"
         xml:lang="en-US"
         version="1.0"
         mode="voice"
         root="city"
         tag-format="semantics/1.0">
```

# 支援的 XML 元素
<a name="srgs-supported-xml"></a>

Amazon Lex V2 支援自訂文法的下列 XML 元素：
+ `<item>`
+ `<token>`
+ `<tag>`
+ `<one-of>`
+ `<rule-ref>`

# 權杖
<a name="srgs-tokens"></a>

下表顯示文法槽類型支援的字符規格。如需詳細資訊，請參閱*語音辨識文法規格第 1 版* W3C 建議中的[權杖](https://www.w3.org/TR/speech-grammar/#S2.1)。


| 權杖類型 | 範例 | 支援？ | 
| --- | --- | --- | 
| 單一未引用字符 | hello | 是 | 
| 單一未引用權杖：非字母 | 2 | 是 | 
| 單引號字符，無空格 | "hello" | 是，當只包含單一字符時捨棄雙引號 | 
| 以空格分隔的兩個字符 | 波本航次 | 是 | 
| 以空格分隔的四個字符 | 這是一個測試 | 是 | 
| 單引號字符，包括空格 | 「舊金山 | 否 | 
| <token> 標籤中的單一 XML 字符 | <token>San Francisco</token> | 否 （與空格的單引號相同） | 

**備註**
+ 包含*空格的單引號* – 規格要求以雙引號括住的字詞視為單一字符。Amazon Lex V2 會將它們視為空格分隔符記。
+ *<token> 中的單一 XML 字符* – 規格需要以 <token> 分隔的單字來代表一個字符。Amazon Lex V2 會將它們視為空格分隔符記。
+ 在文法中找到任一種用量時，Amazon Lex V2 會擲回驗證錯誤。

**範例**

```
<rule id="state" scope="public">
    <one-of>
        <item>FL</item>
        <item>MA</item>
        <item>NY</item>
    </one-of>
</rule>
```

# 規則參考
<a name="srgs-rule-reference"></a>

下表摘要說明文法文件中可能的各種規則參考形式。如需詳細資訊，請參閱*《語音辨識文法規格第 1 版* W3C 建議》中的[規則參考](https://www.w3.org/TR/speech-grammar/#S2.2)。


| 參考類型 | XML 表單 | 支援 | 
| --- | --- | --- | 
| [ 2.2.1 ](https://www.w3.org/TR/speech-grammar/#S2.2.1) 明確本機規則參考 | <ruleref uri="\$1rulename"/> | 是 | 
| [ 2.2.2 ](https://www.w3.org/TR/speech-grammar/#S2.2.2) URI 所識別文法命名規則的明確參考 [https://www.w3.org/TR/speech-grammar/#term-uri](https://www.w3.org/TR/speech-grammar/#term-uri) | <ruleref uri="grammarURI\$1rulename"/> | 否 | 
| [ 2.2.2 ](https://www.w3.org/TR/speech-grammar/#S2.2.2) URI 所識別文法根規則的隱含參考 [https://www.w3.org/TR/speech-grammar/#term-uri](https://www.w3.org/TR/speech-grammar/#term-uri) | <ruleref uri="grammarURI"/> | 否 | 
| [ 2.2.2 ](https://www.w3.org/TR/speech-grammar/#S2.2.2) 明確參考由具有[媒體類型的](https://www.w3.org/TR/speech-grammar/#term-media-type) [URI](https://www.w3.org/TR/speech-grammar/#term-uri) 識別的文法命名規則 | <ruleref uri="grammarURI\$1rulename" type="media-type"/> | 否 | 
| [ 2.2.2 ](https://www.w3.org/TR/speech-grammar/#S2.2.2) 隱含參考具有[媒體類型的](https://www.w3.org/TR/speech-grammar/#term-media-type) [URI](https://www.w3.org/TR/speech-grammar/#term-uri) 所識別文法的根規則 | <ruleref uri="grammarURI" type="media-type"/> | 否 | 
| [ 2.2.3 ](https://www.w3.org/TR/speech-grammar/#S2.2.3) 特殊規則定義 | `<ruleref special="NULL"/>` `<ruleref special="VOID"/>` `<ruleref special="GARBAGE"/>` | 否 | 

**備註**

1. 文法 URI 是外部 URI。例如 `http://grammar.example.com/world-cities.grxml`。

1. 媒體類型可以是：
   + `application/srgs+xml`
   + `text/plain`

**範例**

```
<rule id="city" scope="public">
    <one-of>
        <item>Boston</item>
        <item>Philadelphia</item>
        <item>Fargo</item>
    </one-of>
</rule>

<rule id="state" scope="public">
    <one-of>
        <item>FL</item>
        <item>MA</item>
        <item>NY</item>
    </one-of>
</rule>

<!-- "Boston MA" -> city = Boston, state = MA -->
<rule id="city_state" scope="public">
    <ruleref uri="#city"/> <ruleref uri="#state"/>
</rule>
```

# 序列和封裝
<a name="srgs-sequence"></a>

下列範例顯示支援的序列。如需詳細資訊，請參閱*語音辨識文法規格第 1 版* W3C 建議中的[序列和封裝](https://www.w3.org/TR/speech-grammar/#S2.3)。

**範例**

```
<!-- sequence of tokens -->
this is a test

<!--sequence of rule references-->
<ruleref uri="#action"/> <ruleref uri="#object"/>

<!--sequence of tokens and rule references-->
the <ruleref uri="#object"/> is <ruleref uri="#color"/>

<!-- sequence container -->
<item>fly to <ruleref uri="#city"/> </item>
```

# 重複
<a name="srgs-repeats"></a>

下表顯示規則支援的重複擴展。如需詳細資訊，請參閱*語音辨識文法規格第 1 版* W3C 建議的[重複](https://www.w3.org/TR/speech-grammar/#S2.5)。


| XML 表單範例 | Behavior (行為) | 支援？ | 
| --- | --- | --- | 
| *repeat="n"* repeat="6" | 包含的表達式只會重複「n」次。「n」必須是「0」或正整數。 | 是 | 
| *repeat="m-n"* repeat="4-6" | 在 "m" 和 "n" 次 （含） 之間重複包含的擴展。"m" 和 "n" 必須同時為 "0" 或正整數，而 "m" 必須小於或等於 "n"。 | 是 | 
| *repeat="m-"* repeat="3-" | 包含的擴展會重複「m」次或更多次 （包含）。「m」必須是「0」或姿勢整數。例如，「3-」宣告包含的擴展可能發生三次、四次、五次或更多次。 | 是 | 
| *repeat="0-1"*  | 包含的擴展是選用的。 | 是 | 
| <item repeat="2-4" repeat-prob="0.8"> |   | 否 | 

# Language
<a name="srgs-language"></a>

下列討論適用於套用至文法的語言識別符。如需詳細資訊，請參閱*語音辨識文法規格第 1 版* W3C 建議中的[語言](https://www.w3.org/TR/speech-grammar/#S2.7)。

根據預設，文法是單一語言文件，具有[文法標頭](https://www.w3.org/TR/speech-grammar/#S4.1)中語言宣告中提供的語言[識別符](https://www.w3.org/TR/speech-grammar/#term-language)。除非另有宣告，否則該文法中的所有字符**都會根據文法的語言處理**。**不支援**文法層級語言宣告。

於下列範例中：

1. Amazon Lex V2 **支援**「en-US」語言的文法標頭宣告。

1. **不支援**項目層級語言附件 （以*紅色*反白顯示）。如果語言附件與標頭宣告不同，Amazon Lex V2 會擲回驗證錯誤。

```
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0//EN"
                  "http://www.w3.org/TR/speech-grammar/grammar.dtd">

<!-- the default grammar language is US English -->
<grammar xmlns="http://www.w3.org/2001/06/grammar"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.w3.org/2001/06/grammar
                             http://www.w3.org/TR/speech-grammar/grammar.xsd"
         xml:lang="en-US" version="1.0">

  <!--
     single language attachment to tokens
     "yes" inherits US English language
     "oui" is Canadian French language
  -->
  <rule id="yes">
    <one-of>
      <item>yes</item>
      <item xml:lang="fr-CA">oui</item>
    </one-of>
  </rule>

  <!-- Single language attachment to an expansion -->
  <rule id="people1">
    <one-of xml:lang="fr-CA">
      <item>Michel Tremblay</item>
      <item>André Roy</item>
    </one-of>
  </rule>
</grammar>
```

# Tags (標籤)
<a name="srgs-tags"></a>

下列討論適用於為文法定義的標籤。如需詳細資訊，請參閱*語音辨識文法規格第 1 版* W3C 建議中的[標籤](https://www.w3.org/TR/speech-grammar/#S2.6)。

根據 SRGS 規格，標籤可以透過下列方式定義：

1. 做為標頭宣告的一部分，如中所述[標頭宣告](srgs-header.md)。

1. 作為 *<rule>* 定義的一部分。

支援下列標籤格式：
+ `semantics/1.0` (SISR、ECMAScript)
+ `semantics/1.0-literals` (SISR 字串常值）

不支援下列標籤格式：
+ `swi-semantics/1.0` (Nuance 專屬）

**範例**

```
<grammar xmlns="http://www.w3.org/2001/06/grammar"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xml:base="http://www.example.com/base-file-path"
         xsi:schemaLocation="http://www.w3.org/2001/06/grammar
                             http://www.w3.org/TR/speech-grammar/grammar.xsd"
         xml:lang="en-US"
         version="1.0"
         mode="voice"
         root="city"
         tag-format="semantics/1.0-literals">
    <rule id="no">
        <one-of>
            <item>no</item>
            <item>nope</item>
            <item>no way</item>
        </one-of>
        <tag>no</tag>
    </rule>
</grammar>
```

# 權重
<a name="grammar-weights"></a>

您可以將*權重*屬性新增至 元素。權重是正浮點值，代表語音辨識期間項目中片語提升的程度。如需詳細資訊，請參閱語音辨識文法規格版本 1 W3C 建議的[權重](https://www.w3.org/TR/speech-grammar/)。

權重必須大於 0 且小於或等於 10，且只能有一個小數位數。如果權重大於 0 且小於 1，則片語會負面提升。如果權重大於 1 且小於或等於 10，則片語會正面提升。權重 1 等於完全不給予權重，而且片語沒有提升。

為改善語音辨識效能的項目指派適當的權重是一項困難的任務。以下是您可以遵循指派權重的一些秘訣：
+ 從未指派項目權重的文法開始。
+ 判斷語音中哪些模式經常被錯誤識別。
+ 套用不同的權重值，直到您注意到語音辨識效能有所改善，而且沒有迴歸為止。

**範例 1**

例如，如果您有機場的文法，而且您發現*紐約*經常被誤識別為*紐瓦克*，您可以指派 5 的權重來積極提升紐約。

```
<rule> id="airport">
    <one-of>
        <item>
            Boston
            <tag>out="Boston"</tag>
        </item>
        <item weight="5">
            New York
            <tag>out="New York"</tag>
        </item>
        <item>
            Newark
            <tag>out="Newark"</tag>
        </item>
    </one-of>
</rule>
```

**範例 2**

例如，您有一個航空公司預訂代碼的文法，開頭是英文字母，後面接著三位數字。保留代碼很可能以 B 或 D 開頭，但您發現 B 經常被錯誤識別為 P，而 D 為 T。您可以正面提升 B 和 D。

```
<rule> id="alphabet">
    <one-of>
        <item>A<tag>out.letters+='A';</tag></item>
        <item weight="3.5">B<tag>out.letters+='B';</tag></item>
        <item>C<tag>out.letters+='C';</tag></item>
        <item weight="2.9">D<tag>out.letters+='D';</tag></item>
        <item>E<tag>out.letters+='E';</tag></item>
        <item>F<tag>out.letters+='F';</tag></item>
        <item>G<tag>out.letters+='G';</tag></item>
        <item>H<tag>out.letters+='H';</tag></item>
        <item>I<tag>out.letters+='I';</tag></item>
        <item>J<tag>out.letters+='J';</tag></item>
        <item>K<tag>out.letters+='K';</tag></item>
        <item>L<tag>out.letters+='L';</tag></item>
        <item>M<tag>out.letters+='M';</tag></item>
        <item>N<tag>out.letters+='N';</tag></item>
        <item>O<tag>out.letters+='O';</tag></item>
        <item>P<tag>out.letters+='P';</tag></item>
        <item>Q<tag>out.letters+='Q';</tag></item>
        <item>R<tag>out.letters+='R';</tag></item>
        <item>S<tag>out.letters+='S';</tag></item>
        <item>T<tag>out.letters+='T';</tag></item>
        <item>U<tag>out.letters+='U';</tag></item>
        <item>V<tag>out.letters+='V';</tag></item>
        <item>W<tag>out.letters+='W';</tag></item>
        <item>X<tag>out.letters+='X';</tag></item>
        <item>Y<tag>out.letters+='Y';</tag></item>
        <item>Z<tag>out.letters+='Z';</tag></item>
    </one-of>
</rule>
```