From 3f3fbe416aec950f74b39e9c8d8849f8b0c790fe Mon Sep 17 00:00:00 2001 From: chemelnucfin Date: Thu, 9 Nov 2017 13:00:51 -0800 Subject: [PATCH] Speech: long_run_recognize document update (#4281) * Speech: Closes #4275 long_run_recognize document update * Review Changes * review changes * more clear expression of transcription * more clear joining of transcription --- docs/speech/index.rst | 11 ++--------- speech/tests/system.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/docs/speech/index.rst b/docs/speech/index.rst index 8cc564f5a196..0328b7f02cd8 100644 --- a/docs/speech/index.rst +++ b/docs/speech/index.rst @@ -42,7 +42,6 @@ See: `Speech Asynchronous Recognize`_ .. code-block:: python - >>> import time >>> from google.cloud import speech >>> client = speech.SpeechClient() >>> operation = client.long_running_recognize( @@ -55,14 +54,8 @@ See: `Speech Asynchronous Recognize`_ ... sample_rate_hertz=44100, ... ), ... ) - >>> retry_count = 100 - >>> while retry_count > 0 and not operation.complete: - ... retry_count -= 1 - ... time.sleep(10) - ... operation.poll() # API call - >>> operation.complete - True - >>> for result in operation.results: + >>> op_result = operation.result() + >>> for result in op_result.results: ... for alternative in result.alternatives: ... print('=' * 20) ... print(alternative.transcript) diff --git a/speech/tests/system.py b/speech/tests/system.py index 88da188cb8c6..460712a7a519 100644 --- a/speech/tests/system.py +++ b/speech/tests/system.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import io import os import time import unittest +import wave import six @@ -245,3 +247,37 @@ def test_stream_recognize_single_utterance(self): for results in self._make_streaming_request( file_obj, single_utterance=False): self._check_results(results.alternatives) + + def test_long_running_recognize(self): + content = None + repetitions = 5 + with open(AUDIO_FILE, 'rb') as in_stream: + in_wavfile = wave.open(in_stream) + params = in_wavfile.getparams() + frames = in_wavfile.readframes(in_wavfile.getnframes()) + + with io.BytesIO() as out_stream: + out_wavfile = wave.open(out_stream, 'w') + out_wavfile.setparams(params) + for _ in range(repetitions): + out_wavfile.writeframes(frames) + content = out_stream.getvalue() + + client = speech.SpeechClient() + + operation = client.long_running_recognize( + config=speech.types.RecognitionConfig( + encoding='LINEAR16', + language_code='en-US', + ), + audio=speech.types.RecognitionAudio(content=content) + ) + + op_result = operation.result() + wav_transcription = 'hello thank you for using google cloud platform' + expected = ' '.join([wav_transcription] * repetitions) + self.assertGreater(len(op_result.results), 0) + for result in op_result.results: + self.assertGreater(len(result.alternatives), 0) + for alternative in result.alternatives: + self.assertEqual(alternative.transcript.lower(), expected)