Skip to main content

Amazon Comprehend

Amazon Comprehend คือบริการ Natural Language Processing (NLP) ที่ใช้ Machine Learning ในการวิเคราะห์และทำความเข้าใจข้อความโดยอัตโนมัติ บริการนี้รองรับภาษาไทย ภาษาอังกฤษ และอีกกว่า 100 ภาษาทั่วโลก โดยไม่จำเป็นต้องมีความรู้ด้าน Machine Learning แต่อย่างใด เพียงแค่ส่งข้อความผ่าน API ก็สามารถรับ insight ที่มีคุณค่าได้ทันที

Comprehend ถูกออกแบบมาให้ใช้งานได้ทั้งแบบ real-time สำหรับข้อความสั้น และแบบ async batch jobs สำหรับเอกสารขนาดใหญ่จำนวนมาก นอกจากนี้ยังมี Comprehend Custom ที่ช่วยให้สามารถฝึกโมเดลด้วยข้อมูลของตัวเองได้ ทำให้ระบบเข้าใจภาษาและคำศัพท์เฉพาะทางของแต่ละอุตสาหกรรม

AWS Docs: https://docs.aws.amazon.com/comprehend/latest/dg/what-is.html

สถาปัตยกรรม


ฟีเจอร์หลัก

Sentiment Analysis

วิเคราะห์อารมณ์และความรู้สึกในข้อความแบ่งออกเป็น 4 ระดับ ได้แก่ Positive, Negative, Neutral และ Mixed พร้อม confidence score สำหรับแต่ละ sentiment เหมาะสำหรับวิเคราะห์รีวิวสินค้า ความคิดเห็นในโซเชียลมีเดีย หรือ feedback จากลูกค้า

Targeted Sentiment

วิเคราะห์ sentiment ในระดับ entity หรือสิ่งของเฉพาะเจาะจงภายในประโยค ตัวอย่างเช่น ในประโยค "แบตเตอรี่แย่มากแต่หน้าจอสวยงาม" ระบบจะระบุได้ว่า "แบตเตอรี่" มี sentiment เป็น Negative ในขณะที่ "หน้าจอ" เป็น Positive

Entity Recognition

ตรวจจับและจำแนก entity ในข้อความออกเป็นหมวดหมู่ต่างๆ เช่น PERSON (บุคคล), LOCATION (สถานที่), ORGANIZATION (องค์กร), DATE (วันที่), QUANTITY (ปริมาณ), COMMERCIAL_ITEM (สินค้าเชิงพาณิชย์), EVENT (เหตุการณ์) และ OTHER ช่วยให้สกัดข้อมูลสำคัญจากข้อความได้อัตโนมัติ

Key Phrase Extraction

สกัดวลีสำคัญ (Key Phrases) จากข้อความ ช่วยระบุหัวข้อหลักและประเด็นสำคัญในเอกสารโดยไม่ต้องอ่านทั้งหมด เหมาะสำหรับการสร้าง tag อัตโนมัติหรือสรุปเนื้อหาเบื้องต้น

Language Detection

ตรวจจับภาษาของข้อความโดยอัตโนมัติ รองรับกว่า 100 ภาษา พร้อม confidence score สำหรับแต่ละภาษาที่ตรวจพบ ช่วยให้ระบบสามารถ route ข้อความไปยัง pipeline ที่เหมาะสมได้อัตโนมัติ

PII Detection and Redaction

ตรวจจับข้อมูลส่วนบุคคล (Personally Identifiable Information) เช่น ชื่อ ที่อยู่ เบอร์โทรศัพท์ อีเมล หมายเลขบัตรเครดิต และ SSN จากข้อความ และสามารถ redact (ซ่อน) ข้อมูลเหล่านี้โดยอัตโนมัติ เหมาะสำหรับการปฏิบัติตาม PDPA และ GDPR

Topic Modeling

วิเคราะห์เอกสารจำนวนมากเพื่อค้นหาหัวข้อหลักที่เกิดขึ้นซ้ำๆ ใช้ Latent Dirichlet Allocation (LDA) เป็นเทคนิคพื้นฐาน เหมาะสำหรับการจัดหมวดหมู่บทความหรือเอกสารโดยอัตโนมัติ

Custom Classifiers

ฝึกโมเดลจำแนกประเภทข้อความด้วยข้อมูล label ของตัวเอง รองรับทั้ง multi-class (แต่ละข้อความมีได้หนึ่ง class) และ multi-label (หนึ่งข้อความมีได้หลาย class) เช่น จำแนก support ticket เป็นหมวดหมู่ต่างๆ ของบริษัท

Custom Entity Recognizers

ฝึกโมเดลตรวจจับ entity เฉพาะทางที่ไม่มีใน built-in เช่น ชื่อผลิตภัณฑ์ รหัสสินค้า หรือตำแหน่งงานเฉพาะองค์กร โดยใช้ข้อมูล annotation ของตัวเอง

Async Batch Processing

ประมวลผลเอกสารจำนวนมากใน S3 แบบ asynchronous เหมาะสำหรับงานขนาดใหญ่ที่ไม่ต้องการผลลัพธ์ทันที โดยระบบจะส่ง notification ผ่าน SNS เมื่องานเสร็จสิ้น

Comprehend Medical (บริการเสริม)

บริการแยกต่างหากสำหรับวิเคราะห์ข้อความทางการแพทย์โดยเฉพาะ ดูรายละเอียดในหน้า Amazon Comprehend Medical


การติดตั้งและการตั้งค่า

1. เปิดใช้งานผ่าน AWS Console

  1. เข้า AWS Console และค้นหา "Amazon Comprehend"
  2. คลิก "Launch Amazon Comprehend"
  3. บริการพร้อมใช้งานทันที ไม่ต้อง provision resources

2. ติดตั้ง boto3

pip install boto3

3. IAM Permissions ที่จำเป็น

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"comprehend:DetectSentiment",
"comprehend:DetectEntities",
"comprehend:DetectKeyPhrases",
"comprehend:DetectDominantLanguage",
"comprehend:DetectPiiEntities",
"comprehend:StartTopicsDetectionJob",
"comprehend:StartDocumentClassificationJob",
"comprehend:CreateDocumentClassifier",
"comprehend:CreateEntityRecognizer"
],
"Resource": "*"
}
]
}

4. ตัวอย่างการตั้งค่า boto3

import boto3

comprehend = boto3.client(
service_name='comprehend',
region_name='ap-southeast-1' # Singapore region
)

วิธีใช้งาน

Sentiment Analysis

import boto3

comprehend = boto3.client('comprehend', region_name='ap-southeast-1')

# วิเคราะห์ sentiment ข้อความภาษาไทย
text = "สินค้านี้คุณภาพดีมาก ราคาเหมาะสม แนะนำให้ซื้อเลย!"

response = comprehend.detect_sentiment(
Text=text,
LanguageCode='th'
)

print(f"Sentiment: {response['Sentiment']}")
print(f"Scores: {response['SentimentScore']}")
# Sentiment: POSITIVE
# Scores: {'Positive': 0.98, 'Negative': 0.01, 'Neutral': 0.01, 'Mixed': 0.0}

Entity Recognition

text = "คุณสมชาย ทำงานที่บริษัท AWS ในกรุงเทพฯ เมื่อวันที่ 15 มกราคม 2567"

response = comprehend.detect_entities(
Text=text,
LanguageCode='th'
)

for entity in response['Entities']:
print(f"Entity: {entity['Text']} | Type: {entity['Type']} | Score: {entity['Score']:.2f}")
# Entity: สมชาย | Type: PERSON | Score: 0.99
# Entity: AWS | Type: ORGANIZATION | Score: 0.97
# Entity: กรุงเทพฯ | Type: LOCATION | Score: 0.95
# Entity: 15 มกราคม 2567 | Type: DATE | Score: 0.98

PII Detection

text = "ติดต่อได้ที่ [email protected] หรือโทร 081-234-5678"

response = comprehend.detect_pii_entities(
Text=text,
LanguageCode='en'
)

for entity in response['Entities']:
print(f"PII Type: {entity['Type']} | Position: {entity['BeginOffset']}-{entity['EndOffset']}")

Batch Processing (Async)

import boto3

comprehend = boto3.client('comprehend', region_name='ap-southeast-1')

# เริ่ม async sentiment analysis job
response = comprehend.start_sentiment_detection_job(
InputDataConfig={
'S3Uri': 's3://my-bucket/input-docs/',
'InputFormat': 'ONE_DOC_PER_LINE'
},
OutputDataConfig={
'S3Uri': 's3://my-bucket/output-results/'
},
DataAccessRoleArn='arn:aws:iam::123456789:role/ComprehendRole',
JobName='sentiment-batch-job',
LanguageCode='th'
)

job_id = response['JobId']
print(f"Job started: {job_id}")

# ตรวจสอบสถานะ
status = comprehend.describe_sentiment_detection_job(JobId=job_id)
print(f"Status: {status['SentimentDetectionJobProperties']['JobStatus']}")

Custom Classifier

# สร้าง custom classifier
response = comprehend.create_document_classifier(
DocumentClassifierName='support-ticket-classifier',
DataAccessRoleArn='arn:aws:iam::123456789:role/ComprehendRole',
InputDataConfig={
'S3Uri': 's3://my-bucket/training-data/tickets.csv'
},
OutputDataConfig={
'S3Uri': 's3://my-bucket/classifier-output/'
},
LanguageCode='th',
Mode='MULTI_CLASS'
)

print(f"Classifier ARN: {response['DocumentClassifierArn']}")

ราคา (ประมาณการในบาท)

อัตราแลกเปลี่ยน: 1 USD = 35 บาท

ฟีเจอร์ราคา (USD)ราคา (บาท)หมายเหตุ
Sentiment Analysis$0.0001/unit0.0035 บาท/unit100 ตัวอักษร = 1 unit
Entity Recognition$0.0001/unit0.0035 บาท/unit100 ตัวอักษร = 1 unit
Key Phrase Extraction$0.0001/unit0.0035 บาท/unit100 ตัวอักษร = 1 unit
Language Detection$0.0001/unit0.0035 บาท/unit100 ตัวอักษร = 1 unit
PII Detection$0.0001/unit0.0035 บาท/unit100 ตัวอักษร = 1 unit
Custom Classifier Training$3.00/hour105 บาท/ชม.ราคา training
Custom Classifier Inference$0.0005/unit0.0175 บาท/unitreal-time
Topic Modeling$1.00/job35 บาท/jobสูงสุด 1,000 docs
Topic Modeling$2.00/job70 บาท/job1,000-10,000 docs

Free Tier: 50,000 units/เดือน สำหรับทุก API ใน 12 เดือนแรก

ตัวอย่างการคำนวณ:

  • วิเคราะห์ reviews 100,000 รายการ เฉลี่ย 200 ตัวอักษร/รายการ = 200,000 units
  • ค่าใช้จ่าย = 200,000 × $0.0001 = $20 (~700 บาท)

เหมาะสำหรับ

  • แบรนด์และ E-Commerce ที่ต้องการวิเคราะห์รีวิวและ feedback ของลูกค้าจากหลายช่องทาง
  • Call Center และ Customer Support ที่ต้องการจัดหมวดหมู่ ticket และวิเคราะห์ความรู้สึกลูกค้า
  • สำนักข่าวและสื่อ ที่ต้องการ tag บทความและจัดหมวดหมู่เนื้อหาโดยอัตโนมัติ
  • สถาบันการเงินและกฎหมาย ที่ต้องการสกัด entity สำคัญจากเอกสารสัญญาหรือรายงาน
  • หน่วยงานที่ต้องการปฏิบัติตาม PDPA/GDPR สำหรับการตรวจจับและซ่อนข้อมูลส่วนบุคคล
  • ทีมวิจัยและวิทยาศาสตร์ข้อมูล ที่ต้องการ explore ข้อมูลข้อความจำนวนมาก

ใช้ร่วมกับ AWS Services

AWS Serviceการผสานรวม
Amazon S3เก็บเอกสาร input/output สำหรับ batch processing
AWS Lambdaทริกเกอร์ Comprehend เมื่อมีข้อมูลใหม่เข้าระบบ
Amazon Kinesisวิเคราะห์ข้อความ streaming แบบ real-time
Amazon DynamoDBเก็บผลลัพธ์การวิเคราะห์ sentiment และ entity
Amazon QuickSightสร้าง dashboard แสดงผลการวิเคราะห์ sentiment
AWS Step Functionsสร้าง workflow NLP pipeline อัตโนมัติ
Amazon SNS/SQSรับ notification เมื่อ batch job เสร็จสิ้น
Amazon Translateแปลภาษาก่อนวิเคราะห์ด้วย Comprehend

Use Case ตัวอย่าง

1. ระบบวิเคราะห์รีวิวสินค้า E-Commerce

แบรนด์ผลิตภัณฑ์ขนาดใหญ่ใช้ Comprehend วิเคราะห์รีวิวสินค้าจากลูกค้ากว่า 100,000 รายการต่อเดือนบน marketplace ต่างๆ ระบบใช้ Sentiment Analysis จำแนก feedback เป็น positive, negative, neutral โดยอัตโนมัติ และใช้ Entity Recognition ระบุว่าลูกค้าพูดถึงสิ่งใดบ้าง (บรรจุภัณฑ์, คุณภาพสินค้า, การจัดส่ง) จากนั้น Targeted Sentiment ช่วยระบุว่าในรีวิวเดียวกัน ลูกค้าพอใจหรือไม่พอใจเรื่องใด ทีม product สามารถเห็น insight แบบ real-time ผ่าน QuickSight dashboard และปรับปรุงสินค้าได้เร็วขึ้น 3 เท่า

2. ระบบ Compliance และ Data Privacy

บริษัทประกันภัยใช้ Comprehend PII Detection สแกนเอกสาร claim ที่รับเข้ามากว่า 50,000 ฉบับต่อเดือน เพื่อตรวจจับและ redact ข้อมูลส่วนบุคคลก่อนที่เอกสารจะถูกส่งต่อไปยังทีมวิเคราะห์ข้อมูล ช่วยให้บริษัทปฏิบัติตาม PDPA ได้อย่างครบถ้วน และลดความเสี่ยงจากการรั่วไหลของข้อมูลส่วนบุคคล ระบบ Lambda trigger ทำงานโดยอัตโนมัติทุกครั้งที่มีเอกสารใหม่ถูกอัปโหลดใน S3

3. ระบบจัดหมวดหมู่ Support Ticket อัตโนมัติ

บริษัท Software as a Service ใช้ Custom Classifier ที่ฝึกด้วยข้อมูล ticket จากระบบ CRM กว่า 50,000 รายการ โมเดลสามารถจำแนก ticket เป็นหมวดหมู่ได้แก่ Billing, Technical Issue, Feature Request, Account Access และ General Inquiry ด้วยความแม่นยำ 94% ระบบจะ route ticket ไปยัง team ที่รับผิดชอบโดยอัตโนมัติผ่าน SQS ลดเวลาตอบสนองลูกค้าจากเฉลี่ย 4 ชั่วโมงเหลือเพียง 45 นาที