Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 31aeffb

Browse filesBrowse files
authored
Add small, generated version of language_sentiment_text (GoogleCloudPlatform#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
1 parent 84c2cb2 commit 31aeffb
Copy full SHA for 31aeffb

File tree

Expand file treeCollapse file tree

4 files changed

+92
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+92
-2
lines changed

‎language/cloud-client/v1/snippets.py

Copy file name to clipboardExpand all lines: language/cloud-client/v1/snippets.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ def sentiment_text(text):
3939
text = text.decode('utf-8')
4040

4141
# Instantiates a plain text document.
42-
# [START language_python_migration_document_text]
4342
# [START language_python_migration_sentiment_text]
4443
document = types.Document(
4544
content=text,
4645
type=enums.Document.Type.PLAIN_TEXT)
47-
# [END language_python_migration_document_text]
4846

4947
# Detects sentiment in the document. You can also analyze HTML with:
5048
# document.type == enums.Document.Type.HTML
@@ -87,9 +85,11 @@ def entities_text(text):
8785

8886
# Instantiates a plain text document.
8987
# [START language_python_migration_entities_text]
88+
# [START language_python_migration_document_text]
9089
document = types.Document(
9190
content=text,
9291
type=enums.Document.Type.PLAIN_TEXT)
92+
# [END language_python_migration_document_text]
9393

9494
# Detects entities in the document. You can also analyze HTML with:
9595
# document.type == enums.Document.Type.HTML
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2018 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# DO NOT EDIT! This is a generated sample ("Request", "analyze_sentiment")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-language
21+
22+
import sys
23+
24+
# [START language_sentiment_text]
25+
26+
from google.cloud import language_v1
27+
from google.cloud.language_v1 import enums
28+
import six
29+
30+
31+
def sample_analyze_sentiment(content):
32+
# [START language_sentiment_text_core]
33+
34+
client = language_v1.LanguageServiceClient()
35+
36+
# content = 'Your text to analyze, e.g. Hello, world!'
37+
38+
if isinstance(content, six.binary_type):
39+
content = content.decode('utf-8')
40+
41+
type_ = enums.Document.Type.PLAIN_TEXT
42+
document = {'type': type_, 'content': content}
43+
44+
response = client.analyze_sentiment(document)
45+
sentiment = response.document_sentiment
46+
print('Score: {}'.format(sentiment.score))
47+
print('Magnitude: {}'.format(sentiment.magnitude))
48+
49+
# [END language_sentiment_text_core]
50+
51+
52+
# [END language_sentiment_text]
53+
54+
55+
def main():
56+
# FIXME: Convert argv from strings to the correct types.
57+
sample_analyze_sentiment(*sys.argv[1:])
58+
59+
60+
if __name__ == '__main__':
61+
main()
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2018 Google, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import language_sentiment_text
17+
18+
19+
def test_analyze_sentiment_text_positive(capsys):
20+
language_sentiment_text.sample_analyze_sentiment('Happy Happy Joy Joy')
21+
out, _ = capsys.readouterr()
22+
assert 'Score: 0.' in out
23+
24+
25+
def test_analyze_sentiment_text_negative(capsys):
26+
language_sentiment_text.sample_analyze_sentiment('Angry Angry Sad Sad')
27+
out, _ = capsys.readouterr()
28+
assert 'Score: -0.' in out
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-language==1.0.2

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.