Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 908fefb

Browse filesBrowse files
author
manshreck
authored
Merge pull request GoogleCloudPlatform#522 from manshreck/master
Add initial Sentiment Analysis Tutorial files for Cloud NL API
2 parents 349cdc8 + 965aff7 commit 908fefb
Copy full SHA for 908fefb

File tree

Expand file treeCollapse file tree

13 files changed

+175
-0
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+175
-0
lines changed

‎language/sentiment/README.txt

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Introduction
2+
3+
This sample contains the code referenced in the
4+
[Sentiment Analysis Tutorial](http://cloud.google.com/natural-language/docs/sentiment-tutorial)
5+
within the Natural Language API Documentation. A full walkthrough of this sample
6+
is located within the documentation.
7+
8+
This sample is a simple illustration of how to construct a sentiment analysis
9+
request and process a response using the API.
10+
11+
## Download the Code
12+
13+
```
14+
git clone sso://python-dev-samples/language/sentiment/
15+
```
16+
17+
### Usage
18+
19+
This sample provides four sample movie reviews which you can
20+
provide to the sample on the command line. (You can also
21+
pass your own text files.)
22+
23+
```
24+
$ python sentiment_analysis.py textfile.txt
25+
Sentiment: polarity of -0.1 with magnitude of 6.7
26+
```
27+

‎language/sentiment/gcloud/.Python

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Library/Frameworks/Python.framework/Versions/2.7/Python

‎language/sentiment/gcloud/bin/python

Copy file name to clipboard
24.4 KB
Binary file not shown.

‎language/sentiment/gcloud/bin/python2

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

‎language/sentiment/mixed.txt

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
I really wanted to love 'Bladerunner' but ultimately I couldn't get
2+
myself to appreciate it fully. However, you may like it if you're into
3+
science fiction, especially if you're interested in the philosophical
4+
exploration of what it means to be human or machine. Some of the gizmos
5+
like the flying cars and the Vouight-Kampff machine (which seemed very
6+
steampunk), were quite cool.
7+
8+
I did find the plot pretty slow and but the dialogue and action sequences
9+
were good. Unlike most science fiction films, this one was mostly quiet, and
10+
not all that much happened, except during the last 15 minutes. I didn't
11+
understand why a unicorn was in the movie. The visual effects were fantastic,
12+
however, and the musical score and overall mood was quite interesting.
13+
A futurist Los Angeles that was both highly polished and also falling apart
14+
reminded me of 'Outland.' Certainly, the style of the film made up for
15+
many of its pedantic plot holes.
16+
17+
If you want your sci-fi to be lasers and spaceships, 'Bladerunner' may
18+
disappoint you. But if you want it to make you think, this movie may
19+
be worth the money.
20+

‎language/sentiment/neg.txt

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
What was Hollywood thinking with this movie! I hated,
2+
hated, hated it. BORING! I went afterwards and demanded my money back.
3+
They refused.
4+

‎language/sentiment/neutral.txt

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
I neither liked nor disliked this movie. Parts were interesting, but
2+
overall I was left wanting more. The acting was pretty good.
3+

‎language/sentiment/out

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SSByZWFsbHkgd2FudGVkIHRvIGxvdmUgJ0JsYWRlcnVubmVyJyBidXQgdWx0aW1hdGVse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iAnT3V0bGFuZC4nIENlcnRhaW5seSwgdGhlIHN0eWxlIG9mIHRoZSBmaWxtIG1hZGUgdXAgZm9yCm1hbnkgb2YgaXRzIHBlZGFudGljIHBsb3QgaG9sZXMuCgpJZiB5b3Ugd2FudCB5b3VyIHNjaS1maSB0byBiZSBsYXNlcnMgYW5kIHNwYWNlc2hpcHMsICdCbGFkZXJ1bm5lcicgbWF5CmRpc2FwcG9pbnQgeW91LiBCdXQgaWYgeW91IHdhbnQgaXQgdG8gbWFrZSB5b3UgdGhpbmssIHRoaXMgbW92aWUgbWF5CmJlIHdvcnRoIHRoZSBtb25leS4KCg==

‎language/sentiment/pos.txt

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
`Bladerunner` is often touted as one of the best science fiction films ever
2+
made. Indeed, it satisfies many of the requisites for good sci-fi: a future
3+
world with flying cars and humanoid robots attempting to rebel against their
4+
creators. But more than anything, `Bladerunner` is a fantastic exploration
5+
of the nature of what it means to be human. If we create robots which can
6+
think, will they become human? And if they do, what makes us unique? Indeed,
7+
how can we be sure we're not human in any case? `Bladerunner` explored
8+
these issues before such movies as `The Matrix,' and did so intelligently.
9+
The visual effects and score by Vangelis set the mood. See this movie
10+
in a dark theatre to appreciate it fully. Highly recommended!
11+
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import argparse
15+
from googleapiclient import discovery
16+
import httplib2
17+
from oauth2client.client import GoogleCredentials
18+
19+
20+
def main(movie_review_filename):
21+
'''Run a sentiment analysis request on text within a passed filename.'''
22+
23+
credentials = GoogleCredentials.get_application_default().create_scoped(
24+
['https://www.googleapis.com/auth/cloud-platform'])
25+
http = httplib2.Http()
26+
credentials.authorize(http)
27+
28+
service = discovery.build('language', 'v1beta1', http=http)
29+
30+
with open(movie_review_filename, 'r') as review_file:
31+
service_request = service.documents().analyzeSentiment(
32+
body={
33+
'document': {
34+
'type': 'PLAIN_TEXT',
35+
'content': review_file.read(),
36+
}
37+
}
38+
)
39+
response = service_request.execute()
40+
41+
try:
42+
polarity = response['documentSentiment']['polarity']
43+
magnitude = response['documentSentiment']['magnitude']
44+
except KeyError:
45+
print("The response did not contain the expected fields.")
46+
print('Sentiment: polarity of %s with magnitude of %s'
47+
% (polarity, magnitude))
48+
return 0
49+
50+
51+
if __name__ == '__main__':
52+
parser = argparse.ArgumentParser()
53+
parser.add_argument(
54+
'movie_review_filename',
55+
help='The filename of the movie review you\'d like to analyze.')
56+
args = parser.parse_args()
57+
main(args.movie_review_filename)
58+
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
from sentiment_analysis import main
16+
17+
18+
def test_pos(resource, capsys):
19+
main(resource('pos.txt'))
20+
out, err = capsys.readouterr()
21+
polarity = re.search('polarity of (.+?) with', out)
22+
magnitude = re.search('magnitude of (.+?)', out)
23+
assert polarity * magnitude > 0
24+
25+
26+
def test_neg(resource, capsys):
27+
main(resource('neg.txt'))
28+
out, err = capsys.readouterr()
29+
polarity = re.search('polarity of (.+?) with', out)
30+
magnitude = re.search('magnitude of (.+?)', out)
31+
assert polarity * magnitude < 0
32+
33+
34+
def test_mixed(resource, capsys):
35+
main(resource('mixed.txt'))
36+
out, err = capsys.readouterr()
37+
polarity = re.search('polarity of (.+?) with', out)
38+
assert polarity <= 0.3 is True
39+
assert polarity >= -0.3 is True
40+
41+
42+
def test_neutral(resource, capsys):
43+
main(resource('neutral.txt'))
44+
out, err = capsys.readouterr()
45+
magnitude = re.search('magnitude of (.+?)', out)
46+
assert magnitude <= 2.0 is True
47+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.