

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

# 啟動自訂實體偵測任務 (API)
<a name="detecting-cer-async-api"></a>

您可以使用 API 啟動和監控自訂實體辨識的非同步分析任務。

若要使用 [StartEntitiesDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartEntitiesDetectionJob.html) 操作啟動自訂實體偵測任務，您需要提供 EntityRecognizerArn，這是訓練模型的 Amazon Resource Name (ARN)。您可以在 [CreateEntityRecognizer](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_CreateEntityRecognizer.html) 操作的回應中找到此 ARN。

**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
<a name="detecting-cer-async-api-cli"></a>

針對 Unix、Linux 和 macOS 環境使用以下範例。用於 Windows 時，請以插入號 (^) 取代每一行結尾處的 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
<a name="detecting-cer-async-api-java"></a>

如需使用 Java 的 Amazon Comprehend 範例，請參閱 [Amazon Comprehend Java 範例](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/comprehend)。

## 使用 偵測自訂實體 適用於 Python (Boto3) 的 AWS SDK
<a name="detecting-cer-async-api-python"></a>

此範例會建立自訂實體辨識器、訓練模型，然後使用 在實體辨識器任務中執行 適用於 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()
```

等待實體辨識器達到 TRAINED 狀態：

```
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 動作
<a name="detecting-cer-api-pdf"></a>

對於映像檔案和 PDF 檔案，您可以使用 中的 `DocumentReaderConfig` 參數覆寫預設擷取動作`InputDataConfig`。

下列範例定義名為 myInputDataConfig.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`操作中，指定 myInputDataConfig.json 檔案做為 `InputDataConfig` 參數：

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

如需`DocumentReaderConfig`參數的詳細資訊，請參閱 [設定文字擷取選項](idp-set-textract-options.md)。