-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentimentapi.py
61 lines (53 loc) · 1.7 KB
/
sentimentapi.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
# This module uses Google Cloud Natural Language to evaluate the sentiment of long-form text.
#
# API requests will be attempted several times before backing off when encountering an error.
import time
import gfunctions
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
class sentiment():
def __init__(self,strings):
self.apiProgress = 0
self.apiStatus = ''
self.apiRequests = []
self.apiNextRequest = -1
self.reportHeaders = []
self.reportData = {}
self.APIdisconnect = False
self.totalErrors = 0
g = gfunctions.gInterface()
self.client = language.LanguageServiceClient(credentials = g.credentials)
for thisString in strings:
if thisString not in self.apiRequests:
self.apiRequests.append(thisString)
def processNext(self):
nextID = self.apiNextRequest + 1
totalRequests = len(self.apiRequests)
if nextID < totalRequests and not self.APIdisconnect:
self.apiProgress = (nextID / totalRequests) * 100
self.apiStatus = self.apiRequests[nextID]
self.checkSentiment(self.apiRequests[nextID])
self.apiNextRequest = nextID
return True;
else:
return False
def checkSentiment(self,string):
string.strip()
document = types.Document(
content = string,
language = "en",
type = enums.Document.Type.PLAIN_TEXT)
sentiment = None
retries = 0
while retries < 5:
try:
sentimentData = self.client.analyze_sentiment(document = document).document_sentiment
self.reportData[string] = [sentimentData.score,sentimentData.magnitude]
break
except:
time.sleep(1)
retries = retries + 1
self.totalErrors = self.totalErrors + 1
if(self.totalErrors > 20):
self.APIdisconnect = True