Skip to content

Commit

Permalink
Add small, generated version of language_sentiment_text [(#1660)](G…
Browse files Browse the repository at this point in the history
…oogleCloudPlatform/python-docs-samples#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 googleapis/gapic-generator#2272
  • Loading branch information
beccasaurus authored Aug 30, 2018
1 parent 4aec82a commit 4844f63
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions samples/snippets/cloud-client/v1/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions samples/snippets/generated-samples/v1/language_sentiment_text.py
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions samples/snippets/generated-samples/v1/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-language==1.0.2

0 comments on commit 4844f63

Please sign in to comment.