Skip to content

Commit

Permalink
Merge pull request #2666 from daspecster/rename-transcript-to-alterna…
Browse files Browse the repository at this point in the history
…tive

Rename Transcript to Alternative.
  • Loading branch information
daspecster authored Nov 2, 2016
2 parents 4dc9fa6 + feb4f9e commit 5b4689f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
StreamingRecognizeRequest)


from google.cloud.speech.transcript import Transcript
from google.cloud.speech.alternative import Alternative


class GAPICSpeechAPI(object):
Expand Down Expand Up @@ -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.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,41 @@
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)

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 = {
'transcript': 'testing 1 2 3',
}

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
Expand All @@ -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)
13 changes: 7 additions & 6 deletions packages/google-cloud-python-speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -122,18 +123,18 @@ 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)

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

Expand Down
10 changes: 5 additions & 5 deletions packages/google-cloud-python-speech/unit_tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down

0 comments on commit 5b4689f

Please sign in to comment.