From 616ec929148f9eef39de9de51803345575e034e8 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 10 Jul 2017 14:23:42 -0700 Subject: [PATCH 01/16] Migrate v1 quickstart to gapic --- language/cloud-client/v1/quickstart.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/language/cloud-client/v1/quickstart.py b/language/cloud-client/v1/quickstart.py index 3fd703a567e..1d743dfff5a 100644 --- a/language/cloud-client/v1/quickstart.py +++ b/language/cloud-client/v1/quickstart.py @@ -19,16 +19,17 @@ def run_quickstart(): # [START language_quickstart] # Imports the Google Cloud client library from google.cloud import language + from google.cloud.language import types # Instantiates a client - language_client = language.Client() + client = language.LanguageServiceClient() # The text to analyze - text = 'Hello, world!' - document = language_client.document_from_text(text) + text = u'Hello, world!' + document = types.Document(content=text, type='PLAIN_TEXT') # Detects the sentiment of the text - sentiment = document.analyze_sentiment().sentiment + sentiment = client.analyze_sentiment(document=document).document_sentiment print('Text: {}'.format(text)) print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude)) From bee630adb599a2fbcc13ff33e98098985f507421 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 10 Jul 2017 15:01:54 -0700 Subject: [PATCH 02/16] Migrate snippets.py to gapic --- language/cloud-client/.DS_Store | Bin 0 -> 6148 bytes language/cloud-client/v1/snippets.py | 49 +++++++++++++++------------ 2 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 language/cloud-client/.DS_Store diff --git a/language/cloud-client/.DS_Store b/language/cloud-client/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f344c851a0ee4f90f50741edcbb6236ebbbc354d GIT binary patch literal 6148 zcmeHK!A`{pJ@TK5l+$r=92a0ahvsOrXzLD-AJ zJA9_tJXH)nbRY%~4!+FJvKg5HW`G%3RR+wdX>F`(fm|0ezzqDF0XiQfDxqUA)u@gR z98?Q{m_xS`w5gY%9BI%om}q`|5!qLbhGr$adW`KG>lp@{#r$6`qDu@SWfEid#21KsjsJmF3xm%a2q`Ow4wopkZ oF4Z_sK|@`|7)w|2E~*mrOEM50gQ-UJpzx1?qJaly;7=L&02eA$o&W#< literal 0 HcmV?d00001 diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index c3205a46e02..b00bb8af00a 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -24,22 +24,27 @@ import argparse from google.cloud import language +from google.cloud.language import types import six +# part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag +POS_TAG = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') + def sentiment_text(text): """Detects sentiment in the text.""" - language_client = language.Client() + client = language.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. - document = language_client.document_from_text(text) + document = types.Document(content=text, type='PLAIN_TEXT') # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - sentiment = document.analyze_sentiment().sentiment + sentiment = client.analyze_sentiment(document).document_sentiment print('Score: {}'.format(sentiment.score)) print('Magnitude: {}'.format(sentiment.magnitude)) @@ -47,14 +52,14 @@ def sentiment_text(text): def sentiment_file(gcs_uri): """Detects sentiment in the file located in Google Cloud Storage.""" - language_client = language.Client() + client = language.LanguageServiceClient() # Instantiates a plain text document. - document = language_client.document_from_url(gcs_uri) + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - sentiment = document.analyze_sentiment().sentiment + sentiment = client.analyze_sentiment(document).document_sentiment print('Score: {}'.format(sentiment.score)) print('Magnitude: {}'.format(sentiment.magnitude)) @@ -62,22 +67,22 @@ def sentiment_file(gcs_uri): def entities_text(text): """Detects entities in the text.""" - language_client = language.Client() + client = language.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. - document = language_client.document_from_text(text) + document = types.Document(content=text, type='PLAIN_TEXT') # Detects entities in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - entities = document.analyze_entities().entities + entities = client.analyze_entities(document).entities for entity in entities: print('=' * 20) print(u'{:<16}: {}'.format('name', entity.name)) - print(u'{:<16}: {}'.format('type', entity.entity_type)) + print(u'{:<16}: {}'.format('type', entity.type)) print(u'{:<16}: {}'.format('metadata', entity.metadata)) print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', @@ -86,19 +91,19 @@ def entities_text(text): def entities_file(gcs_uri): """Detects entities in the file located in Google Cloud Storage.""" - language_client = language.Client() + client = language.LanguageServiceClient() # Instantiates a plain text document. - document = language_client.document_from_url(gcs_uri) + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - entities = document.analyze_entities().entities + entities = client.analyze_entities(document).entities for entity in entities: print('=' * 20) print(u'{:<16}: {}'.format('name', entity.name)) - print(u'{:<16}: {}'.format('type', entity.entity_type)) + print(u'{:<16}: {}'.format('type', entity.type)) print(u'{:<16}: {}'.format('metadata', entity.metadata)) print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', @@ -107,35 +112,35 @@ def entities_file(gcs_uri): def syntax_text(text): """Detects syntax in the text.""" - language_client = language.Client() + client = language.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. - document = language_client.document_from_text(text) + document = types.Document(content=text, type='PLAIN_TEXT') # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - tokens = document.analyze_syntax().tokens + tokens = client.analyze_syntax(document).tokens for token in tokens: - print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content)) + print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) def syntax_file(gcs_uri): """Detects syntax in the file located in Google Cloud Storage.""" - language_client = language.Client() + client = language.LanguageServiceClient() # Instantiates a plain text document. - document = language_client.document_from_url(gcs_uri) + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - tokens = document.analyze_syntax().tokens + tokens = client.analyze_syntax(document).tokens for token in tokens: - print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content)) + print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) if __name__ == '__main__': From e34ab1d0704b10edc5ff6f12b86e2202866f408e Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 11 Jul 2017 11:58:48 -0700 Subject: [PATCH 03/16] migrate v1beta2 quickstart and snippets to gapic, and flake --- language/cloud-client/v1/snippets.py | 6 +- language/cloud-client/v1beta2/quickstart.py | 12 +-- language/cloud-client/v1beta2/snippets.py | 98 ++++++++++----------- 3 files changed, 59 insertions(+), 57 deletions(-) diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index b00bb8af00a..d5200d6d31b 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -125,7 +125,8 @@ def syntax_text(text): tokens = client.analyze_syntax(document).tokens for token in tokens: - print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) + print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + token.text.content)) def syntax_file(gcs_uri): @@ -140,7 +141,8 @@ def syntax_file(gcs_uri): tokens = client.analyze_syntax(document).tokens for token in tokens: - print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) + print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + token.text.content)) if __name__ == '__main__': diff --git a/language/cloud-client/v1beta2/quickstart.py b/language/cloud-client/v1beta2/quickstart.py index c5a4b9c3ebc..16c2bad536b 100644 --- a/language/cloud-client/v1beta2/quickstart.py +++ b/language/cloud-client/v1beta2/quickstart.py @@ -18,17 +18,17 @@ def run_quickstart(): # [START language_quickstart] # Imports the Google Cloud client library - from google.cloud import language + from google.cloud import language_v1beta2 + from google.cloud.language_v1beta2 import types # Instantiates a client with they v1beta2 version - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() # The text to analyze - text = 'Hallo Welt!' - document = language_client.document_from_text(text, language='DE') - + text = u'Hallo Welt!' + document = types.Document(content=text, type='PLAIN_TEXT', language='de') # Detects the sentiment of the text - sentiment = document.analyze_sentiment().sentiment + sentiment = client.analyze_sentiment(document).document_sentiment print('Text: {}'.format(text)) print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude)) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 02d0d8e5b31..2af3c323a68 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -24,64 +24,66 @@ import argparse import sys -from google.cloud import language -from google.cloud.gapic.language.v1beta2 import enums -from google.cloud.gapic.language.v1beta2 import language_service_client -from google.cloud.proto.language.v1beta2 import language_service_pb2 +from google.cloud import language_v1beta2 +from google.cloud.language_v1beta2 import types import six +# part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag +POS_TAG = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') + def sentiment_text(text): """Detects sentiment in the text.""" - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. - document = language_client.document_from_text(text) + document = types.Document(content=text, type='PLAIN_TEXT') # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - sentiment = document.analyze_sentiment().sentiment + sentiment = client.analyze_sentiment(document).document_sentiment - print(u'Score: {}'.format(sentiment.score)) - print(u'Magnitude: {}'.format(sentiment.magnitude)) + print('Score: {}'.format(sentiment.score)) + print('Magnitude: {}'.format(sentiment.magnitude)) def sentiment_file(gcs_uri): """Detects sentiment in the file located in Google Cloud Storage.""" - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = language_client.document_from_gcs_url(gcs_uri) + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - sentiment = document.analyze_sentiment().sentiment + sentiment = client.analyze_sentiment(document).document_sentiment - print(u'Score: {}'.format(sentiment.score)) - print(u'Magnitude: {}'.format(sentiment.magnitude)) + print('Score: {}'.format(sentiment.score)) + print('Magnitude: {}'.format(sentiment.magnitude)) def entities_text(text): """Detects entities in the text.""" - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. - document = language_client.document_from_text(text) + document = types.Document(content=text, type='PLAIN_TEXT') # Detects entities in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - entities = document.analyze_entities().entities + entities = client.analyze_entities(document).entities for entity in entities: - print(u'=' * 20) + print('=' * 20) print(u'{:<16}: {}'.format('name', entity.name)) - print(u'{:<16}: {}'.format('type', entity.entity_type)) + print(u'{:<16}: {}'.format('type', entity.type)) print(u'{:<16}: {}'.format('metadata', entity.metadata)) print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', @@ -90,74 +92,74 @@ def entities_text(text): def entities_file(gcs_uri): """Detects entities in the file located in Google Cloud Storage.""" - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = language_client.document_from_gcs_url(gcs_uri) + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - entities = document.analyze_entities().entities + entities = client.analyze_entities(document).entities for entity in entities: print('=' * 20) - print('{:<16}: {}'.format('name', entity.name)) - print('{:<16}: {}'.format('type', entity.entity_type)) - print('{:<16}: {}'.format('metadata', entity.metadata)) - print('{:<16}: {}'.format('salience', entity.salience)) - print('{:<16}: {}'.format('wikipedia_url', + print(u'{:<16}: {}'.format('name', entity.name)) + print(u'{:<16}: {}'.format('type', entity.type)) + print(u'{:<16}: {}'.format('metadata', entity.metadata)) + print(u'{:<16}: {}'.format('salience', entity.salience)) + print(u'{:<16}: {}'.format('wikipedia_url', entity.metadata.get('wikipedia_url', '-'))) def syntax_text(text): """Detects syntax in the text.""" - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. - document = language_client.document_from_text(text) + document = types.Document(content=text, type='PLAIN_TEXT') # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - tokens = document.analyze_syntax().tokens + tokens = client.analyze_syntax(document).tokens for token in tokens: - print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content)) + print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + token.text.content)) def syntax_file(gcs_uri): """Detects syntax in the file located in Google Cloud Storage.""" - language_client = language.Client(api_version='v1beta2') + client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = language_client.document_from_gcs_url(gcs_uri) + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML - tokens = document.analyze_syntax().tokens + tokens = client.analyze_syntax(document).tokens for token in tokens: - print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content)) + print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + token.text.content)) def entity_sentiment_text(text): """Detects entity sentiment in the provided text.""" - language_client = language_service_client.LanguageServiceClient() - document = language_service_pb2.Document() + client = language_v1beta2.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') - document.content = text.encode('utf-8') - document.type = enums.Document.Type.PLAIN_TEXT + document = types.Document(content=text.encode('utf-8'), type='PLAIN_TEXT') - encoding = enums.EncodingType.UTF32 + encoding = 'UTF32' if sys.maxunicode == 65535: - encoding = enums.EncodingType.UTF16 + encoding = 'UTF16' - result = language_client.analyze_entity_sentiment( + result = client.analyze_entity_sentiment( document, encoding) for entity in result.entities: @@ -175,17 +177,15 @@ def entity_sentiment_text(text): def entity_sentiment_file(gcs_uri): """Detects entity sentiment in a Google Cloud Storage file.""" - language_client = language_service_client.LanguageServiceClient() - document = language_service_pb2.Document() + client = language_v1beta2.LanguageServiceClient() - document.gcs_content_uri = gcs_uri - document.type = enums.Document.Type.PLAIN_TEXT + document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') - encoding = enums.EncodingType.UTF32 + encoding = 'UTF32' if sys.maxunicode == 65535: - encoding = enums.EncodingType.UTF16 + encoding = 'UTF16' - result = language_client.analyze_entity_sentiment( + result = client.analyze_entity_sentiment( document, encoding) for entity in result.entities: From 694092cd0d728fd1b6fb9c794879ffd3ab7e3928 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 11 Jul 2017 12:50:23 -0700 Subject: [PATCH 04/16] update google-cloud-language version in requirements --- language/cloud-client/v1/requirements.txt | 2 +- language/cloud-client/v1beta2/requirements.txt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/language/cloud-client/v1/requirements.txt b/language/cloud-client/v1/requirements.txt index 1d3c69aeae7..1a76afed28d 100644 --- a/language/cloud-client/v1/requirements.txt +++ b/language/cloud-client/v1/requirements.txt @@ -1 +1 @@ -google-cloud-language==0.25.0 +google-cloud-language==0.26.0 diff --git a/language/cloud-client/v1beta2/requirements.txt b/language/cloud-client/v1beta2/requirements.txt index d44360febc2..1a76afed28d 100644 --- a/language/cloud-client/v1beta2/requirements.txt +++ b/language/cloud-client/v1beta2/requirements.txt @@ -1,2 +1 @@ -gapic-google-cloud-language-v1beta2==0.15.3 -google-cloud-language==0.25.0 +google-cloud-language==0.26.0 From 12f1ad8990d4366d39d6649231852f4807ae083d Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 09:51:01 -0700 Subject: [PATCH 05/16] correct version number in requirements.txt --- language/sentiment/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/sentiment/requirements.txt b/language/sentiment/requirements.txt index 1d3c69aeae7..1a76afed28d 100644 --- a/language/sentiment/requirements.txt +++ b/language/sentiment/requirements.txt @@ -1 +1 @@ -google-cloud-language==0.25.0 +google-cloud-language==0.26.0 From d3dc7121307b37152ac37a100f1631cbd86db545 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 14:03:19 -0700 Subject: [PATCH 06/16] add region tags and update sentiment analysis tutorial --- language/cloud-client/v1/snippets.py | 12 ++++++++++++ language/cloud-client/v1beta2/snippets.py | 2 ++ language/sentiment/sentiment_analysis.py | 23 +++++++++++++---------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index d5200d6d31b..1c1c9688ce6 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -32,6 +32,7 @@ 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') +# [START def_sentiment_text] def sentiment_text(text): """Detects sentiment in the text.""" client = language.LanguageServiceClient() @@ -48,8 +49,10 @@ def sentiment_text(text): print('Score: {}'.format(sentiment.score)) print('Magnitude: {}'.format(sentiment.magnitude)) +# [END def_sentiment_text] +# [START def_sentiment_file] def sentiment_file(gcs_uri): """Detects sentiment in the file located in Google Cloud Storage.""" client = language.LanguageServiceClient() @@ -63,8 +66,10 @@ def sentiment_file(gcs_uri): print('Score: {}'.format(sentiment.score)) print('Magnitude: {}'.format(sentiment.magnitude)) +# [END def_sentiment_file] +# [START def_entities_text] def entities_text(text): """Detects entities in the text.""" client = language.LanguageServiceClient() @@ -87,8 +92,10 @@ def entities_text(text): print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', entity.metadata.get('wikipedia_url', '-'))) +# [END def_entities_text] +# [START def_entities_file] def entities_file(gcs_uri): """Detects entities in the file located in Google Cloud Storage.""" client = language.LanguageServiceClient() @@ -108,8 +115,10 @@ def entities_file(gcs_uri): print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', entity.metadata.get('wikipedia_url', '-'))) +# [END def_entities_file] +# [START def_syntax_text] def syntax_text(text): """Detects syntax in the text.""" client = language.LanguageServiceClient() @@ -127,8 +136,10 @@ def syntax_text(text): for token in tokens: print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) +# [END def_syntax_text] +# [START def_syntax_file] def syntax_file(gcs_uri): """Detects syntax in the file located in Google Cloud Storage.""" client = language.LanguageServiceClient() @@ -143,6 +154,7 @@ def syntax_file(gcs_uri): for token in tokens: print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) +# [END def_syntax_file] if __name__ == '__main__': diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 2af3c323a68..f3a41f209b4 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -146,6 +146,7 @@ def syntax_file(gcs_uri): token.text.content)) +# [START def_entity_sentiment_text] def entity_sentiment_text(text): """Detects entity sentiment in the provided text.""" client = language_v1beta2.LanguageServiceClient() @@ -173,6 +174,7 @@ def entity_sentiment_text(text): 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): diff --git a/language/sentiment/sentiment_analysis.py b/language/sentiment/sentiment_analysis.py index c574c31827b..60aec34c448 100644 --- a/language/sentiment/sentiment_analysis.py +++ b/language/sentiment/sentiment_analysis.py @@ -18,12 +18,14 @@ import argparse from google.cloud import language +from google.cloud.language import types # [END sentiment_tutorial_import] +# [START def_print_result] def print_result(annotations): - score = annotations.sentiment.score - magnitude = annotations.sentiment.magnitude + score = annotations.document_sentiment.score + magnitude = annotations.document_sentiment.magnitude for index, sentence in enumerate(annotations.sentences): sentence_sentiment = sentence.sentiment.score @@ -37,23 +39,24 @@ def print_result(annotations): print('Sentiment: score of {} with magnitude of {}'.format( score, magnitude)) return 0 +# [END def_print_result] +# [START def_analyze] def analyze(movie_review_filename): """Run a sentiment analysis request on text within a passed filename.""" - language_client = language.Client() + client = language.LanguageServiceClient() with open(movie_review_filename, 'r') as review_file: # Instantiates a plain text document. - document = language_client.document_from_html(review_file.read()) + content = review_file.read() - # Detects sentiment in the document. - annotations = document.annotate_text(include_sentiment=True, - include_syntax=False, - include_entities=False) + document = types.Document(content=content, type='PLAIN_TEXT') + annotations = client.analyze_sentiment(document=document) - # Print the results - print_result(annotations) + # Print the results + print_result(annotations) +# [END def_analyze] if __name__ == '__main__': From cae06c2958519dec72d0a60e95a3f7c0e0f1fc7b Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 16:01:16 -0700 Subject: [PATCH 07/16] use enums --- language/cloud-client/v1/quickstart.py | 7 ++++++- language/cloud-client/v1/snippets.py | 13 +++++++------ language/cloud-client/v1beta2/quickstart.py | 1 + language/cloud-client/v1beta2/snippets.py | 17 +++++++++-------- language/sentiment/sentiment_analysis.py | 7 ++----- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/language/cloud-client/v1/quickstart.py b/language/cloud-client/v1/quickstart.py index 1d743dfff5a..ad18a3723c0 100644 --- a/language/cloud-client/v1/quickstart.py +++ b/language/cloud-client/v1/quickstart.py @@ -18,15 +18,20 @@ def run_quickstart(): # [START language_quickstart] # Imports the Google Cloud client library + # [START migration_import] from google.cloud import language + from google.cloud.language import enums from google.cloud.language import types + # [END migration_import] # Instantiates a client + # [START migration_client] client = language.LanguageServiceClient() + # [END migration_client] # The text to analyze text = u'Hello, world!' - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects the sentiment of the text sentiment = client.analyze_sentiment(document=document).document_sentiment diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index 1c1c9688ce6..dc644f7fb05 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -24,6 +24,7 @@ import argparse from google.cloud import language +from google.cloud.language import enums from google.cloud.language import types import six @@ -41,7 +42,7 @@ def sentiment_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -58,7 +59,7 @@ def sentiment_file(gcs_uri): client = language.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -78,7 +79,7 @@ def entities_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects entities in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -101,7 +102,7 @@ def entities_file(gcs_uri): client = language.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -127,7 +128,7 @@ def syntax_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -145,7 +146,7 @@ def syntax_file(gcs_uri): client = language.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML diff --git a/language/cloud-client/v1beta2/quickstart.py b/language/cloud-client/v1beta2/quickstart.py index 16c2bad536b..0efcc4e9150 100644 --- a/language/cloud-client/v1beta2/quickstart.py +++ b/language/cloud-client/v1beta2/quickstart.py @@ -19,6 +19,7 @@ def run_quickstart(): # [START language_quickstart] # Imports the Google Cloud client library from google.cloud import language_v1beta2 + from google.cloud.language_v1beta2 import enums from google.cloud.language_v1beta2 import types # Instantiates a client with they v1beta2 version diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index f3a41f209b4..8fe519540f0 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -25,6 +25,7 @@ import sys from google.cloud import language_v1beta2 +from google.cloud.language_v1beta2 import enums from google.cloud.language_v1beta2 import types import six @@ -41,7 +42,7 @@ def sentiment_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -56,7 +57,7 @@ def sentiment_file(gcs_uri): client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -74,7 +75,7 @@ def entities_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects entities in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -95,7 +96,7 @@ def entities_file(gcs_uri): client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -119,7 +120,7 @@ def syntax_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type='PLAIN_TEXT') + document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -135,7 +136,7 @@ def syntax_file(gcs_uri): client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: # document.doc_type == language.Document.HTML @@ -154,7 +155,7 @@ def entity_sentiment_text(text): if isinstance(text, six.binary_type): text = text.decode('utf-8') - document = types.Document(content=text.encode('utf-8'), type='PLAIN_TEXT') + document = types.Document(content=text.encode('utf-8'), type=enums.Document.Type.PLAIN_TEXT) encoding = 'UTF32' if sys.maxunicode == 65535: @@ -181,7 +182,7 @@ 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='PLAIN_TEXT') + document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) encoding = 'UTF32' if sys.maxunicode == 65535: diff --git a/language/sentiment/sentiment_analysis.py b/language/sentiment/sentiment_analysis.py index 60aec34c448..387a676955b 100644 --- a/language/sentiment/sentiment_analysis.py +++ b/language/sentiment/sentiment_analysis.py @@ -18,6 +18,7 @@ import argparse from google.cloud import language +from google.cloud.language import enums from google.cloud.language import types # [END sentiment_tutorial_import] @@ -35,10 +36,6 @@ def print_result(annotations): print('Overall Sentiment: score of {} with magnitude of {}'.format( score, magnitude)) return 0 - - print('Sentiment: score of {} with magnitude of {}'.format( - score, magnitude)) - return 0 # [END def_print_result] @@ -51,7 +48,7 @@ def analyze(movie_review_filename): # Instantiates a plain text document. content = review_file.read() - document = types.Document(content=content, type='PLAIN_TEXT') + document = types.Document(content=content, type=enums.Document.Type.PLAIN_TEXT) annotations = client.analyze_sentiment(document=document) # Print the results From 06025a730d06c2a336beee55502add9fa96ec84a Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 16:47:07 -0700 Subject: [PATCH 08/16] flake --- language/cloud-client/v1/quickstart.py | 4 +- language/cloud-client/v1/snippets.py | 46 +++++++++++++++------ language/cloud-client/v1beta2/quickstart.py | 5 ++- language/cloud-client/v1beta2/snippets.py | 44 +++++++++++++------- language/sentiment/sentiment_analysis.py | 4 +- 5 files changed, 74 insertions(+), 29 deletions(-) diff --git a/language/cloud-client/v1/quickstart.py b/language/cloud-client/v1/quickstart.py index ad18a3723c0..3c19e395a42 100644 --- a/language/cloud-client/v1/quickstart.py +++ b/language/cloud-client/v1/quickstart.py @@ -31,7 +31,9 @@ def run_quickstart(): # The text to analyze text = u'Hello, world!' - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) # Detects the sentiment of the text sentiment = client.analyze_sentiment(document=document).document_sentiment diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index dc644f7fb05..8b65ce7a4e3 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -42,14 +42,20 @@ def sentiment_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + # [START migration_document_text] + # [START migration_analyze_sentiment] + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) + # [END migration_document_text] # Detects sentiment in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML sentiment = client.analyze_sentiment(document).document_sentiment print('Score: {}'.format(sentiment.score)) print('Magnitude: {}'.format(sentiment.magnitude)) + # [END migration_analyze_sentiment] # [END def_sentiment_text] @@ -59,10 +65,14 @@ def sentiment_file(gcs_uri): client = language.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) + # [START migration_document_gcs_uri] + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) + # [END migration_document_gcs_uri] # Detects sentiment in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML sentiment = client.analyze_sentiment(document).document_sentiment print('Score: {}'.format(sentiment.score)) @@ -79,10 +89,13 @@ def entities_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + # [START migration_analyze_entities] + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) # Detects entities in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML entities = client.analyze_entities(document).entities for entity in entities: @@ -93,6 +106,7 @@ def entities_text(text): print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', entity.metadata.get('wikipedia_url', '-'))) + # [END migration_analyze_entities] # [END def_entities_text] @@ -102,10 +116,12 @@ def entities_file(gcs_uri): client = language.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML entities = client.analyze_entities(document).entities for entity in entities: @@ -128,15 +144,19 @@ def syntax_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + # [START migration_analyze_syntax] + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens for token in tokens: print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], token.text.content)) + # [END migration_analyze_syntax] # [END def_syntax_text] @@ -146,10 +166,12 @@ def syntax_file(gcs_uri): client = language.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens for token in tokens: diff --git a/language/cloud-client/v1beta2/quickstart.py b/language/cloud-client/v1beta2/quickstart.py index 0efcc4e9150..e0a6a75cecc 100644 --- a/language/cloud-client/v1beta2/quickstart.py +++ b/language/cloud-client/v1beta2/quickstart.py @@ -27,7 +27,10 @@ def run_quickstart(): # The text to analyze text = u'Hallo Welt!' - document = types.Document(content=text, type='PLAIN_TEXT', language='de') + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT, + language='de') # Detects the sentiment of the text sentiment = client.analyze_sentiment(document).document_sentiment diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 8fe519540f0..a317490eae6 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -42,10 +42,12 @@ def sentiment_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML sentiment = client.analyze_sentiment(document).document_sentiment print('Score: {}'.format(sentiment.score)) @@ -57,10 +59,12 @@ def sentiment_file(gcs_uri): client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML sentiment = client.analyze_sentiment(document).document_sentiment print('Score: {}'.format(sentiment.score)) @@ -75,10 +79,12 @@ def entities_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) # Detects entities in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML entities = client.analyze_entities(document).entities for entity in entities: @@ -96,10 +102,12 @@ def entities_file(gcs_uri): client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) # Detects sentiment in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML entities = client.analyze_entities(document).entities for entity in entities: @@ -120,10 +128,12 @@ def syntax_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + content=text, + type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens for token in tokens: @@ -136,10 +146,12 @@ def syntax_file(gcs_uri): client = language_v1beta2.LanguageServiceClient() # Instantiates a plain text document. - document = types.Document(gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) # Detects syntax in the document. You can also analyze HTML with: - # document.doc_type == language.Document.HTML + # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens for token in tokens: @@ -155,7 +167,9 @@ def entity_sentiment_text(text): 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) + document = types.Document( + content=text.encode('utf-8'), + type=enums.Document.Type.PLAIN_TEXT) encoding = 'UTF32' if sys.maxunicode == 65535: @@ -182,7 +196,9 @@ 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) + document = types.Document( + gcs_content_uri=gcs_uri, + type=enums.Document.Type.PLAIN_TEXT) encoding = 'UTF32' if sys.maxunicode == 65535: diff --git a/language/sentiment/sentiment_analysis.py b/language/sentiment/sentiment_analysis.py index 387a676955b..8ac8575b08e 100644 --- a/language/sentiment/sentiment_analysis.py +++ b/language/sentiment/sentiment_analysis.py @@ -48,7 +48,9 @@ def analyze(movie_review_filename): # Instantiates a plain text document. content = review_file.read() - document = types.Document(content=content, type=enums.Document.Type.PLAIN_TEXT) + document = types.Document( + content=content, + type=enums.Document.Type.PLAIN_TEXT) annotations = client.analyze_sentiment(document=document) # Print the results From 9dba7ccabfd0ed023f883f0d1166d818ed571f86 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 16:57:05 -0700 Subject: [PATCH 09/16] move pos_tag to local variable --- language/cloud-client/v1/snippets.py | 16 ++++++++++------ language/cloud-client/v1beta2/snippets.py | 16 ++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index 8b65ce7a4e3..dcd2ebf4639 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -28,10 +28,6 @@ from google.cloud.language import types import six -# part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag -POS_TAG = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', - 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') - # [START def_sentiment_text] def sentiment_text(text): @@ -153,8 +149,12 @@ def syntax_text(text): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens + # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') + for token in tokens: - print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], token.text.content)) # [END migration_analyze_syntax] # [END def_syntax_text] @@ -174,8 +174,12 @@ def syntax_file(gcs_uri): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens + # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') + for token in tokens: - print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], token.text.content)) # [END def_syntax_file] diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index a317490eae6..5181e32f869 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -29,10 +29,6 @@ from google.cloud.language_v1beta2 import types import six -# part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag -POS_TAG = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', - 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') - def sentiment_text(text): """Detects sentiment in the text.""" @@ -136,8 +132,12 @@ def syntax_text(text): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens + # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') + for token in tokens: - print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], token.text.content)) @@ -154,8 +154,12 @@ def syntax_file(gcs_uri): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens + # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') + for token in tokens: - print(u'{}: {}'.format(POS_TAG[token.part_of_speech.tag], + print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], token.text.content)) From 36c2c192f7db1544893a6cc54d262507b92efd4f Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 16:59:14 -0700 Subject: [PATCH 10/16] use module name --- language/cloud-client/v1/snippets.py | 4 ++-- language/cloud-client/v1beta2/snippets.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/language/cloud-client/v1/snippets.py b/language/cloud-client/v1/snippets.py index dcd2ebf4639..d485752b09c 100644 --- a/language/cloud-client/v1/snippets.py +++ b/language/cloud-client/v1/snippets.py @@ -149,7 +149,7 @@ def syntax_text(text): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens - # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + # part-of-speech tags from enums.PartOfSpeech.Tag pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') @@ -174,7 +174,7 @@ def syntax_file(gcs_uri): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens - # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + # part-of-speech tags from enums.PartOfSpeech.Tag pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index 5181e32f869..e3a5c458471 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -132,7 +132,7 @@ def syntax_text(text): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens - # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + # part-of-speech tags from enums.PartOfSpeech.Tag pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') @@ -154,7 +154,7 @@ def syntax_file(gcs_uri): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens - # part-of-speech tags from google.cloud.language.enums.PartOfSpeech.Tag + # part-of-speech tags from enums.PartOfSpeech.Tag pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') From 92f4f8346de9a8cecbef9586b0e6f31650a1aa1f Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 17:05:33 -0700 Subject: [PATCH 11/16] use enums --- language/cloud-client/v1beta2/snippets.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index e3a5c458471..a9c1b48136a 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -175,9 +175,9 @@ def entity_sentiment_text(text): content=text.encode('utf-8'), type=enums.Document.Type.PLAIN_TEXT) - encoding = 'UTF32' + encoding = enums.EncodingType.UTF32 if sys.maxunicode == 65535: - encoding = 'UTF16' + encoding = enums.EncodingType.UTF16 result = client.analyze_entity_sentiment( document, encoding) @@ -204,9 +204,9 @@ def entity_sentiment_file(gcs_uri): gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) - encoding = 'UTF32' + encoding = enums.EncodingType.UTF32 if sys.maxunicode == 65535: - encoding = 'UTF16' + encoding = enums.EncodingType.UTF16 result = client.analyze_entity_sentiment( document, encoding) From 4777e509a1fc12cd77c88eb24c0a5801b08cdcae Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 19 Jul 2017 17:09:30 -0700 Subject: [PATCH 12/16] remove encoding --- language/cloud-client/v1beta2/snippets.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index a9c1b48136a..c63fca562a2 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -175,12 +175,7 @@ def entity_sentiment_text(text): content=text.encode('utf-8'), type=enums.Document.Type.PLAIN_TEXT) - encoding = enums.EncodingType.UTF32 - if sys.maxunicode == 65535: - encoding = enums.EncodingType.UTF16 - - result = client.analyze_entity_sentiment( - document, encoding) + result = client.analyze_entity_sentiment(document) for entity in result.entities: print('Mentions: ') @@ -204,12 +199,7 @@ def entity_sentiment_file(gcs_uri): gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) - encoding = enums.EncodingType.UTF32 - if sys.maxunicode == 65535: - encoding = enums.EncodingType.UTF16 - - result = client.analyze_entity_sentiment( - document, encoding) + result = client.analyze_entity_sentiment(document) for entity in result.entities: print(u'Name: "{}"'.format(entity.name)) From 0b03649e19a83c8644848dae7e04a47ef844d8ce Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Thu, 20 Jul 2017 11:01:14 -0700 Subject: [PATCH 13/16] update README --- language/cloud-client/v1/README.rst | 4 ++++ language/cloud-client/v1/README.rst.in | 6 ++++++ language/cloud-client/v1beta2/README.rst | 4 ++++ language/cloud-client/v1beta2/README.rst.in | 6 ++++++ 4 files changed, 20 insertions(+) diff --git a/language/cloud-client/v1/README.rst b/language/cloud-client/v1/README.rst index 7e66faa8b8d..319dbb87d98 100644 --- a/language/cloud-client/v1/README.rst +++ b/language/cloud-client/v1/README.rst @@ -5,6 +5,10 @@ Google Cloud Natural Language API Python Samples This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. +- See the `migration guide`_ for information about migrating to Python client library v0.26. + +.. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration + diff --git a/language/cloud-client/v1/README.rst.in b/language/cloud-client/v1/README.rst.in index faf402bfe9c..df9fa3a9809 100644 --- a/language/cloud-client/v1/README.rst.in +++ b/language/cloud-client/v1/README.rst.in @@ -10,6 +10,12 @@ product: entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. + + - See the `migration guide`_ for information about migrating to Python client library v0.26. + + + .. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration + setup: - auth - install_deps diff --git a/language/cloud-client/v1beta2/README.rst b/language/cloud-client/v1beta2/README.rst index 3d260b9df90..b453f442a56 100644 --- a/language/cloud-client/v1beta2/README.rst +++ b/language/cloud-client/v1beta2/README.rst @@ -5,6 +5,10 @@ Google Cloud Natural Language API Python Samples This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. +- See the `migration guide`_ for information about migrating to Python client library v0.26. + +.. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration + diff --git a/language/cloud-client/v1beta2/README.rst.in b/language/cloud-client/v1beta2/README.rst.in index faf402bfe9c..df9fa3a9809 100644 --- a/language/cloud-client/v1beta2/README.rst.in +++ b/language/cloud-client/v1beta2/README.rst.in @@ -10,6 +10,12 @@ product: entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. + + - See the `migration guide`_ for information about migrating to Python client library v0.26. + + + .. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration + setup: - auth - install_deps From c2a7448dd41f69d030ade4f8c841e8fdeba7f96c Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Thu, 20 Jul 2017 15:10:02 -0700 Subject: [PATCH 14/16] remove unused import --- language/cloud-client/v1beta2/quickstart.py | 6 +++++- language/cloud-client/v1beta2/snippets.py | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/language/cloud-client/v1beta2/quickstart.py b/language/cloud-client/v1beta2/quickstart.py index e0a6a75cecc..3cef5fca819 100644 --- a/language/cloud-client/v1beta2/quickstart.py +++ b/language/cloud-client/v1beta2/quickstart.py @@ -18,12 +18,16 @@ def run_quickstart(): # [START language_quickstart] # Imports the Google Cloud client library + # [START beta_import_client] + # [START beta_import] from google.cloud import language_v1beta2 from google.cloud.language_v1beta2 import enums from google.cloud.language_v1beta2 import types + # [END beta_import] - # Instantiates a client with they v1beta2 version + # Instantiates a client with the v1beta2 version client = language_v1beta2.LanguageServiceClient() + # [END beta_import_client] # The text to analyze text = u'Hallo Welt!' diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index c63fca562a2..b00f47b8ddb 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -22,7 +22,6 @@ """ import argparse -import sys from google.cloud import language_v1beta2 from google.cloud.language_v1beta2 import enums From 5125b44d6f46d038b4720882a15bf6dd1a4e5d14 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 24 Jul 2017 10:40:24 -0700 Subject: [PATCH 15/16] encoding should be included to have useful offsets --- language/cloud-client/v1beta2/snippets.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/language/cloud-client/v1beta2/snippets.py b/language/cloud-client/v1beta2/snippets.py index b00f47b8ddb..af7836ba3c6 100644 --- a/language/cloud-client/v1beta2/snippets.py +++ b/language/cloud-client/v1beta2/snippets.py @@ -22,6 +22,7 @@ """ import argparse +import sys from google.cloud import language_v1beta2 from google.cloud.language_v1beta2 import enums @@ -174,7 +175,12 @@ def entity_sentiment_text(text): content=text.encode('utf-8'), type=enums.Document.Type.PLAIN_TEXT) - result = client.analyze_entity_sentiment(document) + # Pass in encoding type to get useful offsets in the response. + 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: ') @@ -198,7 +204,12 @@ def entity_sentiment_file(gcs_uri): gcs_content_uri=gcs_uri, type=enums.Document.Type.PLAIN_TEXT) - result = client.analyze_entity_sentiment(document) + # Pass in encoding type to get useful offsets in the response. + 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)) From 83f7f88d0eb7146b33790293da4fe2b52d3ab664 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Fri, 28 Jul 2017 11:32:35 -0700 Subject: [PATCH 16/16] correct python client library version --- language/cloud-client/v1/README.rst | 2 +- language/cloud-client/v1/README.rst.in | 2 +- language/cloud-client/v1/requirements.txt | 2 +- language/cloud-client/v1beta2/README.rst | 2 +- language/cloud-client/v1beta2/README.rst.in | 2 +- language/cloud-client/v1beta2/requirements.txt | 2 +- language/sentiment/requirements.txt | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/language/cloud-client/v1/README.rst b/language/cloud-client/v1/README.rst index 319dbb87d98..a3ee4b7f641 100644 --- a/language/cloud-client/v1/README.rst +++ b/language/cloud-client/v1/README.rst @@ -5,7 +5,7 @@ Google Cloud Natural Language API Python Samples This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. -- See the `migration guide`_ for information about migrating to Python client library v0.26. +- See the `migration guide`_ for information about migrating to Python client library v0.26.1. .. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration diff --git a/language/cloud-client/v1/README.rst.in b/language/cloud-client/v1/README.rst.in index df9fa3a9809..1b4855fb4f3 100644 --- a/language/cloud-client/v1/README.rst.in +++ b/language/cloud-client/v1/README.rst.in @@ -11,7 +11,7 @@ product: Cloud Machine Learning API. - - See the `migration guide`_ for information about migrating to Python client library v0.26. + - See the `migration guide`_ for information about migrating to Python client library v0.26.1. .. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration diff --git a/language/cloud-client/v1/requirements.txt b/language/cloud-client/v1/requirements.txt index 1a76afed28d..8cd367eac3a 100644 --- a/language/cloud-client/v1/requirements.txt +++ b/language/cloud-client/v1/requirements.txt @@ -1 +1 @@ -google-cloud-language==0.26.0 +google-cloud-language==0.26.1 diff --git a/language/cloud-client/v1beta2/README.rst b/language/cloud-client/v1beta2/README.rst index b453f442a56..77df4ffbec9 100644 --- a/language/cloud-client/v1beta2/README.rst +++ b/language/cloud-client/v1beta2/README.rst @@ -5,7 +5,7 @@ Google Cloud Natural Language API Python Samples This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API. -- See the `migration guide`_ for information about migrating to Python client library v0.26. +- See the `migration guide`_ for information about migrating to Python client library v0.26.1. .. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration diff --git a/language/cloud-client/v1beta2/README.rst.in b/language/cloud-client/v1beta2/README.rst.in index df9fa3a9809..1b4855fb4f3 100644 --- a/language/cloud-client/v1beta2/README.rst.in +++ b/language/cloud-client/v1beta2/README.rst.in @@ -11,7 +11,7 @@ product: Cloud Machine Learning API. - - See the `migration guide`_ for information about migrating to Python client library v0.26. + - See the `migration guide`_ for information about migrating to Python client library v0.26.1. .. _migration guide: https://cloud.google.com/natural-language/docs/python-client-migration diff --git a/language/cloud-client/v1beta2/requirements.txt b/language/cloud-client/v1beta2/requirements.txt index 1a76afed28d..8cd367eac3a 100644 --- a/language/cloud-client/v1beta2/requirements.txt +++ b/language/cloud-client/v1beta2/requirements.txt @@ -1 +1 @@ -google-cloud-language==0.26.0 +google-cloud-language==0.26.1 diff --git a/language/sentiment/requirements.txt b/language/sentiment/requirements.txt index 1a76afed28d..8cd367eac3a 100644 --- a/language/sentiment/requirements.txt +++ b/language/sentiment/requirements.txt @@ -1 +1 @@ -google-cloud-language==0.26.0 +google-cloud-language==0.26.1