diff --git a/language/sentiment/requirements.txt b/language/sentiment/requirements.txt index 2cd2a1334ea..afd4c94e7f9 100644 --- a/language/sentiment/requirements.txt +++ b/language/sentiment/requirements.txt @@ -1 +1 @@ -google-api-python-client==1.5.5 +google-cloud-language==0.22.2 diff --git a/language/sentiment/sentiment_analysis.py b/language/sentiment/sentiment_analysis.py index 6f92fc66567..c574c31827b 100644 --- a/language/sentiment/sentiment_analysis.py +++ b/language/sentiment/sentiment_analysis.py @@ -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)) @@ -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__, @@ -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] diff --git a/language/sentiment/sentiment_analysis_test.py b/language/sentiment/sentiment_analysis_test.py index 7ba1d1446f7..19ec86f17d5 100644 --- a/language/sentiment/sentiment_analysis_test.py +++ b/language/sentiment/sentiment_analysis_test.py @@ -13,11 +13,11 @@ 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)) @@ -25,7 +25,7 @@ def test_pos(resource, capsys): 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)) @@ -33,7 +33,7 @@ def test_neg(resource, capsys): 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 @@ -41,7 +41,7 @@ def test_mixed(resource, capsys): 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