-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomprehend.py
executable file
·37 lines (32 loc) · 1.45 KB
/
comprehend.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import boto3
def get_sentiment(text, language_code='en'):
"""Inspects text and returns an inference of the prevailing sentiment
(positive, neutral, mixed, or negative).
Input:
text -- UTF-8 text string. Each string must contain fewer that
5,000 bytes of UTF-8 encoded characters (required | type: str);
language_code -- language of text (not required | type: str |
default: 'en').
Output:
sentiment -- sentiment: positive, neutral, mixed, or negative
(type: str).
"""
comprehend = boto3.client('comprehend')
try:
r = comprehend.detect_sentiment(Text=text, LanguageCode='en')
except Exception as exception:
return 1
sentiment = r['Sentiment'].lower()
return sentiment
# Example. Get sentiment of text below:
# "I ordered a small and expected it to fit just right but it was a little bit
# more like a medium-large. It was great quality. It's a lighter brown than
# pictured but fairly close. Would be ten times better if it was lined with
# cotton or wool on the inside."
#text = "I ordered a small and expected it to fit just right but it was a \
# little bit more like a medium-large. It was great quality. It's a \
# lighter brown than pictured but fairly close. Would be ten times \
# better if it was lined with cotton or wool on the inside."
#get_sentiment(text)