Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds snippets and tests for documentation tutorial. #729

Merged
merged 7 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions language/tutorial/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file is used to generate README.rst

product:
name: Google Cloud Natural Language Tutorial
short_name: Cloud Natural Language Tutorial
url: https://cloud.google.com/natural-language/docs/
description: >
The `Google Cloud Natural Language API`_ provides natural language
understanding technologies to developers, including sentiment analysis,
entity recognition, and syntax analysis. This API is part of the larger
Cloud Machine Learning API.

setup:
- auth
- install_deps

samples:
- name: Language tutorial
file: tutorial.py
show_help: true
1 change: 1 addition & 0 deletions language/tutorial/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-api-python-client==1.5.5
19 changes: 19 additions & 0 deletions language/tutorial/reviews/bladerunner-mixed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
I really wanted to love 'Bladerunner' but ultimately I couldn't get
myself to appreciate it fully. However, you may like it if you're into
science fiction, especially if you're interested in the philosophical
exploration of what it means to be human or machine. Some of the gizmos
like the flying cars and the Vouight-Kampff machine (which seemed very
steampunk), were quite cool.

I did find the plot pretty slow and but the dialogue and action sequences
were good. Unlike most science fiction films, this one was mostly quiet, and
not all that much happened, except during the last 15 minutes. I didn't
understand why a unicorn was in the movie. The visual effects were fantastic,
however, and the musical score and overall mood was quite interesting.
A futurist Los Angeles that was both highly polished and also falling apart
reminded me of 'Outland.' Certainly, the style of the film made up for
many of its pedantic plot holes.

If you want your sci-fi to be lasers and spaceships, 'Bladerunner' may
disappoint you. But if you want it to make you think, this movie may
be worth the money.
3 changes: 3 additions & 0 deletions language/tutorial/reviews/bladerunner-neg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
What was Hollywood thinking with this movie! I hated,
hated, hated it. BORING! I went afterwards and demanded my money back.
They refused.
2 changes: 2 additions & 0 deletions language/tutorial/reviews/bladerunner-neutral.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
I neither liked nor disliked this movie. Parts were interesting, but
overall I was left wanting more. The acting was pretty good.
10 changes: 10 additions & 0 deletions language/tutorial/reviews/bladerunner-pos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
`Bladerunner` is often touted as one of the best science fiction films ever
made. Indeed, it satisfies many of the requisites for good sci-fi: a future
world with flying cars and humanoid robots attempting to rebel against their
creators. But more than anything, `Bladerunner` is a fantastic exploration
of the nature of what it means to be human. If we create robots which can
think, will they become human? And if they do, what makes us unique? Indeed,
how can we be sure we're not human in any case? `Bladerunner` explored
these issues before such movies as `The Matrix,' and did so intelligently.
The visual effects and score by Vangelis set the mood. See this movie
in a dark theatre to appreciate it fully. Highly recommended!
76 changes: 76 additions & 0 deletions language/tutorial/tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python

# Copyright 2016 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START full_tutorial_script]
# [START import_libraries]
import argparse

from googleapiclient import discovery

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No empty line between these two.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

from oauth2client.client import GoogleCredentials
# [END import_libraries]


def get_response(filename):
"""Runs sentiment analysis on text within the specified file."""
# [START authenticating_to_the_api]
credentials = GoogleCredentials.get_application_default()
service = discovery.build('language', 'v1', credentials=credentials)
# [END authenticating_to_the_api]
# [START constructing_the_request]
with open(filename, 'r') as review_file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Put an empty newline above control statements
  2. You didn't limit the scope of the with statement as I mentioned in the last round. :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

service_request = service.documents().analyzeSentiment(
body={
'document': {
'type': 'PLAIN_TEXT',
'content': review_file.read(),
}
}
)
response = service_request.execute()
# [END constructing_the_request]
return response


def print_response_contents(response):
"""Prints document sentiment, magnitude, and sentence score."""
# [START parsing_the_response]
score = response['documentSentiment']['score']
magnitude = response['documentSentiment']['magnitude']
for n, sentence in enumerate(response['sentences']):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank newline above control statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

sentence_sentiment = sentence['sentiment']['score']
print('Sentence {} has a sentiment score of {}'.format(n,
sentence_sentiment))
print('Overall Sentiment: score of {} with magnitude of {}'.format(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank newline above this statement, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

score, magnitude))
# [END parsing_the_response]


# [START running_your_application]
def main(filename):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this function now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"""Run sentiment analysis on the file contents given a filename."""
print_response_contents(get_response(filename))
return 0


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'movie_review_filename',
help='The filename of the movie review you\'d like to analyze.')
args = parser.parse_args()
main(args.movie_review_filename)
# [END running_your_application]
# [END full_tutorial_script]
42 changes: 42 additions & 0 deletions language/tutorial/tutorial_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tutorial


def test_neutral():
result = tutorial.get_response('reviews/bladerunner-neutral.txt')
assert result['language'] == 'en'
assert (-1 < result['documentSentiment']['score'] < 1)
assert (0 < result['documentSentiment']['magnitude'] < 2.0)


def test_pos():
result = tutorial.get_response('reviews/bladerunner-pos.txt')
assert result['language'] == 'en'
assert result['documentSentiment']['score'] > 0.0
assert result['documentSentiment']['magnitude'] > 2.0


def test_neg():
result = tutorial.get_response('reviews/bladerunner-neg.txt')
assert result['language'] == 'en'
assert result['documentSentiment']['score'] < 0.0
assert result['documentSentiment']['magnitude'] > 1.0


def test_mixed():
result = tutorial.get_response('reviews/bladerunner-mixed.txt')
assert result['language'] == 'en'
assert (-1 < result['documentSentiment']['score'] < 1)
assert result['documentSentiment']['magnitude'] > 4.0