|
| 1 | +#!/bin/bash |
| 2 | +# Tutorial: Detect sentiment, entities, and key phrases with Amazon Comprehend |
| 3 | +# Source: https://docs.aws.amazon.com/comprehend/latest/dg/get-started-api.html |
| 4 | + |
| 5 | +WORK_DIR=$(mktemp -d) |
| 6 | +LOG_FILE="$WORK_DIR/comprehend-$(date +%Y%m%d-%H%M%S).log" |
| 7 | +exec > >(tee -a "$LOG_FILE") 2>&1 |
| 8 | + |
| 9 | +REGION=${AWS_DEFAULT_REGION:-${AWS_REGION:-$(aws configure get region 2>/dev/null)}} |
| 10 | +if [ -z "$REGION" ]; then |
| 11 | + echo "ERROR: No AWS region configured. Set one with: export AWS_DEFAULT_REGION=us-east-1" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | +export AWS_DEFAULT_REGION="$REGION" |
| 15 | +echo "Region: $REGION" |
| 16 | + |
| 17 | +TEXT="Amazon Comprehend is a natural language processing service that uses machine learning to find insights and relationships in text. The service can identify the language of the text, extract key phrases, places, people, brands, or events, and understand how positive or negative the text is." |
| 18 | + |
| 19 | +echo "" |
| 20 | +echo "Sample text:" |
| 21 | +echo " $TEXT" |
| 22 | +echo "" |
| 23 | + |
| 24 | +# Step 1: Detect dominant language |
| 25 | +echo "Step 1: Detecting dominant language" |
| 26 | +aws comprehend detect-dominant-language --text "$TEXT" \ |
| 27 | + --query 'Languages[0].{Language:LanguageCode,Confidence:Score}' --output table |
| 28 | + |
| 29 | +# Step 2: Detect sentiment |
| 30 | +echo "" |
| 31 | +echo "Step 2: Detecting sentiment" |
| 32 | +aws comprehend detect-sentiment --text "$TEXT" --language-code en \ |
| 33 | + --query '{Sentiment:Sentiment,Positive:SentimentScore.Positive,Negative:SentimentScore.Negative,Neutral:SentimentScore.Neutral}' --output table |
| 34 | + |
| 35 | +# Step 3: Detect entities |
| 36 | +echo "" |
| 37 | +echo "Step 3: Detecting entities" |
| 38 | +aws comprehend detect-entities --text "$TEXT" --language-code en \ |
| 39 | + --query 'Entities[].{Text:Text,Type:Type,Score:Score}' --output table |
| 40 | + |
| 41 | +# Step 4: Detect key phrases |
| 42 | +echo "" |
| 43 | +echo "Step 4: Detecting key phrases" |
| 44 | +aws comprehend detect-key-phrases --text "$TEXT" --language-code en \ |
| 45 | + --query 'KeyPhrases[].{Text:Text,Score:Score}' --output table |
| 46 | + |
| 47 | +# Step 5: Detect PII entities |
| 48 | +echo "" |
| 49 | +echo "Step 5: Detecting PII entities" |
| 50 | +PII_TEXT="Please contact Jane Smith at jane.smith@example.com or call 555-0123. Her account number is 1234567890." |
| 51 | +echo " PII sample: $PII_TEXT" |
| 52 | +aws comprehend detect-pii-entities --text "$PII_TEXT" --language-code en \ |
| 53 | + --query 'Entities[].{Type:Type,Score:Score}' --output table |
| 54 | + |
| 55 | +echo "" |
| 56 | +echo "Tutorial complete. No resources were created — Comprehend is a stateless API." |
| 57 | +rm -rf "$WORK_DIR" |
0 commit comments