-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Add initial Sentiment Analysis tutorial to Natural Language samples #524
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc396d0
Add sentiment analysis sample
3e90d6f
Move sample review files into resources directory
manshreck 6d51ccc
Remove blank line from end of file
manshreck 148f5be
Update set up instructions to point to getting started guide
manshreck b0c69b4
Update README to remove need to set up gcloud. Itemize what setting
manshreck 80bf146
Update NL README to link to Sentiment tutorial code
manshreck 8e645c2
Merge branch 'master' of https://github.com/manshreck/python-docs-sam…
manshreck f11678e
Coerce number types before comparison
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,48 @@ | ||
# Introduction | ||
|
||
This sample contains the code referenced in the | ||
[Sentiment Analysis Tutorial](http://cloud.google.com/natural-language/docs/sentiment-tutorial) | ||
within the Google Cloud Natural Language API Documentation. A full walkthrough of this sample | ||
is located within the documentation. | ||
|
||
This sample is a simple illustration of how to construct a sentiment analysis | ||
request and process a response using the API. | ||
|
||
## Prerequisites | ||
|
||
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app). | ||
|
||
2. Setup the gcloud tool. This provides authentication to Google Cloud APIs and services. | ||
|
||
``` | ||
$ gcloud init | ||
``` | ||
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. Actually, I think this won't work. Does |
||
|
||
|
||
## Download the Code | ||
|
||
``` | ||
$ git clone https://github.com/GoogleCloudPlatform/python-dev-samples/language/sentiment/ | ||
$ cd python-docs-samples/language/sentiment | ||
``` | ||
|
||
## Run the Code | ||
|
||
Open a sample folder, create a virtualenv, install dependencies, and run the sample: | ||
|
||
``` | ||
$ virtualenv env | ||
$ source env/bin/activate | ||
(env)$ pip install -r requirements.txt | ||
``` | ||
|
||
### Usage | ||
|
||
This sample provides four sample movie reviews which you can | ||
provide to the sample on the command line. (You can also | ||
pass your own text files.) | ||
|
||
``` | ||
(env)$ python sentiment_analysis.py textfile.txt | ||
Sentiment: polarity of -0.1 with magnitude of 6.7 | ||
``` |
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 @@ | ||
google-api-python-client==1.5.3 | ||
|
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 @@ | ||
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,4 @@ | ||
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,3 @@ | ||
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,11 @@ | ||
`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,55 @@ | ||
# 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. | ||
|
||
'''Demonstrates how to make a simple call to the Natural Language API''' | ||
|
||
import argparse | ||
from googleapiclient import discovery | ||
from oauth2client.client import GoogleCredentials | ||
|
||
|
||
def main(movie_review_filename): | ||
'''Run a sentiment analysis request on text within a passed filename.''' | ||
|
||
credentials = GoogleCredentials.get_application_default() | ||
service = discovery.build('language', 'v1beta1', credentials=credentials) | ||
|
||
with open(movie_review_filename, 'r') as review_file: | ||
service_request = service.documents().analyzeSentiment( | ||
body={ | ||
'document': { | ||
'type': 'PLAIN_TEXT', | ||
'content': review_file.read(), | ||
} | ||
} | ||
) | ||
response = service_request.execute() | ||
|
||
polarity = response['documentSentiment']['polarity'] | ||
magnitude = response['documentSentiment']['magnitude'] | ||
|
||
print('Sentiment: polarity of {} with magnitude of {}'.format( | ||
polarity, magnitude)) | ||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
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) | ||
|
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,47 @@ | ||
# 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 | ||
from sentiment_analysis import main | ||
|
||
|
||
def test_pos(resource, capsys): | ||
main(resource('pos.txt')) | ||
out, err = capsys.readouterr() | ||
polarity = re.search('polarity of (.+?) with', out) | ||
magnitude = re.search('magnitude of (.+?)', out) | ||
assert polarity * magnitude > 0 | ||
|
||
|
||
def test_neg(resource, capsys): | ||
main(resource('neg.txt')) | ||
out, err = capsys.readouterr() | ||
polarity = re.search('polarity of (.+?) with', out) | ||
magnitude = re.search('magnitude of (.+?)', out) | ||
assert polarity * magnitude < 0 | ||
|
||
|
||
def test_mixed(resource, capsys): | ||
main(resource('mixed.txt')) | ||
out, err = capsys.readouterr() | ||
polarity = re.search('polarity of (.+?) with', out) | ||
assert polarity <= 0.3 is True | ||
assert polarity >= -0.3 is True | ||
|
||
|
||
def test_neutral(resource, capsys): | ||
main(resource('neutral.txt')) | ||
out, err = capsys.readouterr() | ||
magnitude = re.search('magnitude of (.+?)', out) | ||
assert magnitude <= 2.0 is True | ||
|
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.
This step isn't necessary anymore