diff --git a/docs/speech-usage.rst b/docs/speech-usage.rst index e3341051d128..95e18f7a58f6 100644 --- a/docs/speech-usage.rst +++ b/docs/speech-usage.rst @@ -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 @@ -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) @@ -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, diff --git a/speech/google/cloud/speech/__init__.py b/speech/google/cloud/speech/__init__.py index ef55810893a7..4a9e4e4f6fc6 100644 --- a/speech/google/cloud/speech/__init__.py +++ b/speech/google/cloud/speech/__init__.py @@ -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 diff --git a/speech/unit_tests/test_client.py b/speech/unit_tests/test_client.py index 5972a0014eb3..e51805a81521 100644 --- a/speech/unit_tests/test_client.py +++ b/speech/unit_tests/test_client.py @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -158,12 +158,12 @@ 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() @@ -171,14 +171,14 @@ def test_async_supported_encodings(self): 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 @@ -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)