-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add initial Sentiment Analysis Tutorial files for Cloud NL API #522
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Introduction | ||
|
|
||
| This sample contains the code referenced in the | ||
| [Sentiment Analysis Tutorial](http://cloud.google.com/natural-language/docs/sentiment-tutorial) | ||
| within the 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. | ||
|
|
||
| ## Download the Code | ||
|
|
||
| ``` | ||
| git clone sso://python-dev-samples/language/sentiment/ | ||
| ``` | ||
|
|
||
| ### 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.) | ||
|
|
||
| ``` | ||
| $ python sentiment_analysis.py textfile.txt | ||
| Sentiment: polarity of -0.1 with magnitude of 6.7 | ||
| ``` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /Library/Frameworks/Python.framework/Versions/2.7/Python | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 |
| 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. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| What was Hollywood thinking with this movie! I hated, | ||
|
Contributor
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. Can you rename the |
||
| hated, hated it. BORING! I went afterwards and demanded my money back. | ||
| They refused. | ||
|
|
||
| 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. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| SSByZWFsbHkgd2FudGVkIHRvIGxvdmUgJ0JsYWRlcnVubmVyJyBidXQgdWx0aW1hdGVseSBJIGNvdWxkbid0IGdldApteXNlbGYgdG8gYXBwcmVjaWF0ZSBpdCBmdWxseS4gSG93ZXZlciwgeW91IG1heSBsaWtlIGl0IGlmIHlvdSdyZSBpbnRvCnNjaWVuY2UgZmljdGlvbiwgZXNwZWNpYWxseSBpZiB5b3UncmUgaW50ZXJlc3RlZCBpbiB0aGUgcGhpbG9zb3BoaWNhbApleHBsb3JhdGlvbiBvZiB3aGF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QgeW91LiBCdXQgaWYgeW91IHdhbnQgaXQgdG8gbWFrZSB5b3UgdGhpbmssIHRoaXMgbW92aWUgbWF5CmJlIHdvcnRoIHRoZSBtb25leS4KCg== | ||
|
Contributor
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. I think this file isn't necessary? |
||
| 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! | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # 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 argparse | ||
| from googleapiclient import discovery | ||
| import httplib2 | ||
| 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().create_scoped( | ||
| ['https://www.googleapis.com/auth/cloud-platform']) | ||
| http = httplib2.Http() | ||
| credentials.authorize(http) | ||
|
|
||
| service = discovery.build('language', 'v1beta1', http=http) | ||
|
Contributor
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. You can just do: credentials = GoogleCredentials.get_application_default()
service = discovery.build('language', 'v1beta1', credentials=credentials)The python client lib will take care of adding the right scope, and authorizing the http object. |
||
|
|
||
| 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() | ||
|
|
||
| try: | ||
| polarity = response['documentSentiment']['polarity'] | ||
| magnitude = response['documentSentiment']['magnitude'] | ||
| except KeyError: | ||
| print("The response did not contain the expected fields.") | ||
|
Contributor
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. Just let the
Contributor
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. style: prefer single quotes over double quotes |
||
| print('Sentiment: polarity of %s with magnitude of %s' | ||
| % (polarity, magnitude)) | ||
|
Contributor
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. Prefer using the |
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser() | ||
|
Contributor
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. Add a module-level docstring and set that as the |
||
| 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) | ||
|
|
||
| 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 | ||
|
|
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.
Can you remove the gcloud dir from the checkin?