2121
2222from googleapiclient import discovery
2323from googleapiclient .errors import HttpError
24+ import httplib2
2425from oauth2client .client import GoogleCredentials
2526import requests
2627
@@ -30,10 +31,7 @@ def analyze_document(service, document):
3031 the movie name."""
3132 logging .info ('Analyzing {}' .format (document .doc_id ))
3233
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 )
3735 return sentiments , entities
3836
3937
@@ -56,62 +54,35 @@ def get_request_body(text, syntax=True, entities=True, sentiment=True):
5654 return body
5755
5856
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-
8257class Document (object ):
8358 """Document class captures a single document of movie reviews."""
8459
8560 def __init__ (self , text , doc_id , doc_path ):
8661 self .text = text
8762 self .doc_id = doc_id
8863 self .doc_path = doc_path
89- self .sentence_entity_pair = None
64+ self .sentiment_entity_pair = None
9065 self .label = None
9166
92- def extract_all_sentences (self , service ):
67+ def extract_sentiment_entities (self , service ):
9368 """Extract the sentences in a document."""
9469
95- if self .sentence_entity_pair is not None :
70+ if self .sentiment_entity_pair is not None :
9671 return self .sentence_entity_pair
9772
9873 docs = service .documents ()
9974 request_body = get_request_body (
10075 self .text ,
101- syntax = True ,
76+ syntax = False ,
10277 entities = True ,
103- sentiment = False )
78+ sentiment = True )
10479 request = docs .annotateText (body = request_body )
10580
10681 ent_list = []
10782
10883 response = request .execute ()
10984 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' , {})
11586
11687 for entity in entities :
11788 ent_type = entity .get ('type' )
@@ -120,9 +91,9 @@ def extract_all_sentences(self, service):
12091 if ent_type == 'PERSON' and wiki_url is not None :
12192 ent_list .append (wiki_url )
12293
123- self .sentence_entity_pair = (sent_list , ent_list )
94+ self .sentiment_entity_pair = (documentSentiment , ent_list )
12495
125- return self .sentence_entity_pair
96+ return self .sentiment_entity_pair
12697
12798
12899def to_sentiment_json (doc_id , sent , label ):
@@ -200,18 +171,9 @@ def get_sentiment_entities(service, document):
200171 """
201172
202173 sentiments , entities = analyze_document (service , document )
174+ score = sentiments .get ('score' )
203175
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 )
215177
216178
217179def get_sentiment_label (sentiment ):
@@ -318,8 +280,12 @@ def get_service():
318280 """Build a client to the Google Cloud Natural Language API."""
319281
320282 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 ,
323289 credentials = credentials )
324290
325291
0 commit comments