-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathlambda_comprehend_batch_process.py
More file actions
43 lines (35 loc) · 1017 Bytes
/
lambda_comprehend_batch_process.py
File metadata and controls
43 lines (35 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import boto3
def datachunk(para):
text_list = []
while para:
text_list.append(str(para[:4700]))
para = para[4700:]
return text_list[:25]
def lambda_handler(event, context):
s3 = boto3.client("s3")
bucket = "bucket-name"
key = "filename.txt"
file = s3.get_object(Bucket=bucket, Key=key)
paragraph = str(file["Body"].read().decode("utf-8"))
comprehend = boto3.client("comprehend")
sentiment = comprehend.batch_detect_sentiment(
TextList=datachunk(paragraph), LanguageCode="en"
)
print(sentiment)
entities = comprehend.batch_detect_entities(
TextList=datachunk(paragraph), LanguageCode="en"
)
print(entities)
keyphrase = comprehend.batch_detect_key_phrases(
TextList=datachunk(paragraph), LanguageCode="en"
)
print(keyphrase)
return "Thanks"