本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
若要建立和訓練自訂實體辨識模型,請使用 Amazon Comprehend CreateEntityRecognizer API 操作
使用 訓練自訂實體識別器 AWS Command Line Interface
下列範例示範搭配 使用 CreateEntityRecognizer
操作和其他相關聯的 APIs AWS CLI。
這些範例已針對 Unix、Linux 和 macOS 格式化。用於 Windows 時,請以插入號 (^) 取代每一行結尾處的 Unix 接續字元斜線 (\)。
使用 CLI create-entity-recognizer
命令建立自訂實體識別器。如需有關 input-data-config 參數的資訊,請參閱《Amazon Comprehend API 參考》中的 CreateEntityRecognizer。
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}" \ --regionregion
使用 CLI list-entity-recognizers
命令列出區域中的所有實體識別符。
aws comprehend list-entity-recognizers \ --region
region
使用 CLI describe-entity-recognizer
命令檢查自訂實體識別器的任務狀態。
aws comprehend describe-entity-recognizer \ --entity-recognizer-arn arn:aws:comprehend:
region
:account number
:entity-recognizer/test-6 \ --regionregion
使用 訓練自訂實體識別器 適用於 Java 的 AWS SDK
此範例會使用 Java 建立自訂實體識別器並訓練模型
如需使用 Java 的 Amazon Comprehend 範例,請參閱 Amazon Comprehend Java 範例
使用 Python 訓練自訂實體識別器 (Boto3)
執行個體化 Boto3 開發套件:
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)