Implementing Natural Language Processing with Amazon Comprehend - Sentiment Analysis and Entity Extraction

Learn about sentiment analysis, entity extraction, and building custom classification models with Comprehend.

Overview of Comprehend

Comprehend is a service that provides natural language processing (NLP) APIs, with supported languages that vary by feature. Given text input, it returns results for sentiment analysis, entity extraction, key phrase extraction, language detection, and syntax analysis. You can incorporate NLP features into your application without ML expertise. Custom classification models and custom entity recognition also support industry-specific text classification. Internally, pre-trained models using transfer learning enable building custom models even with small amounts of labeled data.

Sentiment Analysis and Custom Models

The sentiment analysis API classifies text sentiment into four categories - Positive, Negative, Neutral, and Mixed - and returns confidence scores for each. It can be used for e-commerce review analysis and call center transcript analysis. Custom classification builds models that classify text into industry-specific categories using labeled training data. It supports both multi-class classification (one label per document) and multi-label classification (multiple labels per document), accepting training data in CSV or augmented manifest format. For multi-class classification in CSV format, training requires a minimum of 50 documents per class. PII detection automatically identifies and masks personal information in text.

PII Detection and Real-Time Analysis

Comprehend's PII detection API automatically identifies personal information in text (names, addresses, phone numbers, email addresses, credit card numbers, social security numbers) and returns location information and confidence scores (supported languages are English and Spanish). The ContainsPiiEntities API determines whether PII is present, while the DetectPiiEntities API identifies specific locations. Creating a real-time analysis endpoint keeps custom classification or custom entity recognition models running continuously, providing inference results via API calls. Asynchronous batch jobs process large volumes of documents on S3 and output results to S3. Comprehend Medical specializes in medical text, extracting drug names, disease names, and procedure names.

Design Best Practices and Pitfalls

When integrating Comprehend into production, several design considerations are important. First, synchronous APIs are subject to dynamic throttling (the number of requests processed increases gradually as processing bandwidth allows), so for high traffic, switching to batch processing or requesting quota increases through Service Quotas are options. Second, synchronous sentiment and syntax analysis accept up to 5 KB per document, so long documents are split into chunks. Splitting at sentence boundaries avoids analyzing fragments cut in the middle of a sentence. For custom model operations, Flywheel is available as a mechanism for retraining. Flywheel automatically monitors model performance and triggers retraining with new data when accuracy drops. Additionally, real-time endpoints are billed from the time they start until they are deleted, so when request frequency is low, starting asynchronous jobs from Lambda is also an option.

Comparison with Other Services

Besides Comprehend, NLP implementation options include building custom models with SageMaker and using large language models (LLMs) via Bedrock. As a pre-built NLP API that is easy to start with, Comprehend is suited to standard tasks like sentiment analysis and entity extraction. SageMaker offers high flexibility for fine-tuning Hugging Face Transformers models but requires managing training infrastructure and model hosting. Bedrock's LLMs enable zero-shot classification with prompts alone. As a decision guide: Comprehend for standard NLP tasks requiring high throughput, SageMaker when custom loss functions or architectures are needed, and Bedrock for small-volume flexible classification tasks. Additionally, Comprehend can be combined with AWS services (S3, Lambda, Kinesis), and access can be controlled in detail with IAM.

Comprehend Pricing

Comprehend pricing is based on the number of characters per API call. Sentiment analysis, entity extraction, and key phrase extraction count 100 characters as 1 unit, with a minimum of 3 units (300 characters) per request. Custom model training is charged at an hourly rate (billed by the second), and real-time endpoints are charged by the number of inference units (each processing 100 characters per second) and their uptime. Batch (asynchronous) processing is charged by the number of characters, while the charge for real-time endpoints depends on how long they run. Deleting endpoints during low-traffic periods and switching to asynchronous jobs means no endpoint uptime charges for that period. Comprehend Flywheel lets you organize the flow of continuous model improvement.

Summary

Comprehend is a service that provides NLP features such as sentiment analysis, entity extraction, and PII detection via API. Custom classification models handle industry-specific text classification, and batch processing and real-time endpoints handle diverse analysis workloads. Comprehend Medical also supports extracting drug names and disease names from medical text.

References (Official AWS Resources)

The primary sources for this page are the official AWS website and documentation. Check the official pages below for the latest specifications and pricing.

If this page and the official documentation disagree, treat the official documentation as authoritative.

Share