forked from morejust/parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleApiSentiment.py
56 lines (45 loc) · 1.73 KB
/
googleApiSentiment.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
import os
import six
from google.cloud import language
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./google_api_cred.json"
def entity_sentiment_text(text):
"""Detects entity sentiment in the provided text."""
client = language.LanguageServiceClient()
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
document = language.types.Document(
content=text.encode('utf-8'),
type=language.enums.Document.Type.PLAIN_TEXT,
language='en'
)
# Detect and send native Python encoding to receive correct word offsets.
encoding = language.enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = language.enums.EncodingType.UTF16
result = client.analyze_entity_sentiment(document, encoding)
return result
def extract_entity_features(entity):
features = {}
features["name"] = entity.name
features["salience"] = entity.salience.real
features["sentiment"] = entity.sentiment.score.real
features["magnitude"] = entity.sentiment.magnitude.real
features["type"] = "sentiment"
features['mentions'] = []
for mention in entity.mentions:
f = {}
f["offset"] = mention.text.begin_offset
f["content"] = mention.text.content
f["magnitude"] = mention.sentiment.magnitude.real
f["sentiment"] = mention.sentiment.score.real
f["type"] = mention.type
features['mentions'].append(f)
return features
def get_sentiments(text):
google_api_result = entity_sentiment_text(text)
all_sentiments_features = []
for entity in google_api_result.entities:
features = extract_entity_features(entity)
all_sentiments_features.append(features)
return all_sentiments_features