From 7b6b17f590e8efe547db14f02594b3d310817ddc Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 19 Sep 2017 09:02:38 -0700 Subject: [PATCH 1/3] update readme --- language/cloud-client/v1/README.rst | 9 +++++++-- language/cloud-client/v1beta2/README.rst | 12 +++++------- language/cloud-client/v1beta2/snippets.py | 6 ++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/language/cloud-client/v1/README.rst b/language/cloud-client/v1/README.rst index 165add01cad..8640369e47d 100644 --- a/language/cloud-client/v1/README.rst +++ b/language/cloud-client/v1/README.rst @@ -76,7 +76,7 @@ To run this sample: $ python snippets.py usage: snippets.py [-h] - {sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + {sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} ... This application demonstrates how to perform basic operations with the @@ -86,7 +86,12 @@ To run this sample: https://cloud.google.com/natural-language/docs. positional arguments: - {sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + {sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + sentiment-entities-text + Detects entity sentiment in the provided text. + sentiment-entities-file + Detects entity sentiment in a Google Cloud Storage + file. sentiment-text Detects sentiment in the text. sentiment-file Detects sentiment in the file located in Google Cloud Storage. diff --git a/language/cloud-client/v1beta2/README.rst b/language/cloud-client/v1beta2/README.rst index 8640369e47d..5a17661e3a0 100644 --- a/language/cloud-client/v1beta2/README.rst +++ b/language/cloud-client/v1beta2/README.rst @@ -76,7 +76,7 @@ To run this sample: $ python snippets.py usage: snippets.py [-h] - {sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + {classify-text,classify-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} ... This application demonstrates how to perform basic operations with the @@ -86,12 +86,10 @@ To run this sample: https://cloud.google.com/natural-language/docs. positional arguments: - {sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} - sentiment-entities-text - Detects entity sentiment in the provided text. - sentiment-entities-file - Detects entity sentiment in a Google Cloud Storage - file. + {classify-text,classify-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + classify-text Classifies content categories of the provided text. + classify-file Classifies content categories of the text in a Google + Cloud Storage file. sentiment-text Detects sentiment in the text. sentiment-file Detects sentiment in the file located in Google Cloud Storage. diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 3ccc2933cd7..08d0bb77d24 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -174,7 +174,7 @@ def syntax_file(gcs_uri): # [START def_classify_text] def classify_text(text): - """Classifies the provided text.""" + """Classifies content categories of the provided text.""" # [START beta_client] client = language_v1beta2.LanguageServiceClient() # [END beta_client] @@ -197,7 +197,9 @@ def classify_text(text): # [START def_classify_file] def classify_file(gcs_uri): - """Classifies the text in a Google Cloud Storage file.""" + """Classifies content categories of the text in a Google Cloud Storage + file. + """ client = language_v1beta2.LanguageServiceClient() document = types.Document( From adc45e6d03009cb03f83b6873c1d9884841b59a9 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 19 Sep 2017 09:12:27 -0700 Subject: [PATCH 2/3] keep entity sentiment in v1beta2 as well --- language/cloud-client/v1beta2/snippets.py | 73 +++++++++++++++++++ .../cloud-client/v1beta2/snippets_test.py | 20 +++++ 2 files changed, 93 insertions(+) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 08d0bb77d24..abf16ada560 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -22,6 +22,7 @@ """ import argparse +import sys # [START beta_import] from google.cloud import language_v1beta2 @@ -125,6 +126,66 @@ def entities_file(gcs_uri): entity.metadata.get('wikipedia_url', '-'))) +# [START def_entity_sentiment_text] +def entity_sentiment_text(text): + """Detects entity sentiment in the provided text.""" + client = language_v1beta2.LanguageServiceClient() + + if isinstance(text, six.binary_type): + text = text.decode('utf-8') + + document = types.Document( + content=text.encode('utf-8'), + type=enums.Document.Type.PLAIN_TEXT) + + # Detect and send native Python encoding to receive correct word offsets. + encoding = enums.EncodingType.UTF32 + if sys.maxunicode == 65535: + encoding = enums.EncodingType.UTF16 + + result = client.analyze_entity_sentiment(document, encoding) + + for entity in result.entities: + print('Mentions: ') + print(u'Name: "{}"'.format(entity.name)) + for mention in entity.mentions: + print(u' Begin Offset : {}'.format(mention.text.begin_offset)) + print(u' Content : {}'.format(mention.text.content)) + print(u' Magnitude : {}'.format(mention.sentiment.magnitude)) + print(u' Sentiment : {}'.format(mention.sentiment.score)) + print(u' Type : {}'.format(mention.type)) + print(u'Salience: {}'.format(entity.salience)) + print(u'Sentiment: {}\n'.format(entity.sentiment)) +# [END def_entity_sentiment_text] + + +def entity_sentiment_file(gcs_uri): + """Detects entity sentiment in a Google Cloud Storage file.""" + client = language_v1beta2.LanguageServiceClient() + + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) + + # Detect and send native Python encoding to receive correct word offsets. + encoding = enums.EncodingType.UTF32 + if sys.maxunicode == 65535: + encoding = enums.EncodingType.UTF16 + + result = client.analyze_entity_sentiment(document, encoding) + + for entity in result.entities: + print(u'Name: "{}"'.format(entity.name)) + for mention in entity.mentions: + print(u' Begin Offset : {}'.format(mention.text.begin_offset)) + print(u' Content : {}'.format(mention.text.content)) + print(u' Magnitude : {}'.format(mention.sentiment.magnitude)) + print(u' Sentiment : {}'.format(mention.sentiment.score)) + print(u' Type : {}'.format(mention.type)) + print(u'Salience: {}'.format(entity.salience)) + print(u'Sentiment: {}\n'.format(entity.sentiment)) + + def syntax_text(text): """Detects syntax in the text.""" client = language_v1beta2.LanguageServiceClient() @@ -229,6 +290,14 @@ def classify_file(gcs_uri): 'classify-file', help=classify_file.__doc__) classify_text_parser.add_argument('gcs_uri') + sentiment_entities_text_parser = subparsers.add_parser( + 'sentiment-entities-text', help=entity_sentiment_text.__doc__) + sentiment_entities_text_parser.add_argument('text') + + sentiment_entities_file_parser = subparsers.add_parser( + 'sentiment-entities-file', help=entity_sentiment_file.__doc__) + sentiment_entities_file_parser.add_argument('gcs_uri') + sentiment_text_parser = subparsers.add_parser( 'sentiment-text', help=sentiment_text.__doc__) sentiment_text_parser.add_argument('text') @@ -267,6 +336,10 @@ def classify_file(gcs_uri): syntax_text(args.text) elif args.command == 'syntax-file': syntax_file(args.gcs_uri) + elif args.command == 'sentiment-entities-text': + entity_sentiment_text(args.text) + elif args.command == 'sentiment-entities-file': + entity_sentiment_file(args.gcs_uri) elif args.command == 'classify-text': classify_text(args.text) elif args.command == 'classify-file': diff --git a/language/cloud-client/v1beta2/snippets_test.py b/language/cloud-client/v1beta2/snippets_test.py index d440136b501..5924ffb4917 100644 --- a/language/cloud-client/v1beta2/snippets_test.py +++ b/language/cloud-client/v1beta2/snippets_test.py @@ -69,6 +69,26 @@ def test_syntax_file(capsys): assert 'NOUN: President' in out +def test_sentiment_entities_text(capsys): + snippets.entity_sentiment_text( + 'President Obama is speaking at the White House.') + out, _ = capsys.readouterr() + assert 'Content : White House' in out + + +def test_sentiment_entities_file(capsys): + snippets.entity_sentiment_file(TEST_FILE_URL) + out, _ = capsys.readouterr() + assert 'Content : White House' in out + + +def test_sentiment_entities_utf(capsys): + snippets.entity_sentiment_text( + 'foo→bar') + out, _ = capsys.readouterr() + assert 'Begin Offset : 4' in out + + def test_classify_text(capsys): snippets.classify_text( 'Android is a mobile operating system developed by Google, ' From 01c4f602b51da32536bb5ef63c901c31b4afd5af Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 19 Sep 2017 09:13:06 -0700 Subject: [PATCH 3/3] update readme --- language/cloud-client/v1beta2/README.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/language/cloud-client/v1beta2/README.rst b/language/cloud-client/v1beta2/README.rst index 5a17661e3a0..dc3b85c4be5 100644 --- a/language/cloud-client/v1beta2/README.rst +++ b/language/cloud-client/v1beta2/README.rst @@ -76,7 +76,7 @@ To run this sample: $ python snippets.py usage: snippets.py [-h] - {classify-text,classify-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + {classify-text,classify-file,sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} ... This application demonstrates how to perform basic operations with the @@ -86,10 +86,15 @@ To run this sample: https://cloud.google.com/natural-language/docs. positional arguments: - {classify-text,classify-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} + {classify-text,classify-file,sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file} classify-text Classifies content categories of the provided text. classify-file Classifies content categories of the text in a Google Cloud Storage file. + sentiment-entities-text + Detects entity sentiment in the provided text. + sentiment-entities-file + Detects entity sentiment in a Google Cloud Storage + file. sentiment-text Detects sentiment in the text. sentiment-file Detects sentiment in the file located in Google Cloud Storage.