-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…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
1 parent
9fed94d
commit fca1973
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
language/snippets/generated-samples/v1/language_sentiment_text.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
28 changes: 28 additions & 0 deletions
28
language/snippets/generated-samples/v1/language_sentiment_text_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
google-cloud-language==1.0.2 |