Skip to content
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

Make Encoding accessible from speech.Encoding. #2597

Merged
merged 3 commits into from
Oct 24, 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
39 changes: 18 additions & 21 deletions docs/speech-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ See: `Speech Asynchronous Recognize`_

>>> import time
>>> from google.cloud import speech
>>> from google.cloud.speech.encoding import Encoding
>>> client = speech.Client()
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
... encoding=Encoding.LINEAR16,
... encoding=speech.Encoding.LINEAR16,
... sample_rate=44100)
>>> operation = client.async_recognize(sample, max_alternatives=2)
>>> retry_count = 100
Expand Down Expand Up @@ -82,35 +81,34 @@ Great Britian.
.. code-block:: python

>>> from google.cloud import speech
>>> from google.cloud.speech.encoding import Encoding
>>> client = speech.Client()
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
... encoding=Encoding.FLAC,
... encoding=speech.Encoding.FLAC,
... sample_rate=44100)
>>> operation = client.async_recognize(sample, max_alternatives=2)
>>> alternatives = client.sync_recognize(
... 'FLAC', 16000, source_uri='gs://my-bucket/recording.flac',
... language_code='en-GB', max_alternatives=2)
>>> for alternative in alternatives:
... print('=' * 20)
... print('transcript: ' + alternative['transcript'])
... print('confidence: ' + alternative['confidence'])
====================
transcript: Hello, this is a test
confidence: 0.81
====================
transcript: Hello, this is one test
confidence: 0
>>> alternatives = client.sync_recognize(
... speech.Encoding.FLAC, 16000,
... source_uri='gs://my-bucket/recording.flac', language_code='en-GB',
... max_alternatives=2)
>>> for alternative in alternatives:
... print('=' * 20)
... print('transcript: ' + alternative['transcript'])
... print('confidence: ' + alternative['confidence'])
====================
transcript: Hello, this is a test
confidence: 0.81
====================
transcript: Hello, this is one test
confidence: 0

Example of using the profanity filter.

.. code-block:: python

>>> from google.cloud import speech
>>> from google.cloud.speech.encoding import Encoding
>>> client = speech.Client()
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
... encoding=Encoding.FLAC,
... encoding=speech.Encoding.FLAC,
... sample_rate=44100)
>>> alternatives = client.sync_recognize(sample, max_alternatives=1,
... profanity_filter=True)
Expand All @@ -129,10 +127,9 @@ words to the vocabulary of the recognizer.
.. code-block:: python

>>> from google.cloud import speech
>>> from google.cloud.speech.encoding import Encoding
>>> client = speech.Client()
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
... encoding=Encoding.FLAC,
... encoding=speech.Encoding.FLAC,
... sample_rate=44100)
>>> hints = ['hi', 'good afternoon']
>>> alternatives = client.sync_recognize(sample, max_alternatives=2,
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 @@ -16,3 +16,4 @@

from google.cloud.speech.client import Client
from google.cloud.speech.connection import Connection
from google.cloud.speech.encoding import Encoding
30 changes: 15 additions & 15 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,33 @@ def test_ctor(self):
self.assertTrue(client.connection.http is http)

def test_create_sample_from_client(self):
from google.cloud.speech.encoding import Encoding
from google.cloud import speech
from google.cloud.speech.sample import Sample

credentials = _Credentials()
client = self._makeOne(credentials=credentials)

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=Encoding.FLAC,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
self.assertIsInstance(sample, Sample)
self.assertEqual(sample.source_uri, self.AUDIO_SOURCE_URI)
self.assertEqual(sample.sample_rate, self.SAMPLE_RATE)
self.assertEqual(sample.encoding, Encoding.FLAC)
self.assertEqual(sample.encoding, speech.Encoding.FLAC)

content_sample = client.sample(content=self.AUDIO_CONTENT,
encoding=Encoding.FLAC,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
self.assertEqual(content_sample.content, self.AUDIO_CONTENT)
self.assertEqual(content_sample.sample_rate, self.SAMPLE_RATE)
self.assertEqual(content_sample.encoding, Encoding.FLAC)
self.assertEqual(content_sample.encoding, speech.Encoding.FLAC)

def test_sync_recognize_content_with_optional_parameters(self):
from base64 import b64encode
from google.cloud._helpers import _to_bytes
from google.cloud._helpers import _bytes_to_unicode

from google.cloud.speech.encoding import Encoding
from google.cloud import speech
from google.cloud.speech.sample import Sample
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
_AUDIO_CONTENT = _to_bytes(self.AUDIO_CONTENT)
Expand All @@ -92,7 +92,7 @@ def test_sync_recognize_content_with_optional_parameters(self):
client = self._makeOne(credentials=credentials)
client.connection = _Connection(RETURNED)

encoding = Encoding.FLAC
encoding = speech.Encoding.FLAC

sample = Sample(content=self.AUDIO_CONTENT, encoding=encoding,
sample_rate=self.SAMPLE_RATE)
Expand All @@ -113,7 +113,7 @@ def test_sync_recognize_content_with_optional_parameters(self):
self.assertEqual(response, expected)

def test_sync_recognize_source_uri_without_optional_parameters(self):
from google.cloud.speech.encoding import Encoding
from google.cloud import speech
from google.cloud.speech.sample import Sample
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE

Expand All @@ -131,7 +131,7 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
client = self._makeOne(credentials=credentials)
client.connection = _Connection(RETURNED)

encoding = Encoding.FLAC
encoding = speech.Encoding.FLAC

sample = Sample(source_uri=self.AUDIO_SOURCE_URI, encoding=encoding,
sample_rate=self.SAMPLE_RATE)
Expand All @@ -148,7 +148,7 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
self.assertEqual(response, expected)

def test_sync_recognize_with_empty_results(self):
from google.cloud.speech.encoding import Encoding
from google.cloud import speech
from google.cloud.speech.sample import Sample
from unit_tests._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE

Expand All @@ -158,27 +158,27 @@ def test_sync_recognize_with_empty_results(self):

with self.assertRaises(ValueError):
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=Encoding.FLAC,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
client.sync_recognize(sample)

def test_async_supported_encodings(self):
from google.cloud.speech.encoding import Encoding
from google.cloud import speech
from google.cloud.speech.sample import Sample

credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client.connection = _Connection({})

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=Encoding.FLAC,
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
with self.assertRaises(ValueError):
client.async_recognize(sample)

def test_async_recognize(self):
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
from google.cloud.speech.encoding import Encoding
from google.cloud import speech
from google.cloud.speech.operation import Operation
from google.cloud.speech.sample import Sample
RETURNED = ASYNC_RECOGNIZE_RESPONSE
Expand All @@ -188,7 +188,7 @@ def test_async_recognize(self):
client.connection = _Connection(RETURNED)

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=Encoding.LINEAR16,
encoding=speech.Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)
operation = client.async_recognize(sample)
self.assertIsInstance(operation, Operation)
Expand Down