Skip to content

Commit c70affa

Browse files
committed
Add ai-ml tutorials (batch 1)
1 parent 49f07d9 commit c70affa

15 files changed

Lines changed: 1131 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Comprehend: Detect sentiment, entities, and key phrases
2+
3+
Analyze text using Amazon Comprehend's real-time APIs for language detection, sentiment analysis, entity recognition, key phrase extraction, and PII detection.
4+
5+
## Source
6+
7+
https://docs.aws.amazon.com/comprehend/latest/dg/get-started-api.html
8+
9+
## Use case
10+
11+
- ID: comprehend/getting-started
12+
- Phase: create
13+
- Complexity: beginner
14+
- Core actions: comprehend:DetectSentiment, comprehend:DetectEntities, comprehend:DetectKeyPhrases
15+
16+
## What it does
17+
18+
1. Detects the dominant language of sample text
19+
2. Analyzes sentiment (positive, negative, neutral, mixed)
20+
3. Extracts named entities (people, organizations, dates)
21+
4. Extracts key phrases
22+
5. Detects PII entities (names, emails, phone numbers)
23+
24+
## Running
25+
26+
```bash
27+
bash amazon-comprehend-gs.sh
28+
```
29+
30+
## Resources created
31+
32+
None. Comprehend is a stateless API.
33+
34+
## Estimated time
35+
36+
- Run: ~5 seconds
37+
38+
## Cost
39+
40+
Comprehend pricing is per unit (100 characters). This tutorial analyzes ~500 characters, costing less than $0.01.
41+
42+
## Related docs
43+
44+
- [Real-time analysis with Amazon Comprehend](https://docs.aws.amazon.com/comprehend/latest/dg/get-started-api.html)
45+
- [Sentiment analysis](https://docs.aws.amazon.com/comprehend/latest/dg/how-sentiment.html)
46+
- [Entity recognition](https://docs.aws.amazon.com/comprehend/latest/dg/how-entities.html)
47+
- [PII detection](https://docs.aws.amazon.com/comprehend/latest/dg/how-pii.html)
48+
49+
---
50+
51+
## Appendix: Generation details
52+
53+
| Field | Value |
54+
|-------|-------|
55+
| Generation date | 2026-04-14 |
56+
| Source script | New, 57 lines |
57+
| Script test result | EXIT 0, 5s, 5 steps, stateless API |
58+
| Issues encountered | None — stateless API, no resource management needed |
59+
| Iterations | v1 (direct to publish) |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Detect sentiment, entities, and key phrases with Amazon Comprehend
2+
3+
This tutorial shows you how to use the Amazon Comprehend real-time analysis APIs to detect the dominant language, sentiment, entities, key phrases, and PII in text.
4+
5+
## Prerequisites
6+
7+
- AWS CLI configured with credentials and a default region
8+
- Permissions to call Amazon Comprehend APIs
9+
10+
## Step 1: Detect the dominant language
11+
12+
```bash
13+
aws comprehend detect-dominant-language --text "Your text here" \
14+
--query 'Languages[0].{Language:LanguageCode,Confidence:Score}' --output table
15+
```
16+
17+
## Step 2: Detect sentiment
18+
19+
```bash
20+
aws comprehend detect-sentiment --text "Your text here" --language-code en \
21+
--query '{Sentiment:Sentiment,Positive:SentimentScore.Positive,Negative:SentimentScore.Negative}' --output table
22+
```
23+
24+
## Step 3: Detect entities
25+
26+
Identifies people, places, organizations, dates, and other entity types.
27+
28+
```bash
29+
aws comprehend detect-entities --text "Your text here" --language-code en \
30+
--query 'Entities[].{Text:Text,Type:Type,Score:Score}' --output table
31+
```
32+
33+
## Step 4: Detect key phrases
34+
35+
```bash
36+
aws comprehend detect-key-phrases --text "Your text here" --language-code en \
37+
--query 'KeyPhrases[].{Text:Text,Score:Score}' --output table
38+
```
39+
40+
## Step 5: Detect PII entities
41+
42+
Identifies personally identifiable information such as names, email addresses, phone numbers, and account numbers.
43+
44+
```bash
45+
aws comprehend detect-pii-entities --text "Contact Jane at jane@example.com" --language-code en \
46+
--query 'Entities[].{Type:Type,Score:Score}' --output table
47+
```
48+
49+
## Cleanup
50+
51+
No cleanup needed. Comprehend is a stateless API — no resources are created.
52+
53+
The script automates all steps:
54+
55+
```bash
56+
bash amazon-comprehend-gs.sh
57+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Translate: Translate text between languages
2+
3+
Translate text between languages using Amazon Translate, with auto-detection of the source language.
4+
5+
## Source
6+
7+
https://docs.aws.amazon.com/translate/latest/dg/get-started.html
8+
9+
## Use case
10+
11+
- ID: translate/getting-started
12+
- Phase: create
13+
- Complexity: beginner
14+
- Core actions: translate:TranslateText, translate:ListLanguages
15+
16+
## What it does
17+
18+
1. Translates English text to Spanish
19+
2. Translates English text to French
20+
3. Translates English text to Japanese
21+
4. Auto-detects source language (German → English)
22+
5. Lists supported languages
23+
24+
## Running
25+
26+
```bash
27+
bash amazon-translate-gs.sh
28+
```
29+
30+
## Resources created
31+
32+
None. Translate is a stateless API.
33+
34+
## Estimated time
35+
36+
- Run: ~5 seconds
37+
38+
## Cost
39+
40+
Translate pricing is per character. This tutorial translates ~600 characters, costing less than $0.01.
41+
42+
## Related docs
43+
44+
- [Getting started with Amazon Translate](https://docs.aws.amazon.com/translate/latest/dg/get-started.html)
45+
- [Translating text using the API](https://docs.aws.amazon.com/translate/latest/dg/get-started-api.html)
46+
- [Supported languages](https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html)
47+
- [Automatic source language detection](https://docs.aws.amazon.com/translate/latest/dg/auto-detect.html)
48+
49+
---
50+
51+
## Appendix: Generation details
52+
53+
| Field | Value |
54+
|-------|-------|
55+
| Generation date | 2026-04-14 |
56+
| Source script | New, 60 lines |
57+
| Script test result | EXIT 0, 5s, 5 steps, stateless API |
58+
| Issues encountered | None — stateless API, no resource management needed |
59+
| Iterations | v1 (direct to publish) |
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Translate text between languages with Amazon Translate
2+
3+
This tutorial shows you how to use Amazon Translate to translate text between languages, auto-detect the source language, and list supported languages.
4+
5+
## Prerequisites
6+
7+
- AWS CLI configured with credentials and a default region
8+
- Permissions to call Amazon Translate APIs
9+
10+
## Step 1: Translate English to Spanish
11+
12+
```bash
13+
aws translate translate-text \
14+
--text "Your text here" \
15+
--source-language-code en --target-language-code es \
16+
--query 'TranslatedText' --output text
17+
```
18+
19+
## Step 2: Translate English to French
20+
21+
```bash
22+
aws translate translate-text \
23+
--text "Your text here" \
24+
--source-language-code en --target-language-code fr \
25+
--query 'TranslatedText' --output text
26+
```
27+
28+
## Step 3: Translate English to Japanese
29+
30+
```bash
31+
aws translate translate-text \
32+
--text "Your text here" \
33+
--source-language-code en --target-language-code ja \
34+
--query 'TranslatedText' --output text
35+
```
36+
37+
## Step 4: Auto-detect source language
38+
39+
Use `auto` as the source language code to let Translate detect the language:
40+
41+
```bash
42+
aws translate translate-text \
43+
--text "Amazon Translate ist ein neuronaler maschineller Übersetzungsdienst." \
44+
--source-language-code auto --target-language-code en \
45+
--query '{Translation:TranslatedText,DetectedLanguage:SourceLanguageCode}' --output table
46+
```
47+
48+
## Step 5: List supported languages
49+
50+
```bash
51+
aws translate list-languages \
52+
--query 'Languages[:10].{Name:LanguageName,Code:LanguageCode}' --output table
53+
```
54+
55+
## Cleanup
56+
57+
No cleanup needed. Translate is a stateless API — no resources are created.
58+
59+
The script automates all steps:
60+
61+
```bash
62+
bash amazon-translate-gs.sh
63+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
# Tutorial: Translate text between languages with Amazon Translate
3+
# Source: https://docs.aws.amazon.com/translate/latest/dg/get-started.html
4+
5+
WORK_DIR=$(mktemp -d)
6+
LOG_FILE="$WORK_DIR/translate-$(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 Translate is a neural machine translation service that delivers fast, high-quality, affordable, and customizable language translation."
18+
19+
echo ""
20+
echo "Source text (English):"
21+
echo " $TEXT"
22+
echo ""
23+
24+
# Step 1: Translate English to Spanish
25+
echo "Step 1: English → Spanish"
26+
aws translate translate-text --text "$TEXT" \
27+
--source-language-code en --target-language-code es \
28+
--query 'TranslatedText' --output text
29+
echo ""
30+
31+
# Step 2: Translate English to French
32+
echo "Step 2: English → French"
33+
aws translate translate-text --text "$TEXT" \
34+
--source-language-code en --target-language-code fr \
35+
--query 'TranslatedText' --output text
36+
echo ""
37+
38+
# Step 3: Translate English to Japanese
39+
echo "Step 3: English → Japanese"
40+
aws translate translate-text --text "$TEXT" \
41+
--source-language-code en --target-language-code ja \
42+
--query 'TranslatedText' --output text
43+
echo ""
44+
45+
# Step 4: Auto-detect source language
46+
echo "Step 4: Auto-detect source language (German input)"
47+
GERMAN="Amazon Translate ist ein neuronaler maschineller Übersetzungsdienst."
48+
echo " Input: $GERMAN"
49+
aws translate translate-text --text "$GERMAN" \
50+
--source-language-code auto --target-language-code en \
51+
--query '{Translation:TranslatedText,DetectedLanguage:SourceLanguageCode}' --output table
52+
echo ""
53+
54+
# Step 5: List supported languages
55+
echo "Step 5: Listing supported languages (first 10)"
56+
aws translate list-languages --query 'Languages[:10].{Name:LanguageName,Code:LanguageCode}' --output table
57+
58+
echo ""
59+
echo "Tutorial complete. No resources were created — Translate is a stateless API."
60+
rm -rf "$WORK_DIR"

0 commit comments

Comments
 (0)