-
Notifications
You must be signed in to change notification settings - Fork 2
/
google_cloud.py
65 lines (48 loc) · 1.63 KB
/
google_cloud.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
57
58
59
60
61
import argparse
import json
import google.cloud.language
import sys
import os
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
def getSenti(cont):
global sentiScore
global sentiSize
# Create a Language client.
language_client = google.cloud.language.LanguageServiceClient()
for tweet in cont["direct_tweets"]:
# TODO (Developer): Replace this with the text you want to analyze.
text = u''+tweet["text"]
document = google.cloud.language.types.Document(
content=text,
type=google.cloud.language.enums.Document.Type.PLAIN_TEXT)
# Use Language to detect the sentiment of the text.
response = language_client.analyze_sentiment(document=document)
sentiment = response.document_sentiment
if( sentiment.score <= -0.21 or 0.21 <= sentiment.score ):
sentiScore += sentiment.score
sentiSize += 1.0
# print(u'Text: {}'.format(text))
# print(u'Sentiment: Score: {}, Magnitude: {}'.format(
# sentiment.score, sentiment.magnitude))
def getMood():
global sentiScore
sentiScore = sentiScore/sentiSize
senti = None
if( sentiScore <= -0.21 ):
senti = "sad"
elif( -0.2 < sentiScore and sentiScore < 0.2 ):
senti = "neutral"
elif( 0.21 <= sentiScore ):
senti = "happy"
else:
senti = "null"
return senti
sentiScore = 0
sentiSize = 0.0
d = open(sys.argv[1], "r")
data = json.loads(d.read())
getSenti(data)
with open(str(os.getpid())+'.txt', 'w') as f:
json.dump(getMood(), f, indent=2)