Skip to content

Refactored the Sentiment Analysis tutorial to use the Cloud Client Lib #713

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 1 commit into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion language/sentiment/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-api-python-client==1.5.5
google-cloud-language==0.22.2
55 changes: 30 additions & 25 deletions language/sentiment/sentiment_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START sentiment_tutorial]
"""Demonstrates how to make a simple call to the Natural Language API."""

# [START sentiment_tutorial_import]
import argparse

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.cloud import language
# [END sentiment_tutorial_import]


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', 'v1', credentials=credentials)
def print_result(annotations):
score = annotations.sentiment.score
magnitude = annotations.sentiment.magnitude

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()

score = response['documentSentiment']['score']
magnitude = response['documentSentiment']['magnitude']

for i, sentence in enumerate(response['sentences']):
sentence_sentiment = sentence['sentiment']['score']
for index, sentence in enumerate(annotations.sentences):
sentence_sentiment = sentence.sentiment.score
print('Sentence {} has a sentiment score of {}'.format(
i, sentence_sentiment))
index, sentence_sentiment))

print('Overall Sentiment: score of {} with magnitude of {}'.format(
score, magnitude))
Expand All @@ -53,6 +39,23 @@ def main(movie_review_filename):
return 0


def analyze(movie_review_filename):
"""Run a sentiment analysis request on text within a passed filename."""
language_client = language.Client()

with open(movie_review_filename, 'r') as review_file:
# Instantiates a plain text document.
document = language_client.document_from_html(review_file.read())

# Detects sentiment in the document.
annotations = document.annotate_text(include_sentiment=True,
include_syntax=False,
include_entities=False)

# Print the results
print_result(annotations)


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
Expand All @@ -61,4 +64,6 @@ def main(movie_review_filename):
'movie_review_filename',
help='The filename of the movie review you\'d like to analyze.')
args = parser.parse_args()
main(args.movie_review_filename)

analyze(args.movie_review_filename)
# [END sentiment_tutorial]
10 changes: 5 additions & 5 deletions language/sentiment/sentiment_analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@

import re

from sentiment_analysis import main
from sentiment_analysis import analyze


def test_pos(resource, capsys):
main(resource('pos.txt'))
analyze(resource('pos.txt'))
out, err = capsys.readouterr()
score = float(re.search('score of (.+?) with', out).group(1))
magnitude = float(re.search('magnitude of (.+?)', out).group(1))
assert score * magnitude > 0


def test_neg(resource, capsys):
main(resource('neg.txt'))
analyze(resource('neg.txt'))
out, err = capsys.readouterr()
score = float(re.search('score of (.+?) with', out).group(1))
magnitude = float(re.search('magnitude of (.+?)', out).group(1))
assert score * magnitude < 0


def test_mixed(resource, capsys):
main(resource('mixed.txt'))
analyze(resource('mixed.txt'))
out, err = capsys.readouterr()
score = float(re.search('score of (.+?) with', out).group(1))
assert score <= 0.3
assert score >= -0.3


def test_neutral(resource, capsys):
main(resource('neutral.txt'))
analyze(resource('neutral.txt'))
out, err = capsys.readouterr()
magnitude = float(re.search('magnitude of (.+?)', out).group(1))
assert magnitude <= 2.0