Skip to content

feat(language): update code example for Cloud NL v2 - python#10544

Merged
telpirion merged 23 commits into
GoogleCloudPlatform:mainfrom
kornosk:nl-v2
Aug 24, 2023
Merged

feat(language): update code example for Cloud NL v2 - python#10544
telpirion merged 23 commits into
GoogleCloudPlatform:mainfrom
kornosk:nl-v2

Conversation

@kornosk
Copy link
Copy Markdown
Contributor

@kornosk kornosk commented Aug 22, 2023

Description

Fixes b/296927741

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

@product-auto-label product-auto-label Bot added samples Issues that are directly related to samples. api: language Issues related to the Cloud Natural Language API API. labels Aug 22, 2023
@kornosk kornosk marked this pull request as ready for review August 22, 2023 19:52
@kornosk kornosk requested review from a team as code owners August 22, 2023 19:52
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented Aug 22, 2023

Here is the summary of changes.

You are about to add 6 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@kornosk kornosk changed the title Update code example for Cloud NL v2 - python Update code example for Cloud NL v2 - python (analyze_entities) Aug 22, 2023
@dandhlee dandhlee added the kokoro:run Add this label to force Kokoro to re-run the tests. label Aug 22, 2023
@kokoro-team kokoro-team removed the kokoro:run Add this label to force Kokoro to re-run the tests. label Aug 22, 2023
@kornosk kornosk marked this pull request as draft August 22, 2023 21:44
Copy link
Copy Markdown
Collaborator

@dandhlee dandhlee left a comment

Choose a reason for hiding this comment

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

Will you be adding tests?

Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py
Comment thread language/v2/language_entities_text.py Outdated
Comment thread language/v2/tests/analyzing_entities.test.yaml Outdated
Comment thread language/v2/language_entities_text.py
kornosk and others added 6 commits August 22, 2023 15:23
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
@kornosk
Copy link
Copy Markdown
Contributor Author

kornosk commented Aug 22, 2023

I copied the test folder from https://github.com/GoogleCloudPlatform/python-docs-samples/tree/HEAD/language/v1/test. Does this work?

Plan to have the other API samples once this language_entities looks good.

@kornosk kornosk marked this pull request as ready for review August 22, 2023 22:48
@kornosk kornosk requested a review from dandhlee August 22, 2023 22:49
@dandhlee
Copy link
Copy Markdown
Collaborator

Gotcha. I'm not entirely sure how the test layout is done since this sample was from a client library repository. I'll leave it to the product team to help verify, but looks like the tests are included in the new run under v2!

@kornosk
Copy link
Copy Markdown
Contributor Author

kornosk commented Aug 22, 2023

@dandhlee Thank you for the review! I've addressed all your comments. Please let me know if you have any more concerns.

dandhlee
dandhlee previously approved these changes Aug 23, 2023
Copy link
Copy Markdown
Collaborator

@dandhlee dandhlee left a comment

Choose a reason for hiding this comment

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

Reviewing for Python style.

Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
Comment thread language/v2/language_entities_text.py Outdated
Comment thread language/v2/language_entities_text.py Outdated
Comment thread language/v2/language_entities_gcs.py Outdated
kornosk and others added 6 commits August 22, 2023 22:10
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
@kornosk kornosk changed the title Update code example for Cloud NL v2 - python (analyze_entities) Update code example for Cloud NL v2 - python Aug 23, 2023
@dandhlee dandhlee dismissed their stale review August 23, 2023 20:01

more filed added, taking a quick look

Comment thread language/v2/language_classify_gcs.py Outdated
Comment thread language/v2/language_entities_text.py Outdated
Comment thread language/v2/language_sentiment_text.py
@telpirion telpirion changed the title Update code example for Cloud NL v2 - python feat(language): update code example for Cloud NL v2 - python Aug 24, 2023
@telpirion telpirion added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Aug 24, 2023
@telpirion
Copy link
Copy Markdown
Contributor

Be sure to review the Python style guidance and Style Guide.

Copy link
Copy Markdown
Collaborator

@dandhlee dandhlee left a comment

Choose a reason for hiding this comment

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

Reviewing for Python style.

@kornosk
Copy link
Copy Markdown
Contributor Author

kornosk commented Aug 24, 2023

Added tests and minor changes in style.

Comment thread language/v2/language_classify_gcs.py Outdated
# [END language_classify_gcs]


def main():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

@kornosk kornosk Aug 24, 2023

Choose a reason for hiding this comment

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

Not sure why you still see this but I've removed all the main() functions already. Probably a github glitch or browser not refreshed.

Comment thread language/v2/language_classify_text.py Outdated
# [END language_classify_text]


def main():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as above, no CLI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's already removed.

language_classify_gcs.sample_classify_text()
captured = capsys.readouterr()
assert (
"TV" in captured.out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue: don't test for model results in these integration tests. Model changes can affect API output, which leads to these tests being broken.

Instead, test for the string literals printed to stdout (e.g. "Confidence" and "Category"). These literals won't be printed if the call to the API fails, which provides sufficient signal for sample quality.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

language_classify_text.sample_classify_text()
captured = capsys.readouterr()
assert (
"TV" in captured.out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See previous about API output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

captured = capsys.readouterr()
assert (
"Representative name for the entity: California" in captured.out
and "Entity type: LOCATION" in captured.out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See previous about testing for API output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread language/v2/language_entities_text.py Outdated
# the language specified in the request or, if not specified,
# the automatically-detected language.
print(f"Language of the text: {response.language_code}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove extraneous line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

language_entities_text.sample_analyze_entities()
captured = capsys.readouterr()
assert (
"Representative name for the entity: California" in captured.out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See previous about testing for output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread language/v2/language_sentiment_gcs.py Outdated
# the language specified in the request or, if not specified,
# the automatically-detected language.
print(f"Language of the text: {response.language_code}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove extraneous empty line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

language_sentiment_gcs.sample_analyze_sentiment()
captured = capsys.readouterr()
assert (
"Document sentiment score: 0." in captured.out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See previous about testing for output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

language_sentiment_text.sample_analyze_sentiment()
captured = capsys.readouterr()
assert (
"Document sentiment score: 0." in captured.out
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See previous about testing for API output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

@telpirion telpirion left a comment

Choose a reason for hiding this comment

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

Looks great! Let's merge it when we can.

@telpirion telpirion removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Aug 24, 2023
@telpirion telpirion merged commit e440097 into GoogleCloudPlatform:main Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: language Issues related to the Cloud Natural Language API API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants