啟動自訂實體偵測工作 (API) - Amazon Comprehend

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

啟動自訂實體偵測工作 (API)

您可以使用 API 啟動和監視非同步分析工作,以進行自訂實體辨識。

若要透過StartEntitiesDetectionJob作業啟動自訂實體偵測任務 EntityRecognizerArn,請提供訓練模型的 Amazon 資源名稱 (ARN)。您可以在對CreateEntityRecognizer操作的響應中找到此 ARN。

使用偵測自訂實體 AWS Command Line Interface

以下範例適用於 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 會回應和,JobIDJobStatus並會傳回您在請求中指定之 S3 儲存貯體中任務的輸出。

使用偵測自訂實體 AWS SDK for Java

對於使用 Java Amazon Comprehend 的例子,請參閱 Amazon Comprehend Java 的例子。

使用偵測自訂實體 AWS SDK for Python (Boto3)

此範例會建立自訂實體辨識器、訓練模型,然後在實體辨識器工作中使用. AWS SDK for Python (Boto3)

為 Python 實例化開發套件。

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 檔案,您可以使用中的DocumentReaderConfig參數來取代預設萃取動作InputDataConfig

下列範例會定義名為 myInputData Config.json 的 JSON 檔案來設定值InputDataConfig。它設置DocumentReadConfig為使用 Amazon Textract 取 DetectDocumentText API 的所有 PDF 文件。

"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組態 .json 檔案做為參數InputDataConfig

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

如需DocumentReaderConfig參數的詳細資訊,請參閱設定文字擷取選項