-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Moved the imports and region tags inside the functions #1891
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
Changes from 5 commits
b0285c8
1008e05
677c3d4
d818474
bc600a5
3c8e76b
9539226
3ec1471
ed84c48
a13340d
1224bad
7e76df7
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2016 Google, Inc. | ||
# 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. | ||
|
@@ -24,17 +24,20 @@ | |
import argparse | ||
import sys | ||
|
||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
import six | ||
|
||
|
||
# [START language_sentiment_text] | ||
def sentiment_text(text): | ||
# [START language_sentiment_text] | ||
import six | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
|
||
"""Detects sentiment in the text.""" | ||
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. Please remove. It doesn't make sense to have a docstring if the user doesn't know it's a function. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# text = 'Your text to analyze, e.g. Hello, world!' | ||
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. It would be better to remove the parameter and have the actual text defined here. This will reduce confusion for the user. Only have commented out variables when absolutely necessary (ex. a project ID). 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. So, the function takes no input parameters as text, and we uncomment text to something like this: Yeah? 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. Yes, you are correct on the 'Hello World' example. For the gcs_uri parameter, upload the sample file to the cloud-samples-data storage bucket, and then update to where you uploaded it. For example: 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. Did we finalize that new sample rubric yet? 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 page the example page alix? https://cloud.google.com/natural-language/docs/quickstart-client-libraries 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. It's not officially finalized because we're still waiting on UX testing for the script vs. function formatting. I believe eliminating the use of commented out variables when possible was non-controversial, though. I don't think that link is the "official" example page, though those look like good samples to me. |
||
|
||
if isinstance(text, six.binary_type): | ||
text = text.decode('utf-8') | ||
|
||
|
@@ -51,14 +54,20 @@ def sentiment_text(text): | |
print('Score: {}'.format(sentiment.score)) | ||
print('Magnitude: {}'.format(sentiment.magnitude)) | ||
# [END language_python_migration_sentiment_text] | ||
# [END language_sentiment_text] | ||
# [END language_sentiment_text] | ||
|
||
|
||
# [START language_sentiment_gcs] | ||
def sentiment_file(gcs_uri): | ||
# [START language_sentiment_gcs] | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects sentiment in the file located in Google Cloud Storage.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# gcs_uri = 'The gcs uri to your file, e.g. gs://my_bucket/my_file' | ||
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 URI could be defined here if you put the file in our sample data GCS bucket - gs://cloud-samples-data. This way the user has an easily accessible test file and the snippet tests don't have to upload a file every time. Let me know if you don't have permissions to that project and I'll add you. |
||
|
||
# Instantiates a plain text document. | ||
# [START language_python_migration_document_gcs] | ||
document = types.Document( | ||
|
@@ -72,14 +81,21 @@ def sentiment_file(gcs_uri): | |
|
||
print('Score: {}'.format(sentiment.score)) | ||
print('Magnitude: {}'.format(sentiment.magnitude)) | ||
# [END language_sentiment_gcs] | ||
# [END language_sentiment_gcs] | ||
|
||
|
||
# [START language_entities_text] | ||
def entities_text(text): | ||
# [START language_entities_text] | ||
import six | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects entities in the text.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# text = 'Your text to analyze, e.g. Hello, world!' | ||
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. Same as above. Please replace with the actual text. |
||
|
||
if isinstance(text, six.binary_type): | ||
text = text.decode('utf-8') | ||
|
||
|
@@ -105,14 +121,20 @@ def entities_text(text): | |
print(u'{:<16}: {}'.format('wikipedia_url', | ||
entity.metadata.get('wikipedia_url', '-'))) | ||
# [END language_python_migration_entities_text] | ||
# [END language_entities_text] | ||
# [END language_entities_text] | ||
|
||
|
||
# [START language_entities_gcs] | ||
def entities_file(gcs_uri): | ||
# [START language_entities_gcs] | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects entities in the file located in Google Cloud Storage.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# gcs_uri = 'The gcs uri to your file, e.g. gs://my_bucket/my_file' | ||
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. Same as above. Please load the file to the sample storage bucket. |
||
|
||
# Instantiates a plain text document. | ||
document = types.Document( | ||
gcs_content_uri=gcs_uri, | ||
|
@@ -131,14 +153,21 @@ def entities_file(gcs_uri): | |
print(u'{:<16}: {}'.format('salience', entity.salience)) | ||
print(u'{:<16}: {}'.format('wikipedia_url', | ||
entity.metadata.get('wikipedia_url', '-'))) | ||
# [END language_entities_gcs] | ||
# [END language_entities_gcs] | ||
|
||
|
||
# [START language_syntax_text] | ||
def syntax_text(text): | ||
# [START language_syntax_text] | ||
import six | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects syntax in the text.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# text = 'Your text to analyze, e.g. Hello, world!' | ||
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. Same as above. Please hardcode the text. |
||
|
||
if isinstance(text, six.binary_type): | ||
text = text.decode('utf-8') | ||
|
||
|
@@ -157,14 +186,20 @@ def syntax_text(text): | |
print(u'{}: {}'.format(part_of_speech_tag.name, | ||
token.text.content)) | ||
# [END language_python_migration_syntax_text] | ||
# [END language_syntax_text] | ||
# [END language_syntax_text] | ||
|
||
|
||
# [START language_syntax_gcs] | ||
def syntax_file(gcs_uri): | ||
# [START language_syntax_gcs] | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects syntax in the file located in Google Cloud Storage.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# gcs_uri = 'The gcs uri to your file, e.g. gs://my_bucket/my_file' | ||
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. Same as above. Please upload the file to the sample file bucket. |
||
|
||
# Instantiates a plain text document. | ||
document = types.Document( | ||
gcs_content_uri=gcs_uri, | ||
|
@@ -178,14 +213,21 @@ def syntax_file(gcs_uri): | |
part_of_speech_tag = enums.PartOfSpeech.Tag(token.part_of_speech.tag) | ||
print(u'{}: {}'.format(part_of_speech_tag.name, | ||
token.text.content)) | ||
# [END language_syntax_gcs] | ||
# [END language_syntax_gcs] | ||
|
||
|
||
# [START language_entity_sentiment_text] | ||
def entity_sentiment_text(text): | ||
# [START language_entity_sentiment_text] | ||
import six | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects entity sentiment in the provided text.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# text = 'Your text to analyze, e.g. Hello, world!' | ||
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. Same as above. Please hardcode the text. |
||
|
||
if isinstance(text, six.binary_type): | ||
text = text.decode('utf-8') | ||
|
||
|
@@ -211,14 +253,20 @@ def entity_sentiment_text(text): | |
print(u' Type : {}'.format(mention.type)) | ||
print(u'Salience: {}'.format(entity.salience)) | ||
print(u'Sentiment: {}\n'.format(entity.sentiment)) | ||
# [END language_entity_sentiment_text] | ||
# [END language_entity_sentiment_text] | ||
|
||
|
||
# [START language_entity_sentiment_gcs] | ||
def entity_sentiment_file(gcs_uri): | ||
# [START language_entity_sentiment_gcs] | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Detects entity sentiment in a Google Cloud Storage file.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# gcs_uri = 'The gcs uri to your file, e.g. gs://my_bucket/my_file' | ||
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. Same as above. Please upload the file to the sample bucket. |
||
|
||
document = types.Document( | ||
gcs_content_uri=gcs_uri, | ||
type=enums.Document.Type.PLAIN_TEXT) | ||
|
@@ -240,14 +288,21 @@ def entity_sentiment_file(gcs_uri): | |
print(u' Type : {}'.format(mention.type)) | ||
print(u'Salience: {}'.format(entity.salience)) | ||
print(u'Sentiment: {}\n'.format(entity.sentiment)) | ||
# [END language_entity_sentiment_gcs] | ||
# [END language_entity_sentiment_gcs] | ||
|
||
|
||
# [START language_classify_text] | ||
def classify_text(text): | ||
# [START language_classify_text] | ||
import six | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Classifies content categories of the provided text.""" | ||
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. Please remove. |
||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# text = 'Your text to analyze, e.g. Hello, world!' | ||
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. Same as above. Please hardcode the text. 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 will need to be longer than 20 tokens or the api call will fail. |
||
|
||
if isinstance(text, six.binary_type): | ||
text = text.decode('utf-8') | ||
|
||
|
@@ -261,16 +316,22 @@ def classify_text(text): | |
print(u'=' * 20) | ||
print(u'{:<16}: {}'.format('name', category.name)) | ||
print(u'{:<16}: {}'.format('confidence', category.confidence)) | ||
# [END language_classify_text] | ||
# [END language_classify_text] | ||
|
||
|
||
# [START language_classify_gcs] | ||
def classify_file(gcs_uri): | ||
# [START language_classify_gcs] | ||
from google.cloud import language | ||
from google.cloud.language import enums | ||
from google.cloud.language import types | ||
"""Classifies content categories of the text in a Google Cloud Storage | ||
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. Please remove. |
||
file. | ||
""" | ||
client = language.LanguageServiceClient() | ||
|
||
# TODO(developer): Uncomment the following line to run this code. | ||
# gcs_uri = 'The gcs uri to your file, e.g. gs://my_bucket/my_file' | ||
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. Same as above. Please upload the file to the sample bucket. |
||
|
||
document = types.Document( | ||
gcs_content_uri=gcs_uri, | ||
type=enums.Document.Type.PLAIN_TEXT) | ||
|
@@ -281,7 +342,7 @@ def classify_file(gcs_uri): | |
print(u'=' * 20) | ||
print(u'{:<16}: {}'.format('name', category.name)) | ||
print(u'{:<16}: {}'.format('confidence', category.confidence)) | ||
# [END language_classify_gcs] | ||
# [END language_classify_gcs] | ||
|
||
|
||
if __name__ == '__main__': | ||
|
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.
For the future, I've been told we only need to update this for new files.
It's just
LLC
no.