From 4844f635b2f3277fbfc05a57b15da379fd5361cf Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Thu, 30 Aug 2018 16:01:18 -0700 Subject: [PATCH] Add small, generated version of `language_sentiment_text` [(#1660)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1660) * Generated sample: language_sentiment_text FYI generated from the following YAML GAPIC config: sample_value_sets: - id: analyze_sentiment title: "Analyzing Sentiment" description: "Proof of concept for analyzing sentiment" parameters: defaults: - document.type=PLAIN_TEXT - document.content="Your text to analyze, e.g. Hello, world!" attributes: - parameter: document.content sample_argument: true on_success: - define: sentiment=$resp.document_sentiment - print: - "Score: %s" - sentiment.score - print: - "Magnitude: %s" - sentiment.magnitude samples: standalone: - calling_forms: ".*" value_sets: analyze_sentiment region_tag: language_sentiment_text * Add requirements.txt (not currently generated) * Add test for language_sentiment_text (not currently generated) * Move language_python_migration_document_text Move language_python_migration_document_text so it uses a different snippet in preparation for deprecation of existing language_sentiment_text sample * Rename generated snippets so filename == region tag * Fix test for generated code sample (file rename to match region tag) * Update Copyright year to 2018 in new hand-written file * Fix lint errors of #language_sentiment_text test * Regenerate #language_sentiment_text to fix lint errors (updated Python sample template) * Binary string support in samples! From PR https://github.com/googleapis/gapic-generator/pull/2272 --- samples/snippets/cloud-client/v1/snippets.py | 4 +- .../v1/language_sentiment_text.py | 61 +++++++++++++++++++ .../v1/language_sentiment_text_test.py | 28 +++++++++ .../generated-samples/v1/requirements.txt | 1 + 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/generated-samples/v1/language_sentiment_text.py create mode 100644 samples/snippets/generated-samples/v1/language_sentiment_text_test.py create mode 100644 samples/snippets/generated-samples/v1/requirements.txt diff --git a/samples/snippets/cloud-client/v1/snippets.py b/samples/snippets/cloud-client/v1/snippets.py index 3b1c02f9..a41c7cb3 100644 --- a/samples/snippets/cloud-client/v1/snippets.py +++ b/samples/snippets/cloud-client/v1/snippets.py @@ -39,12 +39,10 @@ def sentiment_text(text): text = text.decode('utf-8') # Instantiates a plain text document. - # [START language_python_migration_document_text] # [START language_python_migration_sentiment_text] document = types.Document( content=text, type=enums.Document.Type.PLAIN_TEXT) - # [END language_python_migration_document_text] # Detects sentiment in the document. You can also analyze HTML with: # document.type == enums.Document.Type.HTML @@ -87,9 +85,11 @@ def entities_text(text): # Instantiates a plain text document. # [START language_python_migration_entities_text] + # [START language_python_migration_document_text] document = types.Document( content=text, type=enums.Document.Type.PLAIN_TEXT) + # [END language_python_migration_document_text] # Detects entities in the document. You can also analyze HTML with: # document.type == enums.Document.Type.HTML diff --git a/samples/snippets/generated-samples/v1/language_sentiment_text.py b/samples/snippets/generated-samples/v1/language_sentiment_text.py new file mode 100644 index 00000000..d99f5d09 --- /dev/null +++ b/samples/snippets/generated-samples/v1/language_sentiment_text.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "analyze_sentiment") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +import sys + +# [START language_sentiment_text] + +from google.cloud import language_v1 +from google.cloud.language_v1 import enums +import six + + +def sample_analyze_sentiment(content): + # [START language_sentiment_text_core] + + client = language_v1.LanguageServiceClient() + + # content = 'Your text to analyze, e.g. Hello, world!' + + if isinstance(content, six.binary_type): + content = content.decode('utf-8') + + type_ = enums.Document.Type.PLAIN_TEXT + document = {'type': type_, 'content': content} + + response = client.analyze_sentiment(document) + sentiment = response.document_sentiment + print('Score: {}'.format(sentiment.score)) + print('Magnitude: {}'.format(sentiment.magnitude)) + + # [END language_sentiment_text_core] + + +# [END language_sentiment_text] + + +def main(): + # FIXME: Convert argv from strings to the correct types. + sample_analyze_sentiment(*sys.argv[1:]) + + +if __name__ == '__main__': + main() diff --git a/samples/snippets/generated-samples/v1/language_sentiment_text_test.py b/samples/snippets/generated-samples/v1/language_sentiment_text_test.py new file mode 100644 index 00000000..e1876da2 --- /dev/null +++ b/samples/snippets/generated-samples/v1/language_sentiment_text_test.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Google, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import language_sentiment_text + + +def test_analyze_sentiment_text_positive(capsys): + language_sentiment_text.sample_analyze_sentiment('Happy Happy Joy Joy') + out, _ = capsys.readouterr() + assert 'Score: 0.' in out + + +def test_analyze_sentiment_text_negative(capsys): + language_sentiment_text.sample_analyze_sentiment('Angry Angry Sad Sad') + out, _ = capsys.readouterr() + assert 'Score: -0.' in out diff --git a/samples/snippets/generated-samples/v1/requirements.txt b/samples/snippets/generated-samples/v1/requirements.txt new file mode 100644 index 00000000..2cbc37eb --- /dev/null +++ b/samples/snippets/generated-samples/v1/requirements.txt @@ -0,0 +1 @@ +google-cloud-language==1.0.2