Skip to content

Commit 4becc0c

Browse files
authored
Merge pull request googleapis#2624 from daspecster/separate-speech-async-json
Move async JSON logic to _JSONSpeechAPI.
2 parents b2662c1 + 4d43d36 commit 4becc0c

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

speech/google/cloud/speech/_gax.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,50 @@ class GAPICSpeechAPI(object):
2727
def __init__(self):
2828
self._gapic_api = SpeechApi()
2929

30+
def async_recognize(self, sample, language_code=None,
31+
max_alternatives=None, profanity_filter=None,
32+
speech_context=None):
33+
"""Asychronous Recognize request to Google Speech API.
34+
35+
.. _async_recognize: https://cloud.google.com/speech/reference/\
36+
rest/v1beta1/speech/asyncrecognize
37+
38+
See `async_recognize`_.
39+
40+
:type sample: :class:`~google.cloud.speech.sample.Sample`
41+
:param sample: Instance of ``Sample`` containing audio information.
42+
43+
:type language_code: str
44+
:param language_code: (Optional) The language of the supplied audio as
45+
BCP-47 language tag. Example: ``'en-GB'``.
46+
If omitted, defaults to ``'en-US'``.
47+
48+
:type max_alternatives: int
49+
:param max_alternatives: (Optional) Maximum number of recognition
50+
hypotheses to be returned. The server may
51+
return fewer than maxAlternatives.
52+
Valid values are 0-30. A value of 0 or 1
53+
will return a maximum of 1. Defaults to 1
54+
55+
:type profanity_filter: bool
56+
:param profanity_filter: If True, the server will attempt to filter
57+
out profanities, replacing all but the
58+
initial character in each filtered word with
59+
asterisks, e.g. ``'f***'``. If False or
60+
omitted, profanities won't be filtered out.
61+
62+
:type speech_context: list
63+
:param speech_context: A list of strings (max 50) containing words and
64+
phrases "hints" so that the speech recognition
65+
is more likely to recognize them. This can be
66+
used to improve the accuracy for specific words
67+
and phrases. This can also be used to add new
68+
words to the vocabulary of the recognizer.
69+
70+
:raises NotImplementedError: Always.
71+
"""
72+
raise NotImplementedError
73+
3074
def sync_recognize(self, sample, language_code=None, max_alternatives=None,
3175
profanity_filter=None, speech_context=None):
3276
"""Synchronous Speech Recognition.

speech/google/cloud/speech/client.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,10 @@ def async_recognize(self, sample, language_code=None,
117117
if sample.encoding is not Encoding.LINEAR16:
118118
raise ValueError('Only LINEAR16 encoding is supported by '
119119
'asynchronous speech requests.')
120-
121-
data = _build_request_data(sample, language_code, max_alternatives,
120+
api = self.speech_api
121+
return api.async_recognize(sample, language_code, max_alternatives,
122122
profanity_filter, speech_context)
123123

124-
api_response = self.connection.api_request(
125-
method='POST', path='speech:asyncrecognize', data=data)
126-
127-
return Operation.from_api_repr(self, api_response)
128-
129124
@staticmethod
130125
def sample(content=None, source_uri=None, encoding=None,
131126
sample_rate=None):
@@ -235,6 +230,56 @@ def __init__(self, client):
235230
self._client = client
236231
self._connection = client.connection
237232

233+
def async_recognize(self, sample, language_code=None,
234+
max_alternatives=None, profanity_filter=None,
235+
speech_context=None):
236+
"""Asychronous Recognize request to Google Speech API.
237+
238+
.. _async_recognize: https://cloud.google.com/speech/reference/\
239+
rest/v1beta1/speech/asyncrecognize
240+
241+
See `async_recognize`_.
242+
243+
:type sample: :class:`~google.cloud.speech.sample.Sample`
244+
:param sample: Instance of ``Sample`` containing audio information.
245+
246+
:type language_code: str
247+
:param language_code: (Optional) The language of the supplied audio as
248+
BCP-47 language tag. Example: ``'en-GB'``.
249+
If omitted, defaults to ``'en-US'``.
250+
251+
:type max_alternatives: int
252+
:param max_alternatives: (Optional) Maximum number of recognition
253+
hypotheses to be returned. The server may
254+
return fewer than maxAlternatives.
255+
Valid values are 0-30. A value of 0 or 1
256+
will return a maximum of 1. Defaults to 1
257+
258+
:type profanity_filter: bool
259+
:param profanity_filter: If True, the server will attempt to filter
260+
out profanities, replacing all but the
261+
initial character in each filtered word with
262+
asterisks, e.g. ``'f***'``. If False or
263+
omitted, profanities won't be filtered out.
264+
265+
:type speech_context: list
266+
:param speech_context: A list of strings (max 50) containing words and
267+
phrases "hints" so that the speech recognition
268+
is more likely to recognize them. This can be
269+
used to improve the accuracy for specific words
270+
and phrases. This can also be used to add new
271+
words to the vocabulary of the recognizer.
272+
273+
:rtype: `~google.cloud.speech.operation.Operation`
274+
:returns: ``Operation`` for asynchronous request to Google Speech API.
275+
"""
276+
data = _build_request_data(sample, language_code, max_alternatives,
277+
profanity_filter, speech_context)
278+
api_response = self._connection.api_request(
279+
method='POST', path='speech:asyncrecognize', data=data)
280+
281+
return Operation.from_api_repr(self, api_response)
282+
238283
def sync_recognize(self, sample, language_code=None, max_alternatives=None,
239284
profanity_filter=None, speech_context=None):
240285
"""Synchronous Speech Recognition.

speech/unit_tests/test_client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_async_supported_encodings(self):
245245
with self.assertRaises(ValueError):
246246
client.async_recognize(sample)
247247

248-
def test_async_recognize(self):
248+
def test_async_recognize_no_gax(self):
249249
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
250250
from google.cloud import speech
251251
from google.cloud.speech.operation import Operation
@@ -254,7 +254,7 @@ def test_async_recognize(self):
254254
RETURNED = ASYNC_RECOGNIZE_RESPONSE
255255

256256
credentials = _Credentials()
257-
client = self._makeOne(credentials=credentials)
257+
client = self._makeOne(credentials=credentials, use_gax=False)
258258
client.connection = _Connection(RETURNED)
259259

260260
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
@@ -265,6 +265,22 @@ def test_async_recognize(self):
265265
self.assertFalse(operation.complete)
266266
self.assertIsNone(operation.metadata)
267267

268+
def test_async_recognize_with_gax(self):
269+
from google.cloud.speech import _gax as MUT
270+
from google.cloud._testing import _Monkey
271+
from google.cloud import speech
272+
273+
credentials = _Credentials()
274+
client = self._makeOne(credentials=credentials)
275+
client.connection = _Connection()
276+
277+
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
278+
encoding=speech.Encoding.LINEAR16,
279+
sample_rate=self.SAMPLE_RATE)
280+
with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI):
281+
with self.assertRaises(NotImplementedError):
282+
client.async_recognize(sample)
283+
268284
def test_speech_api_with_gax(self):
269285
from google.cloud.speech import _gax as MUT
270286
from google.cloud._testing import _Monkey

0 commit comments

Comments
 (0)