Skip to content

Use Transcript object in sync and async. #2613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/speech-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ Great Britian.
... max_alternatives=2)
>>> for alternative in alternatives:
... print('=' * 20)
... print('transcript: ' + alternative['transcript'])
... print('confidence: ' + alternative['confidence'])
... print('transcript: ' + alternative.transcript)
... print('confidence: ' + alternative.confidence)
====================
transcript: Hello, this is a test
confidence: 0.81
Expand All @@ -114,8 +114,8 @@ Example of using the profanity filter.
... profanity_filter=True)
>>> for alternative in alternatives:
... print('=' * 20)
... print('transcript: ' + alternative['transcript'])
... print('confidence: ' + alternative['confidence'])
... print('transcript: ' + alternative.transcript)
... print('confidence: ' + alternative.confidence)
====================
transcript: Hello, this is a f****** test
confidence: 0.81
Expand All @@ -136,8 +136,8 @@ words to the vocabulary of the recognizer.
... speech_context=hints)
>>> for alternative in alternatives:
... print('=' * 20)
... print('transcript: ' + alternative['transcript'])
... print('confidence: ' + alternative['confidence'])
... print('transcript: ' + alternative.transcript)
... print('confidence: ' + alternative.confidence)
====================
transcript: Hello, this is a test
confidence: 0.81
Expand Down
1 change: 1 addition & 0 deletions speech/google/cloud/speech/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
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.transcript import Transcript
5 changes: 4 additions & 1 deletion speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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

This comment was marked as spam.



class Client(client_module.Client):
Expand Down Expand Up @@ -195,7 +196,9 @@ def sync_recognize(self, sample, language_code=None,
method='POST', path='speech:syncrecognize', data=data)

if len(api_response['results']) == 1:
return api_response['results'][0]['alternatives']
result = api_response['results'][0]
return [Transcript.from_api_repr(alternative)
for alternative in result['alternatives']]
else:
raise ValueError('result in api should have length 1')

Expand Down
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _update(self, response):
results = []
if raw_results:
for result in raw_results[0]['alternatives']:
results.append(Transcript(result))
results.append(Transcript.from_api_repr(result))
if metadata:
self._metadata = Metadata.from_api_repr(metadata)

Expand Down
27 changes: 21 additions & 6 deletions speech/google/cloud/speech/transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@


class Transcript(object):
"""Representation of Speech Transcripts
"""Representation of Speech Transcripts.

:type result: dict
:param result: Dictionary of transcript and confidence of recognition.
:type transcript: str
:param transcript: String of transcribed data.

:type confidence: float
:param confidence: The confidence estimate between 0.0 and 1.0.
"""
def __init__(self, result):
self._transcript = result.get('transcript')
self._confidence = result.get('confidence')
def __init__(self, transcript, confidence):
self._transcript = transcript
self._confidence = confidence

@classmethod
def from_api_repr(cls, transcript):
"""Factory: construct ``Transcript`` from JSON response.

:type transcript: dict
:param transcript: Dictionary response from the REST API.

:rtype: :class:`~Transcript`
:returns: Instance of ``Transcript``.
"""
return cls(transcript['transcript'], transcript['confidence'])

This comment was marked as spam.

This comment was marked as spam.


@property
def transcript(self):
Expand Down
19 changes: 15 additions & 4 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def test_sync_recognize_content_with_optional_parameters(self):

from google.cloud import speech
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)
_B64_AUDIO_CONTENT = _bytes_to_unicode(b64encode(_AUDIO_CONTENT))
RETURNED = SYNC_RECOGNIZE_RESPONSE
Expand Down Expand Up @@ -109,12 +111,17 @@ def test_sync_recognize_content_with_optional_parameters(self):
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], 'speech:syncrecognize')

expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives']
self.assertEqual(response, expected)
alternative = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0]
expected = Transcript.from_api_repr(alternative)
self.assertEqual(len(response), 1)
self.assertIsInstance(response[0], Transcript)

This comment was marked as spam.

self.assertEqual(response[0].transcript, expected.transcript)
self.assertEqual(response[0].confidence, expected.confidence)

def test_sync_recognize_source_uri_without_optional_parameters(self):
from google.cloud import speech
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 @@ -144,8 +151,12 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], 'speech:syncrecognize')

expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives']
self.assertEqual(response, expected)
expected = Transcript.from_api_repr(
SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0])

This comment was marked as spam.

self.assertEqual(len(response), 1)
self.assertIsInstance(response[0], Transcript)
self.assertEqual(response[0].transcript, expected.transcript)
self.assertEqual(response[0].confidence, expected.confidence)

def test_sync_recognize_with_empty_results(self):
from google.cloud import speech
Expand Down
3 changes: 2 additions & 1 deletion speech/unit_tests/test_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def _makeOne(self, *args, **kwargs):
def test_ctor(self):
from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE as DATA
TRANSCRIPT_DATA = DATA['response']['results'][0]['alternatives'][0]
transcript = self._makeOne(TRANSCRIPT_DATA)
transcript = self._makeOne(TRANSCRIPT_DATA['transcript'],
TRANSCRIPT_DATA['confidence'])
self.assertEqual('how old is the Brooklyn Bridge',
transcript.transcript)
self.assertEqual(0.98267895, transcript.confidence)