Skip to content

Commit b7d2b50

Browse files
gguussJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Add snippets and tests for language tutorial. (#729)
1 parent 79d0d16 commit b7d2b50

8 files changed

+177
-0
lines changed

language/tutorial/README.rst.in

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Natural Language Tutorial
5+
short_name: Cloud Natural Language Tutorial
6+
url: https://cloud.google.com/natural-language/docs/
7+
description: >
8+
The `Google Cloud Natural Language API`_ provides natural language
9+
understanding technologies to developers, including sentiment analysis,
10+
entity recognition, and syntax analysis. This API is part of the larger
11+
Cloud Machine Learning API.
12+
13+
setup:
14+
- auth
15+
- install_deps
16+
17+
samples:
18+
- name: Language tutorial
19+
file: tutorial.py
20+
show_help: true

language/tutorial/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client==1.5.5
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I neither liked nor disliked this movie. Parts were interesting, but
2+
overall I was left wanting more. The acting was pretty good.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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!

language/tutorial/tutorial.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google, Inc
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# [START full_tutorial_script]
17+
# [START import_libraries]
18+
import argparse
19+
import io
20+
21+
from googleapiclient import discovery
22+
from oauth2client.client import GoogleCredentials
23+
# [END import_libraries]
24+
25+
26+
def print_sentiment(filename):
27+
"""Prints sentiment analysis on a given file contents."""
28+
# [START authenticating_to_the_api]
29+
credentials = GoogleCredentials.get_application_default()
30+
service = discovery.build('language', 'v1', credentials=credentials)
31+
# [END authenticating_to_the_api]
32+
33+
# [START constructing_the_request]
34+
with io.open(filename, 'r') as review_file:
35+
review_file_contents = review_file.read()
36+
37+
service_request = service.documents().analyzeSentiment(
38+
body={
39+
'document': {
40+
'type': 'PLAIN_TEXT',
41+
'content': review_file_contents,
42+
}
43+
}
44+
)
45+
response = service_request.execute()
46+
# [END constructing_the_request]
47+
48+
# [START parsing_the_response]
49+
score = response['documentSentiment']['score']
50+
magnitude = response['documentSentiment']['magnitude']
51+
52+
for n, sentence in enumerate(response['sentences']):
53+
sentence_sentiment = sentence['sentiment']['score']
54+
print('Sentence {} has a sentiment score of {}'.format(n,
55+
sentence_sentiment))
56+
57+
print('Overall Sentiment: score of {} with magnitude of {}'.format(
58+
score, magnitude))
59+
# [END parsing_the_response]
60+
61+
62+
# [START running_your_application]
63+
if __name__ == '__main__':
64+
parser = argparse.ArgumentParser()
65+
parser.add_argument(
66+
'movie_review_filename',
67+
help='The filename of the movie review you\'d like to analyze.')
68+
args = parser.parse_args()
69+
print_sentiment(args.movie_review_filename)
70+
# [END running_your_application]
71+
# [END full_tutorial_script]

language/tutorial/tutorial_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import re
14+
15+
import tutorial
16+
17+
18+
def test_neutral(capsys):
19+
tutorial.print_sentiment('reviews/bladerunner-neutral.txt')
20+
out, _ = capsys.readouterr()
21+
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I)
22+
assert re.search(
23+
r'Overall Sentiment: score of -?[0-2]\.?[0-9]? with '
24+
r'magnitude of [0-1]\.?[0-9]?', out, re.I)
25+
26+
27+
def test_pos(capsys):
28+
tutorial.print_sentiment('reviews/bladerunner-pos.txt')
29+
out, _ = capsys.readouterr()
30+
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I)
31+
assert re.search(
32+
r'Overall Sentiment: score of [0-9]\.?[0-9]? with '
33+
r'magnitude of [0-9]\.?[0-9]?', out, re.I)
34+
35+
36+
def test_neg(capsys):
37+
tutorial.print_sentiment('reviews/bladerunner-neg.txt')
38+
out, _ = capsys.readouterr()
39+
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I)
40+
assert re.search(
41+
r'Overall Sentiment: score of -[0-9]\.?[0-9]? with '
42+
r'magnitude of [2-7]\.?[0-9]?', out, re.I)
43+
44+
45+
def test_mixed(capsys):
46+
tutorial.print_sentiment('reviews/bladerunner-mixed.txt')
47+
out, _ = capsys.readouterr()
48+
assert re.search(r'Sentence \d has a sentiment score of \d', out, re.I)
49+
assert re.search(
50+
r'Overall Sentiment: score of -?[0-9]\.?[0-9]? with '
51+
r'magnitude of [3-6]\.?[0-9]?', out, re.I)

0 commit comments

Comments
 (0)