Skip to content

Commit 8bca2aa

Browse files
committed
Refactored the Sentiment Analysis tutorial to use the Cloud Client Library.
1 parent a683165 commit 8bca2aa

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

language/sentiment/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-api-python-client==1.5.5
1+
google-cloud-language==0.22.2

language/sentiment/sentiment_analysis.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,21 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14+
# [START sentiment_tutorial]
1415
"""Demonstrates how to make a simple call to the Natural Language API."""
1516

17+
# [START sentiment_tutorial_import]
1618
import argparse
1719

18-
from googleapiclient import discovery
19-
from oauth2client.client import GoogleCredentials
20+
from google.cloud import language
21+
# [END sentiment_tutorial_import]
2022

23+
def print_result(annotations):
24+
score = annotations.sentiment.score
25+
magnitude = annotations.sentiment.magnitude
2126

22-
def main(movie_review_filename):
23-
"""Run a sentiment analysis request on text within a passed filename."""
24-
25-
credentials = GoogleCredentials.get_application_default()
26-
service = discovery.build('language', 'v1', credentials=credentials)
27-
28-
with open(movie_review_filename, 'r') as review_file:
29-
service_request = service.documents().analyzeSentiment(
30-
body={
31-
'document': {
32-
'type': 'PLAIN_TEXT',
33-
'content': review_file.read(),
34-
}
35-
}
36-
)
37-
response = service_request.execute()
38-
39-
score = response['documentSentiment']['score']
40-
magnitude = response['documentSentiment']['magnitude']
41-
42-
for i, sentence in enumerate(response['sentences']):
43-
sentence_sentiment = sentence['sentiment']['score']
27+
for i, sentence in enumerate(annotations.sentences):
28+
sentence_sentiment = sentence.sentiment.score
4429
print('Sentence {} has a sentiment score of {}'.format(
4530
i, sentence_sentiment))
4631

@@ -53,6 +38,19 @@ def main(movie_review_filename):
5338
return 0
5439

5540

41+
def analyze(movie_review_filename):
42+
"""Run a sentiment analysis request on text within a passed filename."""
43+
language_client = language.Client()
44+
45+
with open(movie_review_filename, 'r') as review_file:
46+
# Instantiates a plain text document.
47+
document = language_client.document_from_html(review_file.read())
48+
49+
# Detects sentiment in the document.
50+
return document.annotate_text(include_sentiment=True,
51+
include_syntax=False, include_entities=False)
52+
53+
5654
if __name__ == '__main__':
5755
parser = argparse.ArgumentParser(
5856
description=__doc__,
@@ -61,4 +59,8 @@ def main(movie_review_filename):
6159
'movie_review_filename',
6260
help='The filename of the movie review you\'d like to analyze.')
6361
args = parser.parse_args()
64-
main(args.movie_review_filename)
62+
# Perform the analysis
63+
annotations = analyze(args.movie_review_filename)
64+
# Print the results
65+
print_result(annotations)
66+
# [END sentiment_tutorial]

0 commit comments

Comments
 (0)