

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用列表创建自定义词汇表
<a name="custom-vocabulary-create-list"></a>

**重要**  
不推荐使用列表格式的自定义词汇表，因此，如果您要创建新的自定义词汇表，我们强烈建议您使用[表格格式](custom-vocabulary-create-table.md)。

您可以使用 AWS 管理控制台、AWS CLI 或 AWS SDK 以列表格式创建自定义词汇表。
+ **AWS 管理控制台**：您必须创建并上传包含自定义词汇表的文本文件。您可以使用行分隔或逗号分隔的条目。请注意，您的列表必须以 `LF` 格式另存为文本 (\$1.txt) 文件。如果您使用任何其它格式，例如 `CRLF`，则 Amazon Transcribe 不接受您的自定义词汇表。
+ **AWS CLI** 和 **AWS SDK**：您必须使用 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html#transcribe-CreateVocabulary-request-Phrases](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html#transcribe-CreateVocabulary-request-Phrases) 标志将自定义词汇表作为逗号分隔的条目包含在 API 调用中。

如果一个条目包含多个单词，则必须用连字符连接每个单词。例如，以 **Los-Angeles** 形式添加“Los Angeles”，以 **Andorra-la-Vella** 形式添加“Andorra la Vella”。

以下是两种有效列表格式的示例。有关特定于方法的示例，请参阅[创建自定义词汇表](#custom-vocabulary-create-list-examples)。
+ 逗号分隔的条目：

  ```
  Los-Angeles,CLI,Eva-Maria,ABCs,Andorra-la-Vella
  ```
+ 行分隔的条目：

  ```
  Los-Angeles
  CLI
  Eva-Maria
  ABCs
  Andorra-la-Vella
  ```

**重要**  
您只能使用您的语言支持的字符。有关详细信息，请参阅您的语言的[字符集](charsets.md)。

[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateMedicalVocabulary.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateMedicalVocabulary.html) 操作不支持自定义词汇表列表。如果要创建自定义医学词汇表，则必须使用表格格式；有关说明，请参阅[使用表格创建自定义词汇表](custom-vocabulary-create-table.md)。

## 创建自定义词汇表
<a name="custom-vocabulary-create-list-examples"></a>

要处理用于 Amazon Transcribe 的自定义词汇表列表，请参阅以下示例：

### AWS CLI
<a name="vocab-create-list-cli"></a>

此示例结合列表格式的自定义词汇表文件使用 [create-vocabulary](https://docs.aws.amazon.com/cli/latest/reference/transcribe/create-vocabulary.html) 命令。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html)。

```
aws transcribe create-vocabulary \ 
--vocabulary-name my-first-vocabulary \ 
--language-code en-US \ 
--phrases {CLI,Eva-Maria,ABCs}
```

以下是另一个使用 [create-vocabulary](https://docs.aws.amazon.com/cli/latest/reference/transcribe/create-vocabulary.html) 命令的示例，以及创建自定义词汇表的请求正文。

```
aws transcribe create-vocabulary \
--cli-input-json file://filepath/my-first-vocab-list.json
```

*my-first-vocab-list.json* 文件包含以下请求正文。

```
{
  "VocabularyName": "my-first-vocabulary",
  "LanguageCode": "en-US",
  "Phrases": [
        "CLI","Eva-Maria","ABCs"
  ]
}
```

`VocabularyState` 从 `PENDING` 变为 `READY` 后，您的自定义词汇表就可以用于转录了。要查看自定义词汇表的当前状态，请运行：

```
aws transcribe get-vocabulary \
--vocabulary-name my-first-vocabulary
```

### 适用于 Python (Boto3) 的 AWS SDK
<a name="vocab-create-list-python-batch"></a>

此示例通过 [create\$1vocabulary](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/transcribe.html#TranscribeService.Client.create_vocabulary) 方法使用 适用于 Python (Boto3) 的 AWS SDK 以列表格式创建自定义词汇表。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html)。

有关使用 AWS SDK 的其它示例，包括特定特征、场景和跨服务示例，请参阅[使用 Amazon Transcribe 的代码示例 AWS SDKs](service_code_examples.md)一章。

```
from __future__ import print_function
import time
import boto3
transcribe = boto3.client('transcribe', 'us-west-2')
vocab_name = "my-first-vocabulary"
response = transcribe.create_vocabulary(
    LanguageCode = 'en-US',
    VocabularyName = vocab_name,
    Phrases = [
        'CLI','Eva-Maria','ABCs'
    ]
)

while True:
    status = transcribe.get_vocabulary(VocabularyName = vocab_name)
    if status['VocabularyState'] in ['READY', 'FAILED']:
        break
    print("Not ready yet...")
    time.sleep(5)
print(status)
```

**注意**  
如果您为自定义词汇表文件创建新的 Amazon S3 存储桶，请确保发出 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabulary.html) 请求的 IAM 角色有权访问此存储桶。如果角色不具备相应的权限，则请求将失败。您可以选择在请求中包含 `DataAccessRoleArn` 参数来指定 IAM 角色。有关 Amazon Transcribe 中的 IAM 角色和策略的更多信息，请参阅[Amazon Transcribe 基于身份的策略示例](security_iam_id-based-policy-examples.md)。