Repository navigation
π Examples of Generated Samples for Code Review [DO NOT MERGE] #9514
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 all commits
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,90 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright 2019 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", "language_sentiment_text") | ||
|
|
||
| # To install the latest published package dependency, execute the following: | ||
| # pip install google-cloud-language | ||
|
|
||
| # sample-metadata | ||
| # title: Analyzing Sentiment | ||
| # description: Analyzing Sentiment in a String | ||
| # usage: python3 samples/v1/language_sentiment_text.py [--text_content "I am so happy and joyful."] | ||
|
|
||
| # [START language_sentiment_text] | ||
| from google.cloud import language_v1 | ||
| from google.cloud.language_v1 import enums | ||
|
|
||
|
|
||
| def sample_analyze_sentiment(text_content): | ||
| """ | ||
| Analyzing Sentiment in a String | ||
|
|
||
| Args: | ||
|
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. IIRC we generally have not been putting ARGS in python sample code. To quote the Authoring GUIDE:
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. Personally, I'd go one step further and say we can have the function definition for ease-of-testing, but when I see the snippet embedded in the docs, it's more a distraction than anything. I'd love to see the imports and region tags all inside the function definition. |
||
| text_content The text content to analyze | ||
| """ | ||
|
|
||
| client = language_v1.LanguageServiceClient() | ||
|
|
||
| # text_content = 'I am so happy and joyful.' | ||
|
|
||
| # Available types: PLAIN_TEXT, HTML | ||
| type_ = enums.Document.Type.PLAIN_TEXT | ||
|
|
||
| # Optional. If not specified, the language is automatically detected. | ||
| # For list of supported languages: | ||
| # https://cloud.google.com/natural-language/docs/languages | ||
| language = "en" | ||
| document = {"content": text_content, "type": type_, "language": language} | ||
|
|
||
| # Available values: NONE, UTF8, UTF16, UTF32 | ||
| encoding_type = enums.EncodingType.UTF8 | ||
|
|
||
| response = client.analyze_sentiment(document, encoding_type=encoding_type) | ||
| # Get overall sentiment of the input document | ||
| print(u"Document sentiment score: {}".format(response.document_sentiment.score)) | ||
| print( | ||
| u"Document sentiment magnitude: {}".format( | ||
| response.document_sentiment.magnitude | ||
| ) | ||
| ) | ||
| # Get sentiment for all sentences in the document | ||
| for sentence in response.sentences: | ||
| print(u"Sentence text: {}".format(sentence.text.content)) | ||
| print(u"Sentence sentiment score: {}".format(sentence.sentiment.score)) | ||
| print(u"Sentence sentiment magnitude: {}".format(sentence.sentiment.magnitude)) | ||
|
|
||
| # Get the language of the text, which will be the same as | ||
| # the language specified in the request or, if not specified, | ||
| # the automatically-detected language. | ||
| print(u"Language of the text: {}".format(response.language)) | ||
|
|
||
|
|
||
| # [END language_sentiment_text] | ||
|
|
||
|
|
||
| def main(): | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--text_content", type=str, default="I am so happy and joyful.") | ||
|
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. As per the style guide: This is a hill I will die on, I have become very used to authoring Python strings preferring single-quotes over double-quotes and it is a convention both highlighted in our authoring guide as well as in older versions of the Google Python style guide (although they recently have loosened up this to just be consistent).
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. We use At some point, I think we should start using |
||
| args = parser.parse_args() | ||
|
|
||
| sample_analyze_sentiment(args.text_content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||
| # -*- coding: utf-8 -*- | ||||||
| # | ||||||
| # Copyright 2019 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 ("LongRunningPromise", "speech_transcribe_async") | ||||||
|
|
||||||
| # To install the latest published package dependency, execute the following: | ||||||
| # pip install google-cloud-speech | ||||||
|
|
||||||
| # sample-metadata | ||||||
| # title: Transcribe Audio File using Long Running Operation (Local File) (LRO) | ||||||
| # description: Transcribe a long audio file using asynchronous speech recognition | ||||||
| # usage: python3 samples/v1/speech_transcribe_async.py [--local_file_path "resources/brooklyn_bridge.raw"] | ||||||
|
|
||||||
| # [START speech_transcribe_async] | ||||||
| from google.cloud import speech_v1 | ||||||
| from google.cloud.speech_v1 import enums | ||||||
|
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. From the Authoring Guide: But use your best judgement here, I am not too picky on this one.
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. I think enums is a module, so this does match the Authoring Guide. |
||||||
| import io | ||||||
|
|
||||||
|
|
||||||
| def sample_long_running_recognize(local_file_path): | ||||||
| """ | ||||||
| Transcribe a long audio file using asynchronous speech recognition | ||||||
|
|
||||||
| Args: | ||||||
| local_file_path Path to local audio file, e.g. /path/audio.wav | ||||||
|
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. Small nit
Suggested change
This is the docstring style used in the client libraries. |
||||||
| """ | ||||||
|
|
||||||
| client = speech_v1.SpeechClient() | ||||||
|
|
||||||
| # local_file_path = 'resources/brooklyn_bridge.raw' | ||||||
|
|
||||||
| # The language of the supplied audio | ||||||
| language_code = "en-US" | ||||||
|
|
||||||
| # Sample rate in Hertz of the audio data sent | ||||||
| sample_rate_hertz = 16000 | ||||||
|
|
||||||
| # Encoding of audio data sent. This sample sets this explicitly. | ||||||
| # This field is optional for FLAC and WAV audio formats. | ||||||
| encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16 | ||||||
| config = { | ||||||
| "language_code": language_code, | ||||||
| "sample_rate_hertz": sample_rate_hertz, | ||||||
| "encoding": encoding, | ||||||
| } | ||||||
| with io.open(local_file_path, "rb") as f: | ||||||
| content = f.read() | ||||||
| audio = {"content": content} | ||||||
|
|
||||||
| operation = client.long_running_recognize(config, audio) | ||||||
|
|
||||||
| print(u"Waiting for operation to complete...") | ||||||
| response = operation.result() | ||||||
|
|
||||||
| for result in response.results: | ||||||
| # First alternative is the most probable result | ||||||
| alternative = result.alternatives[0] | ||||||
| print(u"Transcript: {}".format(alternative.transcript)) | ||||||
|
|
||||||
|
|
||||||
| # [END speech_transcribe_async] | ||||||
|
|
||||||
|
|
||||||
| def main(): | ||||||
| import argparse | ||||||
|
|
||||||
| parser = argparse.ArgumentParser() | ||||||
| parser.add_argument( | ||||||
| "--local_file_path", type=str, default="resources/brooklyn_bridge.raw" | ||||||
|
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. Will this resource file be added alongside the sample? |
||||||
| ) | ||||||
| args = parser.parse_args() | ||||||
|
|
||||||
| sample_long_running_recognize(args.local_file_path) | ||||||
|
|
||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| 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.
from google.cloud import language_v1-->from google.cloud import language_v1 as languageMakes it a lot easier to perform updates