

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

# 训练自定义实体识别器 (API)
<a name="train-cer-model"></a>

要创建和训练自定义实体识别模型，请使用 Amazon Comprehend API [CreateEntityRecognizer](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_CreateEntityRecognizer.html)操作

**Topics**
+ [使用训练自定义实体识别器 AWS Command Line Interface](#get-started-api-cer-cli)
+ [使用训练自定义实体识别器 适用于 Java 的 AWS SDK](#get-started-api-cer-java)
+ [使用 Python (Boto3) 训练自定义实体识别器](#cer-python)

## 使用训练自定义实体识别器 AWS Command Line Interface
<a name="get-started-api-cer-cli"></a>

以下示例演示如何使用该`CreateEntityRecognizer`操作以及 APIs 与相关的其他操作 AWS CLI。

此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows，请将每行末尾的反斜杠 (\$1) Unix 行继续符替换为脱字号 (^)。

使用 `create-entity-recognizer` CLI 命令创建自定义实体识别器。有关 input-data-config参数的信息，请参阅《*亚马逊 Comprehend* API 参考》[CreateEntityRecognizer](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_CreateEntityRecognizer.html)中的。

```
aws comprehend create-entity-recognizer \
     --language-code en \
     --recognizer-name test-6 \
     --data-access-role-arn "arn:aws:iam::account number:role/service-role/AmazonComprehendServiceRole-role" \
     --input-data-config "EntityTypes=[{Type=PERSON}],Documents={S3Uri=s3://Bucket Name/Bucket Path/documents},
                Annotations={S3Uri=s3://Bucket Name/Bucket Path/annotations}" \
     --region region
```

使用 `list-entity-recognizers` CLI 命令列出一个区域中的所有实体识别器。

```
aws comprehend list-entity-recognizers \
     --region region
```

使用 `describe-entity-recognizer` CLI 命令检查自定义实体识别器的任务状态。

```
aws comprehend describe-entity-recognizer \
     --entity-recognizer-arn arn:aws:comprehend:region:account number:entity-recognizer/test-6 \
     --region region
```

## 使用训练自定义实体识别器 适用于 Java 的 AWS SDK
<a name="get-started-api-cer-java"></a>

此示例使用 Java 创建自定义实体识别器并训练模型

有关使用 Java 的 Amazon Comprehend 示例，请参阅 [Amazon Comprehend Java 示例](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/comprehend)。

## 使用 Python (Boto3) 训练自定义实体识别器
<a name="cer-python"></a>

实例化 Boto3 SDK：

```
import boto3
import uuid
comprehend = boto3.client("comprehend", region_name="region")
```

创建实体识别器：

```
response = comprehend.create_entity_recognizer(
    RecognizerName="Recognizer-Name-Goes-Here-{}".format(str(uuid.uuid4())),
    LanguageCode="en",
    DataAccessRoleArn="Role ARN",
    InputDataConfig={
        "EntityTypes": [
            {
                "Type": "ENTITY_TYPE"
            }
        ],
        "Documents": {
            "S3Uri": "s3://Bucket Name/Bucket Path/documents"
        },
        "Annotations": {
            "S3Uri": "s3://Bucket Name/Bucket Path/annotations"
        }
    }
)
recognizer_arn = response["EntityRecognizerArn"]
```

列出所有识别器：

```
response = comprehend.list_entity_recognizers()
```

等待识别器达到“已训练”状态：

```
while True:
    response = comprehend.describe_entity_recognizer(
        EntityRecognizerArn=recognizer_arn
    )

    status = response["EntityRecognizerProperties"]["Status"]
    if "IN_ERROR" == status:
        sys.exit(1)
    if "TRAINED" == status:
        break

    time.sleep(10)
```