Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.7k
Add small, generated version of language_sentiment_text#1660
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b585a287ed0cab3f11bff29809fa48bb113df700511ac1bb1dabc4cdc62ac9d1b9aa96File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
Member 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 Contributor 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. ContributorAuthor 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 | ||
Member 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 | ||
| 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:]) | ||
Member 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 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 |
| 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.