-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Adds snippets and tests for documentation tutorial. #729
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9fa5a3a
Adds snippets and tests for documentation tutorial.
gguuss db5574e
Updates snippets to have region tags
gguuss 17e15f8
fix for lint, newline after import
gguuss 24d441d
Addresses review feedback
gguuss ef46924
Flattens tutorial code as suggested in review and updates tests accor…
gguuss a9ad263
Removes leading newline for consistency with tutorial.py
gguuss 67a7a9c
Tweak asserts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
google-api-python-client==1.5.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/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 | ||
import io | ||
|
||
from googleapiclient import discovery | ||
from oauth2client.client import GoogleCredentials | ||
# [END import_libraries] | ||
|
||
|
||
def print_sentiment(filename): | ||
"""Prints sentiment analysis on a given file contents.""" | ||
# [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 io.open(filename, 'r') as review_file: | ||
review_file_contents = review_file.read() | ||
|
||
service_request = service.documents().analyzeSentiment( | ||
body={ | ||
'document': { | ||
'type': 'PLAIN_TEXT', | ||
'content': review_file_contents, | ||
} | ||
} | ||
) | ||
response = service_request.execute() | ||
# [END constructing_the_request] | ||
|
||
# [START parsing_the_response] | ||
score = response['documentSentiment']['score'] | ||
magnitude = response['documentSentiment']['magnitude'] | ||
|
||
for n, sentence in enumerate(response['sentences']): | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank newline above this statement, please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
score, magnitude)) | ||
# [END parsing_the_response] | ||
|
||
|
||
# [START running_your_application] | ||
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() | ||
print_sentiment(args.movie_review_filename) | ||
# [END running_your_application] | ||
# [END full_tutorial_script] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 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 re | ||
|
||
import tutorial | ||
|
||
|
||
def test_neutral(capsys): | ||
tutorial.print_sentiment('reviews/bladerunner-neutral.txt') | ||
out, _ = capsys.readouterr() | ||
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I) | ||
assert re.search( | ||
r'Overall Sentiment: score of -?[0-2]\.?[0-9]? with ' | ||
r'magnitude of [0-1]\.?[0-9]?', out, re.I) | ||
|
||
|
||
def test_pos(capsys): | ||
tutorial.print_sentiment('reviews/bladerunner-pos.txt') | ||
out, _ = capsys.readouterr() | ||
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I) | ||
assert re.search( | ||
r'Overall Sentiment: score of [0-9]\.?[0-9]? with ' | ||
r'magnitude of [0-9]\.?[0-9]?', out, re.I) | ||
|
||
|
||
def test_neg(capsys): | ||
tutorial.print_sentiment('reviews/bladerunner-neg.txt') | ||
out, _ = capsys.readouterr() | ||
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I) | ||
assert re.search( | ||
r'Overall Sentiment: score of -[0-9]\.?[0-9]? with ' | ||
r'magnitude of [2-7]\.?[0-9]?', out, re.I) | ||
|
||
|
||
def test_mixed(capsys): | ||
tutorial.print_sentiment('reviews/bladerunner-mixed.txt') | ||
out, _ = capsys.readouterr() | ||
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I) | ||
assert re.search( | ||
r'Overall Sentiment: score of -?[0-9]\.?[0-9]? with ' | ||
r'magnitude of [3-6]\.?[0-9]?', out, re.I) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.