AWSSDK Amazon Comprehend 文書分類ジョブを開始する - AWSSDK コードサンプル

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります


AWSSDK Amazon Comprehend 文書分類ジョブを開始する

次のコード例は、Amazon Comprehend ドキュメント分類ジョブを開始する方法を示しています。

SDK for Python (Boto3)

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

class ComprehendClassifier: """Encapsulates an Amazon Comprehend custom classifier.""" def __init__(self, comprehend_client): """ :param comprehend_client: A Boto3 Comprehend client. """ self.comprehend_client = comprehend_client self.classifier_arn = None def start_job( self, job_name, input_bucket, input_key, input_format, output_bucket, output_key, data_access_role_arn): """ Starts a classification job. The classifier must be trained or the job will fail. Input is read from the specified Amazon S3 input bucket and written to the specified output bucket. Output data is stored in a tar archive compressed in gzip format. The job runs asynchronously, so you can call `describe_document_classification_job` to get job status until it returns a status of SUCCEEDED. :param job_name: The name of the job. :param input_bucket: The Amazon S3 bucket that contains input data. :param input_key: The prefix used to find input data in the input bucket. If multiple objects have the same prefix, all of them are used. :param input_format: The format of the input data, either one document per file or one document per line. :param output_bucket: The Amazon S3 bucket where output data is written. :param output_key: The prefix prepended to the output data. :param data_access_role_arn: The Amazon Resource Name (ARN) of a role that grants Comprehend permission to read from the input bucket and write to the output bucket. :return: Information about the job, including the job ID. """ try: response = self.comprehend_client.start_document_classification_job( DocumentClassifierArn=self.classifier_arn, JobName=job_name, InputDataConfig={ 'S3Uri': f's3://{input_bucket}/{input_key}', 'InputFormat': input_format.value}, OutputDataConfig={'S3Uri': f's3://{output_bucket}/{output_key}'}, DataAccessRoleArn=data_access_role_arn) "Document classification job %s is %s.", job_name, response['JobStatus']) except ClientError: logger.exception("Couldn't start classification job %s.", job_name) raise else: return response
  • API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいStartDocumentClassificationJob