diff --git a/docs/index.rst b/docs/index.rst index e7f331b04cf8..c1746b988511 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -178,7 +178,7 @@ speech-encoding speech-operation speech-sample - speech-transcript + speech-alternative .. toctree:: :maxdepth: 0 diff --git a/docs/speech-alternative.rst b/docs/speech-alternative.rst new file mode 100644 index 000000000000..7c287b8dfa44 --- /dev/null +++ b/docs/speech-alternative.rst @@ -0,0 +1,7 @@ +Speech Alternative +================== + +.. automodule:: google.cloud.speech.alternative + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/speech-transcript.rst b/docs/speech-transcript.rst deleted file mode 100644 index f71f72bd2645..000000000000 --- a/docs/speech-transcript.rst +++ /dev/null @@ -1,7 +0,0 @@ -Speech Transcript -================= - -.. automodule:: google.cloud.speech.transcript - :members: - :undoc-members: - :show-inheritance: diff --git a/speech/google/cloud/speech/__init__.py b/speech/google/cloud/speech/__init__.py index dd054830659a..2d0ba94b738c 100644 --- a/speech/google/cloud/speech/__init__.py +++ b/speech/google/cloud/speech/__init__.py @@ -14,8 +14,8 @@ """Google Cloud Speech API wrapper.""" +from google.cloud.speech.alternative import Alternative from google.cloud.speech.client import Client from google.cloud.speech.connection import Connection from google.cloud.speech.encoding import Encoding from google.cloud.speech.operation import Operation -from google.cloud.speech.transcript import Transcript diff --git a/speech/google/cloud/speech/_gax.py b/speech/google/cloud/speech/_gax.py index 0019e3e2bea2..9437a806b1ce 100644 --- a/speech/google/cloud/speech/_gax.py +++ b/speech/google/cloud/speech/_gax.py @@ -24,7 +24,7 @@ StreamingRecognizeRequest) -from google.cloud.speech.transcript import Transcript +from google.cloud.speech.alternative import Alternative class GAPICSpeechAPI(object): @@ -139,7 +139,7 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None, if len(api_response.results) == 1: results = api_response.results.pop() alternatives = results.alternatives - return [Transcript.from_pb(alternative) + return [Alternative.from_pb(alternative) for alternative in alternatives] else: raise ValueError('More than one result or none returned from API.') diff --git a/speech/google/cloud/speech/transcript.py b/speech/google/cloud/speech/alternative.py similarity index 63% rename from speech/google/cloud/speech/transcript.py rename to speech/google/cloud/speech/alternative.py index 0ac22e6d296f..af71788a1923 100644 --- a/speech/google/cloud/speech/transcript.py +++ b/speech/google/cloud/speech/alternative.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Transcript representation for Google Speech API""" +"""Representation of Speech Alternative for the Google Speech API.""" -class Transcript(object): - """Representation of Speech Transcripts. +class Alternative(object): + """Representation of Speech Alternative. :type transcript: str :param transcript: String of transcribed data. @@ -29,33 +29,33 @@ def __init__(self, transcript, confidence): self._confidence = confidence @classmethod - def from_api_repr(cls, transcript): - """Factory: construct ``Transcript`` from JSON response. + def from_api_repr(cls, alternative): + """Factory: construct ``Alternative`` from JSON response. - :type transcript: dict - :param transcript: Dictionary response from the REST API. + :type alternative: dict + :param alternative: Dictionary response from the REST API. - :rtype: :class:`Transcript` - :returns: Instance of ``Transcript``. + :rtype: :class:`Alternative` + :returns: Instance of ``Alternative``. """ - return cls(transcript['transcript'], transcript.get('confidence')) + return cls(alternative['transcript'], alternative.get('confidence')) @classmethod - def from_pb(cls, transcript): - """Factory: construct ``Transcript`` from protobuf response. + def from_pb(cls, alternative): + """Factory: construct ``Alternative`` from protobuf response. - :type transcript: + :type alternative: :class:`google.cloud.speech.v1beta1.SpeechRecognitionAlternative` - :param transcript: Instance of ``SpeechRecognitionAlternative`` + :param alternative: Instance of ``SpeechRecognitionAlternative`` from protobuf. - :rtype: :class:`Transcript` - :returns: Instance of ``Transcript``. + :rtype: :class:`Alternative` + :returns: Instance of ``Alternative``. """ - confidence = transcript.confidence + confidence = alternative.confidence if confidence == 0.0: # In the protobof 0.0 means unset. confidence = None - return cls(transcript.transcript, confidence) + return cls(alternative.transcript, confidence) @property def transcript(self): diff --git a/speech/google/cloud/speech/client.py b/speech/google/cloud/speech/client.py index 8355e31f8aee..7959a1c05e4c 100644 --- a/speech/google/cloud/speech/client.py +++ b/speech/google/cloud/speech/client.py @@ -26,7 +26,7 @@ from google.cloud.speech.encoding import Encoding from google.cloud.speech.operation import Operation from google.cloud.speech.sample import Sample -from google.cloud.speech.transcript import Transcript +from google.cloud.speech.alternative import Alternative try: from google.cloud.speech._gax import GAPICSpeechAPI @@ -349,7 +349,7 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None, if len(api_response['results']) == 1: result = api_response['results'][0] - return [Transcript.from_api_repr(alternative) + return [Alternative.from_api_repr(alternative) for alternative in result['alternatives']] else: raise ValueError('More than one result or none returned from API.') diff --git a/speech/google/cloud/speech/operation.py b/speech/google/cloud/speech/operation.py index 7a91366bf5e0..f56f0602fa1c 100644 --- a/speech/google/cloud/speech/operation.py +++ b/speech/google/cloud/speech/operation.py @@ -17,7 +17,7 @@ from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 from google.cloud import operation -from google.cloud.speech.transcript import Transcript +from google.cloud.speech.alternative import Alternative operation.register_type(cloud_speech_pb2.AsyncRecognizeMetadata) @@ -64,5 +64,5 @@ def _update_state(self, operation_pb): pb_results) result = pb_results[0] - self.results = [Transcript.from_pb(alternative) + self.results = [Alternative.from_pb(alternative) for alternative in result.alternatives] diff --git a/speech/unit_tests/test_transcript.py b/speech/unit_tests/test_alternative.py similarity index 63% rename from speech/unit_tests/test_transcript.py rename to speech/unit_tests/test_alternative.py index a049eac6e815..ac6d9f56fcfb 100644 --- a/speech/unit_tests/test_transcript.py +++ b/speech/unit_tests/test_alternative.py @@ -15,11 +15,11 @@ import unittest -class TestTranscript(unittest.TestCase): +class TestAlternative(unittest.TestCase): def _getTargetClass(self): - from google.cloud.speech.transcript import Transcript - return Transcript + from google.cloud.speech.alternative import Alternative + return Alternative def _makeOne(self, *args, **kwargs): return self._getTargetClass()(*args, **kwargs) @@ -27,19 +27,19 @@ def _makeOne(self, *args, **kwargs): def test_constructor(self): text = 'hello goodbye upstairs' confidence = 0.5546875 - transcript = self._makeOne(text, confidence) - self.assertEqual(transcript._transcript, text) - self.assertEqual(transcript._confidence, confidence) + alternative = self._makeOne(text, confidence) + self.assertEqual(alternative._transcript, text) + self.assertEqual(alternative._confidence, confidence) def test_transcript_property(self): text = 'is this thing on?' - transcript = self._makeOne(text, None) - self.assertEqual(transcript.transcript, text) + alternative = self._makeOne(text, None) + self.assertEqual(alternative.transcript, text) def test_confidence_property(self): confidence = 0.412109375 - transcript = self._makeOne(None, confidence) - self.assertEqual(transcript.confidence, confidence) + alternative = self._makeOne(None, confidence) + self.assertEqual(alternative.confidence, confidence) def test_from_api_repr_with_no_confidence(self): data = { @@ -47,9 +47,9 @@ def test_from_api_repr_with_no_confidence(self): } klass = self._getTargetClass() - transcript = klass.from_api_repr(data) - self.assertEqual(transcript.transcript, data['transcript']) - self.assertIsNone(transcript.confidence) + alternative = klass.from_api_repr(data) + self.assertEqual(alternative.transcript, data['transcript']) + self.assertIsNone(alternative.confidence) def test_from_pb_with_no_confidence(self): from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 @@ -60,6 +60,6 @@ def test_from_pb_with_no_confidence(self): self.assertEqual(pb_value.confidence, 0.0) klass = self._getTargetClass() - transcript = klass.from_pb(pb_value) - self.assertEqual(transcript.transcript, text) - self.assertIsNone(transcript.confidence) + alternative = klass.from_pb(pb_value) + self.assertEqual(alternative.transcript, text) + self.assertIsNone(alternative.confidence) diff --git a/speech/unit_tests/test_client.py b/speech/unit_tests/test_client.py index 0f9b8e1ed492..f26313940fd4 100644 --- a/speech/unit_tests/test_client.py +++ b/speech/unit_tests/test_client.py @@ -75,8 +75,9 @@ def test_sync_recognize_content_with_optional_params_no_gax(self): from google.cloud._testing import _Monkey from google.cloud.speech import client as MUT from google.cloud import speech + from google.cloud.speech.alternative import Alternative from google.cloud.speech.sample import Sample - from google.cloud.speech.transcript import Transcript + from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE _AUDIO_CONTENT = _to_bytes(self.AUDIO_CONTENT) @@ -122,9 +123,9 @@ def test_sync_recognize_content_with_optional_params_no_gax(self): self.assertEqual(req['path'], 'speech:syncrecognize') alternative = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0] - expected = Transcript.from_api_repr(alternative) + expected = Alternative.from_api_repr(alternative) self.assertEqual(len(response), 1) - self.assertIsInstance(response[0], Transcript) + self.assertIsInstance(response[0], Alternative) self.assertEqual(response[0].transcript, expected.transcript) self.assertEqual(response[0].confidence, expected.confidence) @@ -132,8 +133,8 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self): from google.cloud._testing import _Monkey from google.cloud.speech import client as MUT from google.cloud import speech + from google.cloud.speech.alternative import Alternative from google.cloud.speech.sample import Sample - from google.cloud.speech.transcript import Transcript from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE RETURNED = SYNC_RECOGNIZE_RESPONSE @@ -164,10 +165,10 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self): self.assertEqual(req['method'], 'POST') self.assertEqual(req['path'], 'speech:syncrecognize') - expected = Transcript.from_api_repr( + expected = Alternative.from_api_repr( SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0]) self.assertEqual(len(response), 1) - self.assertIsInstance(response[0], Transcript) + self.assertIsInstance(response[0], Alternative) self.assertEqual(response[0].transcript, expected.transcript) self.assertEqual(response[0].confidence, expected.confidence) diff --git a/speech/unit_tests/test_operation.py b/speech/unit_tests/test_operation.py index f067b47838e9..dd0dbd4a1d72 100644 --- a/speech/unit_tests/test_operation.py +++ b/speech/unit_tests/test_operation.py @@ -83,7 +83,7 @@ def test__update_state_no_response(self): self.assertIsNone(operation.results) def test__update_state_with_response(self): - from google.cloud.speech.transcript import Transcript + from google.cloud.speech.alternative import Alternative client = object() operation = self._makeOne( @@ -97,10 +97,10 @@ def test__update_state_with_response(self): self.assertIsNotNone(operation.response) self.assertEqual(len(operation.results), 1) - transcript = operation.results[0] - self.assertIsInstance(transcript, Transcript) - self.assertEqual(transcript.transcript, text) - self.assertEqual(transcript.confidence, confidence) + alternative = operation.results[0] + self.assertIsInstance(alternative, Alternative) + self.assertEqual(alternative.transcript, text) + self.assertEqual(alternative.confidence, confidence) def test__update_state_bad_response(self): client = object() diff --git a/system_tests/speech.py b/system_tests/speech.py index ccbde3343ef5..d6f63f8dc3d1 100644 --- a/system_tests/speech.py +++ b/system_tests/speech.py @@ -18,7 +18,7 @@ from google.cloud import exceptions from google.cloud import speech from google.cloud import storage -from google.cloud.speech.transcript import Transcript +from google.cloud.speech.alternative import Alternative from system_test_utils import unique_resource_id from retry import RetryErrors @@ -121,13 +121,13 @@ def _check_results(self, results, num_results=1): self.assertEqual(len(results), num_results) top_result = results[0] - self.assertIsInstance(top_result, Transcript) + self.assertIsInstance(top_result, Alternative) self.assertEqual(top_result.transcript, 'hello ' + self.ASSERT_TEXT) self.assertGreater(top_result.confidence, 0.90) if num_results == 2: second_alternative = results[1] - self.assertIsInstance(second_alternative, Transcript) + self.assertIsInstance(second_alternative, Alternative) self.assertEqual(second_alternative.transcript, self.ASSERT_TEXT) self.assertIsNone(second_alternative.confidence)