# Amazon Comprehend code examples for the SDK for Python ## Overview Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Comprehend. *Amazon Comprehend uses natural language processing (NLP) to extract insights about the content of documents without the need of any special preprocessing.* ## ⚠ Important * Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). * Running the tests might result in charges to your AWS account. * We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). * This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). ## Code examples ### Prerequisites For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. Install the packages required by these examples by running the following in a virtual environment: ``` python -m pip install -r requirements.txt ``` ### Single actions Code excerpts that show you how to call individual service functions. * [Create a document classifier](comprehend_classifier.py#L41) (`CreateDocumentClassifier`) * [Delete a document classifier](comprehend_classifier.py#L119) (`DeleteDocumentClassifier`) * [Describe a document classification job](comprehend_classifier.py#L179) (`DescribeDocumentClassificationJob`) * [Describe a document classifier](comprehend_classifier.py#L79) (`DescribeDocumentClassifier`) * [Describe a topic modeling job](comprehend_topic_modeler.py#L75) (`DescribeTopicsDetectionJob`) * [Detect entities in a document](comprehend_detect.py#L51) (`DetectEntities`) * [Detect key phrases in a document](comprehend_detect.py#L73) (`DetectKeyPhrases`) * [Detect personally identifiable information in a document](comprehend_detect.py#L95) (`DetectPiiEntities`) * [Detect syntactical elements of a document](comprehend_detect.py#L138) (`DetectSyntax`) * [Detect the dominant language in a document](comprehend_detect.py#L32) (`DetectDominantLanguage`) * [Detect the sentiment of a document](comprehend_detect.py#L117) (`DetectSentiment`) * [List document classification jobs](comprehend_classifier.py#L199) (`ListDocumentClassificationJobs`) * [List document classifiers](comprehend_classifier.py#L101) (`ListDocumentClassifiers`) * [List topic modeling jobs](comprehend_topic_modeler.py#L95) (`ListTopicsDetectionJobs`) * [Start a document classification job](comprehend_classifier.py#L134) (`StartDocumentClassificationJob`) * [Start a topic modeling job](comprehend_topic_modeler.py#L34) (`StartTopicsDetectionJob`) ### Scenarios Code examples that show you how to accomplish a specific task by calling multiple functions within the same service. * [Detect document elements](comprehend_detect.py) * [Run a topic modeling job on sample data](comprehend_topic_modeler.py) * [Train a custom classifier and classify documents](comprehend_classifier.py) ### Cross-service examples Sample applications that work across multiple AWS services. * [Detect entities in text extracted from an image](../../cross_service/textract_comprehend_notebook) ## Run the examples ### Instructions #### Detect document elements This example shows you how to do the following: * Detect languages, entities, and key phrases in a document. * Detect personally identifiable information (PII) in a document. * Detect the sentiment of a document. * Detect syntax elements in a document. Start the example by running the following at a command prompt: ``` python comprehend_detect.py ``` #### Run a topic modeling job on sample data This example shows you how to do the following: * Run an Amazon Comprehend topic modeling job on sample data. * Get information about the job. * Extract job output data from Amazon S3. Start the example by running the following at a command prompt: ``` python comprehend_topic_modeler.py ``` #### Train a custom classifier and classify documents This example shows you how to do the following: * Create an Amazon Comprehend multi-label classifier. * Train the classifier on sample data. * Run a classification job on a second set of data. * Extract the job output data from Amazon S3. Start the example by running the following at a command prompt: ``` python comprehend_classifier.py ``` ### Tests ⚠ Running tests might result in charges to your AWS account. To find instructions for running these tests, see the [README](../../README.md#Tests) in the `python` folder. ## Additional resources * [Amazon Comprehend Developer Guide](https://docs.aws.amazon.com/comprehend/latest/dg/what-is.html) * [Amazon Comprehend API Reference](https://docs.aws.amazon.com/comprehend/latest/APIReference/welcome.html) * [SDK for Python Amazon Comprehend reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/comprehend.html) --- Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0