-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sentiments.py
26 lines (20 loc) · 795 Bytes
/
sentiments.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
# Imports the Google Cloud client library
from google.cloud import language_v1
import os
import json
with open('config/config.json') as json_file:
config = json.load(json_file)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=config["G_CLOUD_SERVICE_KEYFILE"]
client = language_v1.LanguageServiceClient()
def google_sentiment_analysis(text: str):
try:
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
sentiment = client.analyze_sentiment(request={'document': document}).document_sentiment
sentiment_score = {
"score": sentiment.score,
"magnitude": sentiment.magnitude
}
except Exception:
return None
output = sentiment_score if sentiment_score else None
return output