

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

# 启动自定义实体检测任务 (API)
分析任务 (API)

您可以使用 API 启动和监控异步分析任务，以进行自定义实体识别。

要使用该[StartEntitiesDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartEntitiesDetectionJob.html)操作启动自定义实体检测任务，请提供 EntityRecognizerArn，即训练模型的 Amazon 资源名称 (ARN)。你可以在对操作的响应中找到此 ARN。[CreateEntityRecognizer](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_CreateEntityRecognizer.html)

**Topics**
+ [

## 使用检测自定义实体 AWS Command Line Interface
](#detecting-cer-async-api-cli)
+ [

## 使用检测自定义实体 适用于 Java 的 AWS SDK
](#detecting-cer-async-api-java)
+ [

## 使用检测自定义实体 适用于 Python (Boto3) 的 AWS SDK
](#detecting-cer-async-api-python)
+ [

## 覆盖 PDF 文件的 API 操作
](#detecting-cer-api-pdf)

## 使用检测自定义实体 AWS Command Line Interface
使用 AWS CLI

以下示例适用 Unix、Linux 和 macOS 环境。对于 Windows，请将每行末尾的反斜杠 (\$1) Unix 行继续符替换为脱字号 (^)。要检测文档集中的自定义实体，请使用以下请求语法：

```
aws comprehend start-entities-detection-job \
     --entity-recognizer-arn "arn:aws:comprehend:region:account number:entity-recognizer/test-6" \
     --job-name infer-1 \
     --data-access-role-arn "arn:aws:iam::account number:role/service-role/AmazonComprehendServiceRole-role" \
     --language-code en \
     --input-data-config "S3Uri=s3://Bucket Name/Bucket Path" \
     --output-data-config "S3Uri=s3://Bucket Name/Bucket Path/" \
     --region region
```

Amazon Comprehend 使用 `JobID` 和 `JobStatus` 作为响应，并将返回您在请求中指定的 S3 存储桶中任务的输出。

## 使用检测自定义实体 适用于 Java 的 AWS SDK
使用适用于 Java 的 SDK

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

## 使用检测自定义实体 适用于 Python (Boto3) 的 AWS SDK
使用 Python SDK

此示例创建自定义实体识别器，训练模型，然后使用 适用于 Python (Boto3) 的 AWS SDK在实体识别器任务中运行该模型。

实例化适用于 Python 的 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)
```

启动自定义实体检测任务：

```
response = comprehend.start_entities_detection_job(
    EntityRecognizerArn=recognizer_arn,
    JobName="Detection-Job-Name-{}".format(str(uuid.uuid4())),
    LanguageCode="en",
    DataAccessRoleArn="Role ARN",
    InputDataConfig={
        "InputFormat": "ONE_DOC_PER_LINE",
        "S3Uri": "s3://Bucket Name/Bucket Path/documents"
    },
    OutputDataConfig={
        "S3Uri": "s3://Bucket Name/Bucket Path/output"
    }
)
```

## 覆盖 PDF 文件的 API 操作


对于图像文件和 PDF 文件，您可以使用 `InputDataConfig` 中的 `DocumentReaderConfig` 参数覆盖默认的提取操作。

以下示例定义了一个名为 myInputData config.json 的 JSON 文件来设置这些`InputDataConfig`值。它将 `DocumentReadConfig` 设置为对所有 PDF 文件使用 Amazon Textract `DetectDocumentText` API。

**Example**  

```
"InputDataConfig": {
  "S3Uri": s3://Bucket Name/Bucket Path",
  "InputFormat": "ONE_DOC_PER_FILE",
  "DocumentReaderConfig": {
      "DocumentReadAction": "TEXTRACT_DETECT_DOCUMENT_TEXT",
      "DocumentReadMode": "FORCE_DOCUMENT_READ_ACTION"
  }
}
```

在`StartEntitiesDetectionJob`操作中，将 myInputData config.json 文件指定为参数：`InputDataConfig`

```
  --input-data-config file://myInputDataConfig.json  
```

有关 `DocumentReaderConfig` 参数的更多信息，请参阅 [设置文本提取选项](idp-set-textract-options.md)。