Skip to content
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

Merged
merged 10 commits into from
Aug 30, 2018
4 changes: 2 additions & 2 deletions language/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]
Copy link
Contributor Author

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.

# [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
57 changes: 57 additions & 0 deletions language/generated-samples/v1/language_sentiment_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be from google.cloud import language_v1 as language?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

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 imports inside the method?

  • because the hand-written snippets are usually in a file alongside many other snippets.py
  • we generate snippets in their own files
  • this means that we can more easily show imports!
  • this ALSO means that we can do this for COMPILED languages as well now!!! 🤘

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update this to as language 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dizcology Is this a blocker or can we revisit after this sample?

from google.cloud.language_v1 import enums
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to not import this separately, but use it as language.enums in the code?



def sample_analyze_sentiment(content):
# [START language_sentiment_text_core]
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

document = types.Document(
        content=text,
        type=enums.Document.Type.PLAIN_TEXT)


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:])
Copy link
Member

Choose a reason for hiding this comment

The 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()
28 changes: 28 additions & 0 deletions language/generated-samples/v1/language_sentiment_text_test.py
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 language/generated-samples/v1/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-language==1.0.2