From 24905ee6105253cc9d363f6a629b241939b7fa97 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Wed, 19 Apr 2017 14:58:27 -0700 Subject: [PATCH] NL v1beta2 [(#908)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/908) * Reorganizes samples, adds new snippet, and demonstrates switching API versions using GAPIC manual layer. * Corrects beta version in link * Copyright dates on new files * Removes README with nav, changes all snippets to use v1beta2 in beta folder * Fixes v1beta2 test on GCS sentiment. --- .../snippets/cloud-client/{ => v1}/README.rst | 0 .../cloud-client/{ => v1}/README.rst.in | 0 .../cloud-client/{ => v1}/quickstart.py | 0 .../cloud-client/{ => v1}/quickstart_test.py | 0 .../cloud-client/{ => v1}/requirements.txt | 0 .../cloud-client/{ => v1}/resources/text.txt | 0 .../cloud-client/{ => v1}/snippets.py | 0 .../cloud-client/{ => v1}/snippets_test.py | 0 .../snippets/cloud-client/v1beta2/README.rst | 144 +++++++++++ .../cloud-client/v1beta2/README.rst.in | 24 ++ .../cloud-client/v1beta2/quickstart.py | 39 +++ .../cloud-client/v1beta2/quickstart_test.py | 22 ++ .../cloud-client/v1beta2/requirements.txt | 2 + .../cloud-client/v1beta2/resources/text.txt | 1 + .../snippets/cloud-client/v1beta2/snippets.py | 236 ++++++++++++++++++ .../cloud-client/v1beta2/snippets_test.py | 71 ++++++ 16 files changed, 539 insertions(+) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/README.rst (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/README.rst.in (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/quickstart.py (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/quickstart_test.py (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/requirements.txt (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/resources/text.txt (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/snippets.py (100%) rename packages/google-cloud-language/samples/snippets/cloud-client/{ => v1}/snippets_test.py (100%) create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst.in create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart.py create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart_test.py create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/requirements.txt create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/resources/text.txt create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets.py create mode 100644 packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets_test.py diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/README.rst b/packages/google-cloud-language/samples/snippets/cloud-client/v1/README.rst similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/README.rst rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/README.rst diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/README.rst.in b/packages/google-cloud-language/samples/snippets/cloud-client/v1/README.rst.in similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/README.rst.in rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/README.rst.in diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/quickstart.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1/quickstart.py similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/quickstart.py rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/quickstart.py diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/quickstart_test.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1/quickstart_test.py similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/quickstart_test.py rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/quickstart_test.py diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/requirements.txt b/packages/google-cloud-language/samples/snippets/cloud-client/v1/requirements.txt similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/requirements.txt rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/requirements.txt diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/resources/text.txt b/packages/google-cloud-language/samples/snippets/cloud-client/v1/resources/text.txt similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/resources/text.txt rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/resources/text.txt diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/snippets.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets.py similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/snippets.py rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets.py diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/snippets_test.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets_test.py similarity index 100% rename from packages/google-cloud-language/samples/snippets/cloud-client/snippets_test.py rename to packages/google-cloud-language/samples/snippets/cloud-client/v1/snippets_test.py diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst new file mode 100644 index 000000000000..49cdec136842 --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst @@ -0,0 +1,144 @@ +.. This file is automatically generated. Do not edit this file directly. + +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. + + + + +.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/ + +Setup +------------------------------------------------------------------------------- + + +Authentication +++++++++++++++ + +Authentication is typically done through `Application Default Credentials`_, +which means you do not have to change the code to authenticate as long as +your environment has credentials. You have a few options for setting up +authentication: + +#. When running locally, use the `Google Cloud SDK`_ + + .. code-block:: bash + + gcloud beta auth application-default login + + +#. When running on App Engine or Compute Engine, credentials are already + set-up. However, you may need to configure your Compute Engine instance + with `additional scopes`_. + +#. You can create a `Service Account key file`_. This file can be used to + authenticate to Google Cloud Platform services from any environment. To use + the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to + the path to the key file, for example: + + .. code-block:: bash + + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json + +.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow +.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using +.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount + +Install Dependencies +++++++++++++++++++++ + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +Quickstart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python quickstart.py + + +Snippets ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ 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} + ... + + This application demonstrates how to perform basic operations with the + Google Cloud Natural Language API + + For more information, the documentation at + 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. + sentiment-text Detects sentiment in the text. + sentiment-file Detects sentiment in the file located in Google Cloud + Storage. + entities-text Detects entities in the text. + entities-file Detects entities in the file located in Google Cloud + Storage. + syntax-text Detects syntax in the text. + syntax-file Detects syntax in the file located in Google Cloud + Storage. + + optional arguments: + -h, --help show this help message and exit + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst.in b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst.in new file mode 100644 index 000000000000..faf402bfe9c0 --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/README.rst.in @@ -0,0 +1,24 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Natural Language API + short_name: Cloud Natural Language API + url: https://cloud.google.com/natural-language/docs/ + description: > + 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. + +setup: +- auth +- install_deps + +samples: +- name: Quickstart + file: quickstart.py +- name: Snippets + file: snippets.py + show_help: true + +cloud_client_library: true diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart.py new file mode 100644 index 000000000000..c5a4b9c3ebc0 --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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. + + +def run_quickstart(): + # [START language_quickstart] + # Imports the Google Cloud client library + from google.cloud import language + + # Instantiates a client with they v1beta2 version + language_client = language.Client(api_version='v1beta2') + + # The text to analyze + text = 'Hallo Welt!' + document = language_client.document_from_text(text, language='DE') + + # Detects the sentiment of the text + sentiment = document.analyze_sentiment().sentiment + + print('Text: {}'.format(text)) + print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude)) + # [END language_quickstart] + + +if __name__ == '__main__': + run_quickstart() diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart_test.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart_test.py new file mode 100644 index 000000000000..839faae2a00d --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/quickstart_test.py @@ -0,0 +1,22 @@ +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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 quickstart + + +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'Sentiment' in out diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/requirements.txt b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/requirements.txt new file mode 100644 index 000000000000..3b8a6a4c1daa --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/requirements.txt @@ -0,0 +1,2 @@ +gapic-google-cloud-language-v1beta2==0.15.3 +google-cloud-language==0.24.1 diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/resources/text.txt b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/resources/text.txt new file mode 100644 index 000000000000..97a1cea02b7a --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/resources/text.txt @@ -0,0 +1 @@ +President Obama is speaking at the White House. \ No newline at end of file diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets.py new file mode 100644 index 000000000000..af4721189562 --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets.py @@ -0,0 +1,236 @@ +#!/usr/bin/env python + +# Copyright 2017 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. + +"""This application demonstrates how to perform basic operations with the +Google Cloud Natural Language API + +For more information, the documentation at +https://cloud.google.com/natural-language/docs. +""" + +import argparse + +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 + + +def sentiment_text(text): + """Detects sentiment in the text.""" + language_client = language.Client(api_version='v1beta2') + + # Instantiates a plain text document. + document = language_client.document_from_text(text) + + # Detects sentiment in the document. You can also analyze HTML with: + # document.doc_type == language.Document.HTML + sentiment = document.analyze_sentiment().sentiment + + 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') + + # Instantiates a plain text document. + document = language_client.document_from_url(gcs_uri) + + # Detects sentiment in the document. You can also analyze HTML with: + # document.doc_type == language.Document.HTML + sentiment = document.analyze_sentiment().sentiment + + 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') + + # Instantiates a plain text document. + document = language_client.document_from_text(text) + + # Detects entities in the document. You can also analyze HTML with: + # document.doc_type == language.Document.HTML + entities = document.analyze_entities().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', + entity.metadata.get('wikipedia_url', '-'))) + + +def entities_file(gcs_uri): + """Detects entities in the file located in Google Cloud Storage.""" + language_client = language.Client(api_version='v1beta2') + + # Instantiates a plain text document. + document = language_client.document_from_url(gcs_uri) + + # Detects sentiment in the document. You can also analyze HTML with: + # document.doc_type == language.Document.HTML + entities = document.analyze_entities().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', + entity.metadata.get('wikipedia_url', '-'))) + + +def syntax_text(text): + """Detects syntax in the text.""" + language_client = language.Client(api_version='v1beta2') + + # Instantiates a plain text document. + document = language_client.document_from_text(text) + + # Detects syntax in the document. You can also analyze HTML with: + # document.doc_type == language.Document.HTML + tokens = document.analyze_syntax().tokens + + for token in tokens: + print('{}: {}'.format(token.part_of_speech, 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') + + # Instantiates a plain text document. + document = language_client.document_from_url(gcs_uri) + + # Detects syntax in the document. You can also analyze HTML with: + # document.doc_type == language.Document.HTML + tokens = document.analyze_syntax().tokens + + for token in tokens: + print('{}: {}'.format(token.part_of_speech, 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() + + document.content = text.encode('utf-8') + document.type = enums.Document.Type.PLAIN_TEXT + + result = language_client.analyze_entity_sentiment( + document, enums.EncodingType.UTF8) + + for entity in result.entities: + print('Mentions: ') + print('Name: "{}"'.format(entity.name)) + for mention in entity.mentions: + print(' Begin Offset : {}'.format(mention.text.begin_offset)) + print(' Content : {}'.format(mention.text.content)) + print(' Magnitude : {}'.format(mention.sentiment.magnitude)) + print(' Sentiment : {}'.format(mention.sentiment.score)) + print(' Type : {}'.format(mention.type)) + print('Salience: {}'.format(entity.salience)) + print('Sentiment: {}\n'.format(entity.sentiment)) + + +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() + + document.gcs_content_uri = gcs_uri + document.type = enums.Document.Type.PLAIN_TEXT + + result = language_client.analyze_entity_sentiment( + document, enums.EncodingType.UTF8) + + for entity in result.entities: + print('Name: "{}"'.format(entity.name)) + for mention in entity.mentions: + print(' Begin Offset : {}'.format(mention.text.begin_offset)) + print(' Content : {}'.format(mention.text.content)) + print(' Magnitude : {}'.format(mention.sentiment.magnitude)) + print(' Sentiment : {}'.format(mention.sentiment.score)) + print(' Type : {}'.format(mention.type)) + print('Salience: {}'.format(entity.salience)) + print('Sentiment: {}\n'.format(entity.sentiment)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + subparsers = parser.add_subparsers(dest='command') + + 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') + + sentiment_file_parser = subparsers.add_parser( + 'sentiment-file', help=sentiment_file.__doc__) + sentiment_file_parser.add_argument('gcs_uri') + + entities_text_parser = subparsers.add_parser( + 'entities-text', help=entities_text.__doc__) + entities_text_parser.add_argument('text') + + entities_file_parser = subparsers.add_parser( + 'entities-file', help=entities_file.__doc__) + entities_file_parser.add_argument('gcs_uri') + + syntax_text_parser = subparsers.add_parser( + 'syntax-text', help=syntax_text.__doc__) + syntax_text_parser.add_argument('text') + + syntax_file_parser = subparsers.add_parser( + 'syntax-file', help=syntax_file.__doc__) + syntax_file_parser.add_argument('gcs_uri') + + args = parser.parse_args() + + if args.command == 'sentiment-text': + sentiment_text(args.text) + elif args.command == 'sentiment-file': + sentiment_file(args.gcs_uri) + elif args.command == 'entities-text': + entities_text(args.text) + elif args.command == 'entities-file': + entities_file(args.gcs_uri) + elif args.command == 'syntax-text': + 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) diff --git a/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets_test.py b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets_test.py new file mode 100644 index 000000000000..d1e6abd0cf6d --- /dev/null +++ b/packages/google-cloud-language/samples/snippets/cloud-client/v1beta2/snippets_test.py @@ -0,0 +1,71 @@ +# Copyright 2017 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 os + +import snippets + +BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] +TEST_FILE_URL = 'gs://{}/text.txt'.format(BUCKET) + + +def test_sentiment_text(capsys): + snippets.sentiment_text('President Obama is speaking at the White House.') + out, _ = capsys.readouterr() + assert 'Score: 0' in out + + +def test_sentiment_file(capsys): + snippets.sentiment_file(TEST_FILE_URL) + out, _ = capsys.readouterr() + assert 'Score: 0' in out + + +def test_entities_text(capsys): + snippets.entities_text('President Obama is speaking at the White House.') + out, _ = capsys.readouterr() + assert 'name' in out + assert ': Obama' in out + + +def test_entities_file(capsys): + snippets.entities_file(TEST_FILE_URL) + out, _ = capsys.readouterr() + assert 'name' in out + assert ': Obama' in out + + +def test_syntax_text(capsys): + snippets.syntax_text('President Obama is speaking at the White House.') + out, _ = capsys.readouterr() + assert 'NOUN: President' in out + + +def test_syntax_file(capsys): + snippets.syntax_file(TEST_FILE_URL) + out, _ = capsys.readouterr() + 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