Amazon Comprehend for beginners: language, mood and personal data from one command
Comprehend reads plain text and hands back structured answers: which language, how people feel, and where the personal details are. Today: when it beats an LLM, when it doesn't, and a CloudShell lab that creates nothing in your account.
Amazon Comprehend, in plain words
Amazon Comprehend is a natural language processing (NLP) service. You send it text and it answers with JSON: the dominant language, the overall sentiment (positive, negative, neutral or mixed), targeted sentiment for each thing mentioned, entities such as people, places and dates, key phrases, the part of speech of each word, and personally identifiable information (PII) such as names, emails and phone numbers. Every answer carries a confidence score between 0 and 1.
There is no model to pick, train or host for the built-in features. The real-time APIs, such as DetectSentiment, take one document and answer within the same call, and batch versions take up to 25 documents at once. For large piles of files in S3 there are asynchronous jobs, and Comprehend Custom lets you train your own classifiers and entity recognizers when the built-in labels don't fit. You pay per 100 characters analyzed, with a minimum of 300 characters per request.
One change to know about: since April 30, 2026, three Comprehend features (topic modeling, event detection and prompt safety classification) are no longer open to new customers. AWS points people to Amazon Bedrock models for the first two and to Bedrock Guardrails for prompt safety. The features in today's lab are not affected.
Reach for it when
- You need the same few answers (language, sentiment, entities, PII) for thousands of short texts such as reviews, tickets or chat messages, and you want fixed, machine-readable labels rather than free-form prose.
- You want to find personal details in text before you store it, log it or send it somewhere else, and you need the exact character positions so you can mask them.
- You want a low per-request price and predictable output without writing or testing prompts.
Skip it when
- You need open-ended work such as summaries, answers to questions or labels you can only describe in a sentence. A Bedrock foundation model with a good prompt fits better.
- Your text is in a language the feature doesn't support. For example, the developer guide lists real-time PII detection for English and Spanish only, and targeted sentiment for English only.
- You need text pulled out of scanned pages or speech first. Use Amazon Textract or Amazon Transcribe to get the text, then decide whether Comprehend is still needed.
The AI angle
Comprehend uses pre-trained, single-purpose models, so it is often the cheap first pass in front of a generative AI app: detect the language to choose a prompt, score sentiment to route angry tickets first, or find PII with DetectPiiEntities and mask it before the text reaches a Bedrock model or a Knowledge Base. If the PII check sits right at the model call, Bedrock Guardrails has its own sensitive information filters that can block or mask PII in both prompts and responses, so compare the two before building both.
Hands-on lab: Ask Comprehend about four short texts and mask the personal details
From AWS CloudShell, detect a language, score overall and per-item sentiment, find PII in a made-up message and print a masked copy. No AWS resources are created; you only make a handful of real-time API calls.
Before you start
- An AWS account that you own and are allowed to experiment in. Don't use a work account without permission.
- Sign-in as an IAM user or role that may call Comprehend's
Detect*actions (an administrator role in a personal sandbox account covers this). No access keys are needed: CloudShell uses your console sign-in. - The console set to US East (N. Virginia),
us-east-1. The commands below use that Region.
Open CloudShell and write the sample texts
Choose the CloudShell icon in the console's top bar. These lines set the Region and save two made-up product reviews to a local file, one review per line. Nothing is created in AWS. If CloudShell restarts, run this step again.
REGION=us-east-1 cat > reviews.txt <<'EOF' The headphones sound great and the battery lasts all week. The case cracked after two days and support never answered my email. EOF cat reviews.txtWhich language is this?
detect-dominant-languageneeds no language code; it works one out. The answer is a list sorted by confidence, so the first entry is the dominant language. You should seeeswith a high score.aws comprehend detect-dominant-language --region "$REGION" \ --text "Hoy hace un día precioso y el parque está lleno de gente." \ --query 'Languages[0]'Score the overall mood of each review
detect-sentimentreturns one label for the whole text plus a score for each of the four possible labels. The loop sends each line of the file as its own request (two requests in total).while IFS= read -r LINE; do echo "$LINE" aws comprehend detect-sentiment --region "$REGION" \ --language-code en --text "$LINE" \ --query '[Sentiment, SentimentScore.Positive, SentimentScore.Negative]' \ --output text done < reviews.txtScore the mood per thing mentioned
A mixed review hides detail inside one label.
detect-targeted-sentimentfinds the entities and gives each mention its own sentiment. Expect the pizza to come back positive and the delivery negative, each with a type such asCOMMERCIAL_ITEMorOTHER.aws comprehend detect-targeted-sentiment --region "$REGION" \ --language-code en \ --text "The pizza was delicious but the delivery took two hours." \ --query 'Entities[].Mentions[].[Text, Type, MentionSentiment.Sentiment]' \ --output tableFind the personal details and mask them
detect-pii-entitiesreturns each PII item's type and its start and end character positions; it does not change your text. The loop uses those positions to print each item and to build a copy with every item replaced by#characters. The message is made up and uses a reserved example domain and a fictional 555 number.TEXT="Hi, this is Jordan Rivera. Email me at jordan.rivera@example.com or call 555-0100 about my order." aws comprehend detect-pii-entities --region "$REGION" \ --language-code en --text "$TEXT" \ --query 'Entities[].[Type, BeginOffset, EndOffset]' --output text > pii.txt MASKED="$TEXT" while read -r TYPE B E; do LEN=$((E - B)) echo "$TYPE -> ${TEXT:$B:$LEN}" MASKED="${MASKED:0:$B}$(printf '%*s' "$LEN" '' | tr ' ' '#')${MASKED:$E}" done < pii.txt echo "$MASKED"
Check that it worked
Step 2 prints es with a score close to 1. Step 3 prints POSITIVE for the first review and NEGATIVE for the second. Step 4 shows one row per mention with its own sentiment. Step 5 lists at least NAME, EMAIL and PHONE and prints the sentence with those parts replaced by #. Exact scores, and sometimes the entity types, can differ slightly from run to run as the models are updated.
Clean up
The lab created no AWS resources: real-time Comprehend calls don't store anything in your account. Remove the two local files from your CloudShell home folder and confirm they are gone; the ls should print No such file or directory for both. Optionally, check that you never started an asynchronous job: the last command should print an empty list.
rm -f reviews.txt pii.txt
ls reviews.txt pii.txt
aws comprehend list-sentiment-detection-jobs --region "$REGION" \
--query 'SentimentDetectionJobPropertiesList[].JobId'Gotchas
- Every request is billed for at least 300 characters, so sending one tweet at a time costs the same as sending 300 characters. For many short texts, the batch APIs take up to 25 documents per call.
- Size limits differ by API: sentiment and targeted sentiment accept up to 5 KB of text per request, while PII, key phrases and dominant language accept up to 100 KB.
- The CLI reference lists the same 12 language codes for most commands, but the developer guide is narrower for some features: real-time PII detection for English and Spanish, targeted sentiment for English only. Check the feature page before relying on another language.
- Real-time PII detection finds and locates PII but doesn't redact it. A redacted copy comes from an asynchronous job, or from your own code using the offsets as in step 5. Scores are confidence values, not guarantees, so review results before trusting them with regulated data.
- If your account never used topic modeling, event detection or prompt safety classification in the 12 months before the April 30, 2026 change, those features are closed to you. Older tutorials that use them won't work in a new sandbox account.