Use StartDocumentClassificationJob
with an AWS SDK or CLI
The following code examples show how to use StartDocumentClassificationJob
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:
- CLI
-
- AWS CLI
-
To start document classification job
The following
start-document-classification-job
example starts a document classification job with a custom model on all of the files at the address specified by the--input-data-config
tag. In this example, the input S3 bucket containsSampleSMStext1.txt
,SampleSMStext2.txt
, andSampleSMStext3.txt
. The model was previously trained on document classifications of spam and non-spam, or, "ham", SMS messages. When the job is complete,output.tar.gz
is put at the location specified by the--output-data-config
tag.output.tar.gz
containspredictions.jsonl
which lists the classification of each document. The Json output is printed on one line per file, but is formatted here for readability.aws comprehend start-document-classification-job \ --job-name
exampleclassificationjob
\ --input-data-config"S3Uri=s3://DOC-EXAMPLE-BUCKET-INPUT/jobdata/"
\ --output-data-config"S3Uri=s3://DOC-EXAMPLE-DESTINATION-BUCKET/testfolder/"
\ --data-access-role-arnarn:aws:iam::111122223333:role/service-role/AmazonComprehendServiceRole-example-role
\ --document-classifier-arnarn:aws:comprehend:us-west-2:111122223333:document-classifier/mymodel/version/12
Contents of
SampleSMStext1.txt
:"CONGRATULATIONS! TXT 2155550100 to win $5000"
Contents of
SampleSMStext2.txt
:"Hi, when do you want me to pick you up from practice?"
Contents of
SampleSMStext3.txt
:"Plz send bank account # to 2155550100 to claim prize!!"
Output:
{ "JobId": "e758dd56b824aa717ceab551fEXAMPLE", "JobArn": "arn:aws:comprehend:us-west-2:111122223333:document-classification-job/e758dd56b824aa717ceab551fEXAMPLE", "JobStatus": "SUBMITTED" }
Contents of
predictions.jsonl
:{"File": "SampleSMSText1.txt", "Line": "0", "Classes": [{"Name": "spam", "Score": 0.9999}, {"Name": "ham", "Score": 0.0001}]} {"File": "SampleSMStext2.txt", "Line": "0", "Classes": [{"Name": "ham", "Score": 0.9994}, {"Name": "spam", "Score": 0.0006}]} {"File": "SampleSMSText3.txt", "Line": "0", "Classes": [{"Name": "spam", "Score": 0.9999}, {"Name": "ham", "Score": 0.0001}]}
For more information, see Custom Classification in the Amazon Comprehend Developer Guide.
-
For API details, see StartDocumentClassificationJob
in AWS CLI Command Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. 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, ) logger.info( "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
-
For API details, see StartDocumentClassificationJob in AWS SDK for Python (Boto3) API Reference.
-
For a complete list of AWS SDK developer guides and code examples, see Using Amazon Comprehend with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.