Skip to content

Commit

Permalink
Speech: long_run_recognize document update (#4281)
Browse files Browse the repository at this point in the history
* Speech: Closes #4275 long_run_recognize document update

* Review Changes

* review changes

* more clear expression of transcription

* more clear joining of transcription
  • Loading branch information
chemelnucfin authored Nov 9, 2017
1 parent 2a3793d commit 3f3fbe4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
11 changes: 2 additions & 9 deletions docs/speech/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions speech/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 3f3fbe4

Please sign in to comment.