-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add small, generated version of language_sentiment_text
#1660
Changes from 6 commits
b585a28
7ed0cab
3f11bff
29809fa
48bb113
df70051
1ac1bb1
dabc4cd
c62ac9d
1b9aa96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- 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 run with the published packages, execute the following before running this code: | ||
# pip install google-cloud-language | ||
|
||
# [START language_sentiment_text] | ||
|
||
from google.cloud import language_v1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we also trying to move the imports to inside the code snippet and inside the method? Not sure what's possible with the auto-gen tool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Either! There's a current project to decide on this moving forward. But, that aside, we should prefer imports > no imports. We can put them into the method or outside of the method. Why do some of the current Python samples have the
|
||
from google.cloud.language_v1 import enums | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to not import this separately, but use it as |
||
|
||
|
||
def sample_analyze_sentiment(content): | ||
# [START language_sentiment_text_core] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't seen this before, but are the 2 region tags necessary or which one would we use in docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, right! These are here for API Explorer, as it was described to me. I'll look into removing this – it doesn't display for users :) |
||
|
||
client = language_v1.LanguageServiceClient() | ||
|
||
# content = 'Your text to analyze, e.g. Hello, world!' | ||
|
||
type_ = enums.Document.Type.PLAIN_TEXT | ||
document = {'type': type_, 'content': content} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to get the tool to use the helper method here?
|
||
|
||
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(): | ||
import sys | ||
sample_analyze_sentiment(*sys.argv[1:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is okay, but if we are going to keep the command line interface (probably not), it's still better to use argparse if possible. |
||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
# 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 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
google-cloud-language==1.0.2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hand-written version of the snippet will be deprecated later.
In preparation, I moved this region tag to a different snippet (below) so it'll be safe to delete this hand-written snippet.