diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets.py index 3b1c02f9c68f..a41c7cb3ccc6 100644 --- a/packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets.py +++ b/packages/google-cloud-language/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/packages/google-cloud-language/samples/snippets/generated-samples/v1/language_sentiment_text.py b/packages/google-cloud-language/samples/snippets/generated-samples/v1/language_sentiment_text.py new file mode 100644 index 000000000000..d99f5d09c3a6 --- /dev/null +++ b/packages/google-cloud-language/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/packages/google-cloud-language/samples/snippets/generated-samples/v1/language_sentiment_text_test.py b/packages/google-cloud-language/samples/snippets/generated-samples/v1/language_sentiment_text_test.py new file mode 100644 index 000000000000..e1876da27525 --- /dev/null +++ b/packages/google-cloud-language/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/packages/google-cloud-language/samples/snippets/generated-samples/v1/requirements.txt b/packages/google-cloud-language/samples/snippets/generated-samples/v1/requirements.txt new file mode 100644 index 000000000000..2cbc37eb15b1 --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/generated-samples/v1/requirements.txt @@ -0,0 +1 @@ +google-cloud-language==1.0.2