Skip to content

Commit

Permalink
Feedback cleanup, tests use protobuf objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Nov 8, 2016
1 parent 0bd9304 commit cc79023
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 34 deletions.
6 changes: 3 additions & 3 deletions docs/speech-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ See: `Single Utterance`_
>>> sample = client.sample(content=stream,
... encoding=speech.Encoding.LINEAR16,
... sample_rate=16000)
>>> response = client.streaming_recognize(sample,
... single_utterance=True)
... alternatives = list(response)
... responses = client.streaming_recognize(sample,
... single_utterance=True)
... alternatives = list(responses)
>>> print(alternatives[0].transcript)
hello
>>> print(alternatives[0].confidence)
Expand Down
6 changes: 4 additions & 2 deletions speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ def streaming_recognize(self, sample, language_code=None,
omitted, only is_final=true result(s) are
returned.
:raises: :class:`EnvironmentError` if gRPC is not enabled and
:class:`ValueError` if stream has closed.
:raises: :class:`ValueError` if sample.content is not a file-like
object. :class:`ValueError` if stream has closed.
:rtype: :class:`~google.cloud.grpc.speech.v1beta1\
.cloud_speech_pb2.StreamingRecognizeResponse`
:returns: ``StreamingRecognizeResponse`` instances.
"""
if getattr(sample.content, 'closed', None) is None:
raise ValueError('Please use file-like object for data stream.')
if sample.content.closed:
raise ValueError('Stream is closed.')

Expand Down
4 changes: 2 additions & 2 deletions speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ def streaming_recognize(self, sample, language_code=None,
interim_results)
for response in responses:
results = getattr(response, 'results', [])
if results or interim_results:
for result in results:
for result in results:
if result.is_final or interim_results:
yield [Alternative.from_pb(alternative)
for alternative in result.alternatives]

Expand Down
4 changes: 2 additions & 2 deletions speech/google/cloud/speech/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Sample(object):
default_encoding = Encoding.FLAC
default_sample_rate = 16000

def __init__(self, content=None, source_uri=None, encoding=None,
sample_rate=None):
def __init__(self, content=None, source_uri=None,
encoding=None, sample_rate=None):

no_source = content is None and source_uri is None
both_source = content is not None and source_uri is not None
Expand Down
36 changes: 36 additions & 0 deletions speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@
import unittest


class TestGAPICSpeechAPI(unittest.TestCase):
SAMPLE_RATE = 16000

def _getTargetClass(self):
from google.cloud.speech._gax import GAPICSpeechAPI

return GAPICSpeechAPI

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_use_bytes_instead_of_file_like_object(self):
from google.cloud import speech
from google.cloud.speech.sample import Sample

credentials = {}
client = speech.Client(credentials=credentials, use_gax=True)
client.connection = _Connection()
client.connection.credentials = credentials

sample = Sample(content=b'', encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)

api = self._makeOne(client)
with self.assertRaises(ValueError):
api.streaming_recognize(sample)
self.assertEqual(client.connection._requested, [])


class TestSpeechGAXMakeRequests(unittest.TestCase):
SAMPLE_RATE = 16000
HINTS = ['hi']
Expand Down Expand Up @@ -137,3 +166,10 @@ def test_stream_requests(self):
self.assertEqual(streaming_request.audio_content, self.AUDIO_CONTENT)
self.assertIsInstance(config_request.streaming_config,
StreamingRecognitionConfig)


class _Connection(object):

def __init__(self, *responses):
self._responses = responses
self._requested = []
88 changes: 63 additions & 25 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,48 @@
import unittest


def _make_result(alternatives=None):
def _make_result(alternatives=()):
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
if alternatives:
return cloud_speech_pb2.SpeechRecognitionResult(
alternatives=[
cloud_speech_pb2.SpeechRecognitionAlternative(
transcript=alternative['transcript'],
confidence=alternative['confidence'],
) for alternative in alternatives
],
)
else:
return cloud_speech_pb2.SpeechRecognitionResult(alternatives=[])

return cloud_speech_pb2.SpeechRecognitionResult(
alternatives=[
cloud_speech_pb2.SpeechRecognitionAlternative(
transcript=alternative['transcript'],
confidence=alternative['confidence'],
) for alternative in alternatives
],
)


def _make_streaming_result(alternatives=(), is_final=True):
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2

return cloud_speech_pb2.StreamingRecognitionResult(
alternatives=[
cloud_speech_pb2.SpeechRecognitionAlternative(
transcript=alternative['transcript'],
confidence=alternative['confidence'],
) for alternative in alternatives
],
is_final=is_final,
)


def _make_streaming_response(*results):
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2

response = cloud_speech_pb2.StreamingRecognizeResponse(
results=results,
)
return response


def _make_sync_response(*results):
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
if results:
response = cloud_speech_pb2.SyncRecognizeResponse(
results=results,
)
else:
response = cloud_speech_pb2.SyncRecognizeResponse()

response = cloud_speech_pb2.SyncRecognizeResponse(
results=results,
)
return response


Expand Down Expand Up @@ -471,10 +490,13 @@ def test_stream_recognize_interim_results(self):
'transcript': 'testing streaming 4 5 6',
'confidence': 0.0123456,
}]
result = _make_result(alternatives)
responses = [_make_sync_response(_make_result([])),
_make_sync_response(result),
_make_sync_response(result)]
first_response = _make_streaming_response(
_make_streaming_result([], is_final=False))
second_response = _make_streaming_response(
_make_streaming_result(alternatives, is_final=False))
last_response = _make_streaming_response(
_make_streaming_result(alternatives, is_final=True))
responses = [first_response, second_response, last_response]

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -523,7 +545,19 @@ def test_stream_recognize(self):
client.connection = _Connection()
client.connection.credentials = credentials

responses = [_make_sync_response()]
alternatives = [{
'transcript': 'testing streaming 1 2 3',
'confidence': 0.9224355,
}, {
'transcript': 'testing streaming 4 5 6',
'confidence': 0.0123456,
}]

first_response = _make_streaming_response(
_make_streaming_result(alternatives=alternatives, is_final=False))
last_response = _make_streaming_response(
_make_streaming_result(alternatives=alternatives, is_final=True))
responses = [first_response, last_response]

channel_args = []
channel_obj = object()
Expand All @@ -547,7 +581,11 @@ def speech_api(channel=None):
sample_rate=self.SAMPLE_RATE)

results = list(client.streaming_recognize(sample))
self.assertEqual(results, [])
self.assertEqual(len(results), 1)
self.assertEqual(results[0][0].transcript,
alternatives[0]['transcript'])
self.assertEqual(results[0][0].confidence,
alternatives[0]['confidence'])

def test_stream_recognize_no_results(self):
from io import BytesIO
Expand All @@ -563,7 +601,7 @@ def test_stream_recognize_no_results(self):
client.connection = _Connection()
client.connection.credentials = credentials

responses = [_make_sync_response()]
responses = [_make_streaming_response()]

channel_args = []
channel_obj = object()
Expand Down

0 comments on commit cc79023

Please sign in to comment.