

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

# 创建词汇表过滤器
<a name="vocabulary-filter-create"></a>

有两个选项可用于创建自定义词汇表过滤器：

1. 将行分隔的单词列表保存为带 UTF-8 编码的纯文本文件。
   + 您可以在 AWS 管理控制台、 AWS CLI或 AWS SDK 中使用这种方法。
   + 如果使用 AWS 管理控制台，则可以为自定义词汇文件提供本地路径或 Amazon S3 URI。
   + 如果使用 AWS CLI 或 AWS SDK，则必须将自定义词汇文件上传到 Amazon S3 存储桶，并在请求中包含 Amazon S3 URI。

1. 在您的 API 请求中直接包含以逗号分隔的单词列表。
   + 您可以使用[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabularyFilter.html#transcribe-CreateVocabularyFilter-request-Words](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabularyFilter.html#transcribe-CreateVocabularyFilter-request-Words)参数将此方法与 AWS CLI 或 S AWS DK 配合使用。

有关每种方法的示例，请参阅[创建自定义词汇表过滤器](#vocabulary-filtering-create-examples)

创建自定义词汇表过滤器时需要注意的事项：
+ 单词不区分大小写。例如，“curse”和“CURSE”被认为是同一个单词。
+ 只有精确的单词匹配才会被过滤。例如，如果您的过滤器包含“swear”，但您的媒体包含“swears”或“swearing”，则这些单词不会被过滤。只有“swear”才会被过滤出来。因此，您必须包括要过滤的单词的所有变体。
+ 过滤器不适用于包含在其它单词中的单词。例如，如果自定义词汇表过滤器包含“marine”，但不包含“submarine”，则“submarine”在转录中不会更改。
+ 每个条目只能包含一个单词（没有空格）。
+ 如果将自定义词汇过滤器另存为文本文件，则必须采用带 UTF-8 编码的纯文本格式。
+ 每个词汇过滤器最多可以有 100 个 AWS 账户 ，每个词汇过滤器的大小可达 50 Kb。
+ 您只能使用您的语言支持的字符。有关详细信息，请参阅您的语言的[字符集](charsets.md)。

## 创建自定义词汇表过滤器
<a name="vocabulary-filtering-create-examples"></a>

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

### AWS 管理控制台
<a name="vocab-filter-create-console-batch"></a>

在继续操作之前，请将您的自定义词汇表过滤器另存为文本 (\*.txt) 文件。您可以选择将文件上传到 Amazon S3 存储桶。

1. 登录到 [AWS 管理控制台](https://console.aws.amazon.com/transcribe/)。

1. 在导航窗格中，选择 **Vocabulary filtering (词汇筛选)**。此时将会打开**词汇表过滤器**页面，您可以在该页面中查看现有自定义词汇表过滤器或创建新过滤器。

1. 选择**创建词汇表过滤器**。  
![Amazon Transcribe 控制台屏幕截图：“词汇过滤器” 页面。](http://docs.aws.amazon.com/zh_cn/transcribe/latest/dg/images/vocab-filter-create-console.png)

   这会将您带至**创建词汇表过滤器**页面。为新的自定义词汇表过滤器输入名称。

   在**词汇表输入源**下选择**文件上传**或 **S3 位置**选项。然后指定自定义词汇表文件的位置。  
![Amazon Transcribe 控制台屏幕截图：“创建词汇过滤器” 页面。](http://docs.aws.amazon.com/zh_cn/transcribe/latest/dg/images/vocab-filter-create-console-upload.png)

1. 您也可以为自定义词汇表过滤器添加标签。完成所有字段后，请选择页面底部的**创建词汇表过滤器**。如果在处理文件时没有出现错误，则会将您带至**词汇表过滤器**页面。

   现在您的自定义词汇表过滤器已准备就绪，可以使用了。

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

此示例使用 [create-vocabulary-filter](https://docs.aws.amazon.com/cli/latest/reference/transcribe/create-vocabulary-filter.html) 命令将单词列表处理为可用的自定义词汇表过滤器。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabularyFilter.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabularyFilter.html)。

**选项 1**：您可以使用 `words` 参数将单词列表包含在请求中。

```
aws transcribe create-vocabulary-filter \ 
--vocabulary-filter-name {{my-first-vocabulary-filter}} \ 
--language-code {{en-US}} \ 
--words {{profane,offensive,Amazon,Transcribe}}
```

**选项 2**：您可以将单词列表另存为文本文件并将其上传到 Amazon S3 存储桶，然后使用 `vocabulary-filter-file-uri` 参数在请求中包含该文件的 URI。

```
aws transcribe create-vocabulary-filter \ 
--vocabulary-filter-name {{my-first-vocabulary-filter}} \ 
--language-code {{en-US}} \ 
--vocabulary-filter-file-uri s3://{{amzn-s3-demo-bucket}}/{{my-vocabulary-filters}}/{{my-vocabulary-filter}}.txt
```

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

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

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

**选项 1**：您可以使用 `Words` 参数将单词列表包含在请求中。

```
{
  "VocabularyFilterName": "{{my-first-vocabulary-filter}}",
  "LanguageCode": "{{en-US}}",
  "Words": [
        "{{profane}}","{{offensive}}","{{Amazon}}","{{Transcribe}}"
  ]
}
```

**选项 2**：您可以将单词列表另存为文本文件并将其上传到 Amazon S3 存储桶，然后使用 `VocabularyFilterFileUri` 参数在请求中包含该文件的 URI。

```
{
  "VocabularyFilterName": "{{my-first-vocabulary-filter}}",
  "LanguageCode": "{{en-US}}",
  "VocabularyFilterFileUri": "s3://{{amzn-s3-demo-bucket}}/{{my-vocabulary-filters}}/{{my-vocabulary-filter}}.txt"
}
```

**注意**  
如果您在请求中包含 `VocabularyFilterFileUri`，则无法使用 `Words`；您必须选择其中一个。

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

此示例使用 create\_ 适用于 Python (Boto3) 的 AWS SDK vocabulary [\_filter 方法创建自定义词汇](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/transcribe.html#TranscribeService.Client.create_vocabulary_filter)过滤器。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabularyFilter.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_CreateVocabularyFilter.html)。

有关使用 AWS 软件开发工具包的其他示例，包括特定功能、场景和跨服务示例，请参阅本章。[使用 Amazon Transcribe 的代码示例 AWS 软件开发工具包](service_code_examples.md)

**选项 1**：您可以使用 `Words` 参数将单词列表包含在请求中。

```
from __future__ import print_function
import time
import boto3
transcribe = boto3.client('transcribe', '{{us-west-2}}')
vocab_name = "{{my-first-vocabulary-filter}}"
response = transcribe.create_vocabulary_filter(
    LanguageCode = '{{en-US}}',
    VocabularyFilterName = vocab_name,
    Words = [        
        '{{profane}}','{{offensive}}','{{Amazon}}','{{Transcribe}}'
    ]
)
```

**选项 2**：您可以将单词列表另存为文本文件并将其上传到 Amazon S3 存储桶，然后使用 `VocabularyFilterFileUri` 参数在请求中包含该文件的 URI。

```
from __future__ import print_function
import time
import boto3
transcribe = boto3.client('transcribe', '{{us-west-2}}')
vocab_name = "{{my-first-vocabulary-filter}}"
response = transcribe.create_vocabulary_filter(
    LanguageCode = '{{en-US}}',
    VocabularyFilterName = vocab_name,
    VocabularyFilterFileUri = 's3://{{amzn-s3-demo-bucket}}/{{my-vocabulary-filters}}/{{my-vocabulary-filter}}.txt'
)
```

**注意**  
如果您在请求中包含 `VocabularyFilterFileUri`，则无法使用 `Words`；您必须选择其中一个。

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