21
21
22
22
from googleapiclient import discovery
23
23
from googleapiclient .errors import HttpError
24
+ import httplib2
24
25
from oauth2client .client import GoogleCredentials
25
26
import requests
26
27
@@ -30,10 +31,7 @@ def analyze_document(service, document):
30
31
the movie name."""
31
32
logging .info ('Analyzing {}' .format (document .doc_id ))
32
33
33
- sentences , entities = document .extract_all_sentences (service )
34
-
35
- sentiments = [get_sentiment (service , sentence ) for sentence in sentences ]
36
-
34
+ sentiments , entities = document .extract_sentiment_entities (service )
37
35
return sentiments , entities
38
36
39
37
@@ -56,62 +54,35 @@ def get_request_body(text, syntax=True, entities=True, sentiment=True):
56
54
return body
57
55
58
56
59
- def get_sentiment (service , sentence ):
60
- """Get the sentence-level sentiment."""
61
- body = get_request_body (
62
- sentence , syntax = False , entities = True , sentiment = True )
63
-
64
- docs = service .documents ()
65
- request = docs .annotateText (body = body )
66
-
67
- response = request .execute (num_retries = 3 )
68
-
69
- sentiment = response .get ('documentSentiment' )
70
-
71
- if sentiment is None :
72
- return (None , None )
73
- else :
74
- pol = sentiment .get ('polarity' )
75
- mag = sentiment .get ('magnitude' )
76
-
77
- if pol is None and mag is not None :
78
- pol = 0
79
- return (pol , mag )
80
-
81
-
82
57
class Document (object ):
83
58
"""Document class captures a single document of movie reviews."""
84
59
85
60
def __init__ (self , text , doc_id , doc_path ):
86
61
self .text = text
87
62
self .doc_id = doc_id
88
63
self .doc_path = doc_path
89
- self .sentence_entity_pair = None
64
+ self .sentiment_entity_pair = None
90
65
self .label = None
91
66
92
- def extract_all_sentences (self , service ):
67
+ def extract_sentiment_entities (self , service ):
93
68
"""Extract the sentences in a document."""
94
69
95
- if self .sentence_entity_pair is not None :
70
+ if self .sentiment_entity_pair is not None :
96
71
return self .sentence_entity_pair
97
72
98
73
docs = service .documents ()
99
74
request_body = get_request_body (
100
75
self .text ,
101
- syntax = True ,
76
+ syntax = False ,
102
77
entities = True ,
103
- sentiment = False )
78
+ sentiment = True )
104
79
request = docs .annotateText (body = request_body )
105
80
106
81
ent_list = []
107
82
108
83
response = request .execute ()
109
84
entities = response .get ('entities' , [])
110
- sentences = response .get ('sentences' , [])
111
-
112
- sent_list = [
113
- sentence .get ('text' , {}).get ('content' ) for sentence in sentences
114
- ]
85
+ documentSentiment = response .get ('documentSentiment' , {})
115
86
116
87
for entity in entities :
117
88
ent_type = entity .get ('type' )
@@ -120,9 +91,9 @@ def extract_all_sentences(self, service):
120
91
if ent_type == 'PERSON' and wiki_url is not None :
121
92
ent_list .append (wiki_url )
122
93
123
- self .sentence_entity_pair = (sent_list , ent_list )
94
+ self .sentiment_entity_pair = (documentSentiment , ent_list )
124
95
125
- return self .sentence_entity_pair
96
+ return self .sentiment_entity_pair
126
97
127
98
128
99
def to_sentiment_json (doc_id , sent , label ):
@@ -200,18 +171,9 @@ def get_sentiment_entities(service, document):
200
171
"""
201
172
202
173
sentiments , entities = analyze_document (service , document )
174
+ score = sentiments .get ('score' )
203
175
204
- sentiments = [sent for sent in sentiments if sent [0 ] is not None ]
205
- negative_sentiments = [
206
- polarity for polarity , magnitude in sentiments if polarity < 0.0 ]
207
- positive_sentiments = [
208
- polarity for polarity , magnitude in sentiments if polarity > 0.0 ]
209
-
210
- negative = sum (negative_sentiments )
211
- positive = sum (positive_sentiments )
212
- total = positive + negative
213
-
214
- return (total , entities )
176
+ return (score , entities )
215
177
216
178
217
179
def get_sentiment_label (sentiment ):
@@ -318,8 +280,12 @@ def get_service():
318
280
"""Build a client to the Google Cloud Natural Language API."""
319
281
320
282
credentials = GoogleCredentials .get_application_default ()
321
-
322
- return discovery .build ('language' , 'v1beta1' ,
283
+ scoped_credentials = credentials .create_scoped (
284
+ ['https://www.googleapis.com/auth/cloud-platform' ])
285
+ http = httplib2 .Http ()
286
+ scoped_credentials .authorize (http )
287
+ return discovery .build ('language' , 'v1' ,
288
+ http = http ,
323
289
credentials = credentials )
324
290
325
291
0 commit comments