forked from aws-samples/aws-nlp-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictfeedbacksentiment.py
38 lines (38 loc) · 1.6 KB
/
predictfeedbacksentiment.py
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
import os
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.client('dynamodb')
comprehend = boto3.client('comprehend')
def lambda_handler(event, context):
print('Received event: ' + json.dumps(event, indent=2))
ids = event['ID'].split(',')
table_name=os.environ['table_name']
responses = []
for id in ids:
try:
response = dynamodb.scan(
ExpressionAttributeNames={'#ID': 'ID'},
ExpressionAttributeValues={':id' : {'S': id}},
FilterExpression='#ID = :id',
TableName=table_name
)
items = response['Items']
postedtime = items[0]['PostedTime']['S']
if 'Feedback' in items[0]:
feedback = items[0]['Feedback']['S']
response = comprehend.detect_sentiment(Text=feedback, LanguageCode='en')
sentiment = response['Sentiment']
print(sentiment)
response = dynamodb.update_item(
ExpressionAttributeNames={'#ST': 'Sentiment'},
ExpressionAttributeValues={':st' : {'S': sentiment}},
Key={'ID': {'S': id}, 'PostedTime': {'S': postedtime}},
ReturnValues='ALL_NEW',
TableName=table_name,
UpdateExpression='SET #ST = :st'
)
responses.append('{} - {}'.format(response['Attributes']['ID']['S'], response['Attributes']['Sentiment']['S']))
except Exception as e:
print('Actual error is: {0}'.format(e))
return responses