Skip to content

Commit 3f3fbe4

Browse files
authored
Speech: long_run_recognize document update (googleapis#4281)
* Speech: Closes googleapis#4275 long_run_recognize document update * Review Changes * review changes * more clear expression of transcription * more clear joining of transcription
1 parent 2a3793d commit 3f3fbe4

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

docs/speech/index.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ See: `Speech Asynchronous Recognize`_
4242

4343
.. code-block:: python
4444
45-
>>> import time
4645
>>> from google.cloud import speech
4746
>>> client = speech.SpeechClient()
4847
>>> operation = client.long_running_recognize(
@@ -55,14 +54,8 @@ See: `Speech Asynchronous Recognize`_
5554
... sample_rate_hertz=44100,
5655
... ),
5756
... )
58-
>>> retry_count = 100
59-
>>> while retry_count > 0 and not operation.complete:
60-
... retry_count -= 1
61-
... time.sleep(10)
62-
... operation.poll() # API call
63-
>>> operation.complete
64-
True
65-
>>> for result in operation.results:
57+
>>> op_result = operation.result()
58+
>>> for result in op_result.results:
6659
... for alternative in result.alternatives:
6760
... print('=' * 20)
6861
... print(alternative.transcript)

speech/tests/system.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import io
1516
import os
1617
import time
1718
import unittest
19+
import wave
1820

1921
import six
2022

@@ -245,3 +247,37 @@ def test_stream_recognize_single_utterance(self):
245247
for results in self._make_streaming_request(
246248
file_obj, single_utterance=False):
247249
self._check_results(results.alternatives)
250+
251+
def test_long_running_recognize(self):
252+
content = None
253+
repetitions = 5
254+
with open(AUDIO_FILE, 'rb') as in_stream:
255+
in_wavfile = wave.open(in_stream)
256+
params = in_wavfile.getparams()
257+
frames = in_wavfile.readframes(in_wavfile.getnframes())
258+
259+
with io.BytesIO() as out_stream:
260+
out_wavfile = wave.open(out_stream, 'w')
261+
out_wavfile.setparams(params)
262+
for _ in range(repetitions):
263+
out_wavfile.writeframes(frames)
264+
content = out_stream.getvalue()
265+
266+
client = speech.SpeechClient()
267+
268+
operation = client.long_running_recognize(
269+
config=speech.types.RecognitionConfig(
270+
encoding='LINEAR16',
271+
language_code='en-US',
272+
),
273+
audio=speech.types.RecognitionAudio(content=content)
274+
)
275+
276+
op_result = operation.result()
277+
wav_transcription = 'hello thank you for using google cloud platform'
278+
expected = ' '.join([wav_transcription] * repetitions)
279+
self.assertGreater(len(op_result.results), 0)
280+
for result in op_result.results:
281+
self.assertGreater(len(result.alternatives), 0)
282+
for alternative in result.alternatives:
283+
self.assertEqual(alternative.transcript.lower(), expected)

0 commit comments

Comments
 (0)