Skip to content
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
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@ Cloud Storage
client = storage.Client()
bucket = client.get_bucket('<your-bucket-name>')
blob = bucket.blob('my-test-file.txt')
blob.upload_from_string('this is test content!')
blob.upload_from_string('this is test content!')

This comment was marked as spam.

This comment was marked as spam.

2 changes: 1 addition & 1 deletion docs/speech-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Connection
.. automodule:: google.cloud.speech.connection
:members:
:undoc-members:
:show-inheritance:
:show-inheritance:
19 changes: 10 additions & 9 deletions docs/speech-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ and returns alternative text transcriptons.

.. code-block:: python

>>> alternatives = client.sync_recognize(None,"gs://my-bucket/recording.flac",
... "FLAC", 16000, max_alternatives=2):
>>> alternatives = client.sync_recognize(
... None, 'gs://my-bucket/recording.flac',
... 'FLAC', 16000, max_alternatives=2)
>>> for alternative in alternatives:
... print('=' * 20)
... print(' transcript: %s' % (alternative["transcript"],))
... print(' confidence: %s' % (alternative["confidence"],))
... print('transcript: ' + alternative['transcript'])
... print('confidence: ' + alternative['confidence'])

This comment was marked as spam.

This comment was marked as spam.

====================
transcript: Hello, this is a test
confidence: 0.81
transcript: Hello, this is a test
confidence: 0.81
====================
transcript: Hello, this is one test
confidence: 0
transcript: Hello, this is one test
confidence: 0

.. _sync_recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize
.. _sync_recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize
44 changes: 22 additions & 22 deletions google/cloud/speech/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 Google Inc. All Rights Reserved.
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,8 +70,7 @@ class Client(client_module.Client):

def sync_recognize(self, content, source_uri, encoding, sample_rate,
language_code=None, max_alternatives=None,
profanity_filter=None,
speech_context=None):
profanity_filter=None, speech_context=None):
"""Synchronous Speech Recognition.

.. _sync_recognize: https://cloud.google.com/speech/reference/\
Expand All @@ -84,10 +83,10 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,

:type source_uri: str
:param source_uri: URI that points to a file that contains audio
data bytes as specified in RecognitionConfig.
Currently, only Google Cloud Storage URIs are
supported, which must be specified in the following
format: gs://bucket_name/object_name
data bytes as specified in RecognitionConfig.
Currently, only Google Cloud Storage URIs are
supported, which must be specified in the following
format: ``gs://bucket_name/object_name``.

:type encoding: str
:param encoding: encoding of audio data sent in all RecognitionAudio
Expand All @@ -97,16 +96,16 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,

:type sample_rate: int
:param sample_rate: Sample rate in Hertz of the audio data sent in all
requests. Valid values are: 8000-48000.
16000 is optimal. For best results, set the
sampling rate of the audio source to 16000 Hz.
If that's not possible, use the native sample rate
of the audio source (instead of re-sampling).
requests. Valid values are: 8000-48000. For best
results, set the sampling rate of the audio source
to 16000 Hz. If that's not possible, use the
native sample rate of the audio source (instead of
re-sampling).

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: "en-GB".
If omitted, defaults to "en-US".
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand All @@ -119,8 +118,8 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,
:param profanity_filter: If True, the server will attempt to filter
out profanities, replacing all but the
initial character in each filtered word with
asterisks, e.g. "f***". If False or omitted,
profanities won't be filtered out.
asterisks, e.g. ``'f***'``. If False or
omitted, profanities won't be filtered out.

:type speech_context: list
:param speech_context: A list of strings (max 50) containing words and
Expand All @@ -140,13 +139,14 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,
between 0 and 1.
"""

if (content is None) and (source_uri is None):
raise ValueError('content and source_uri cannot be both equal to\
None')
if content is None and source_uri is None:
raise ValueError('content and source_uri cannot be both '
'equal to None')

if content is not None and source_uri is not None:
raise ValueError('content and source_uri cannot be both '
'different from None')

if (content is not None) and (source_uri is not None):
raise ValueError('content and source_uri cannot be both different from\
None')
if encoding is None:
raise ValueError('encoding cannot be None')
if sample_rate is None:
Expand Down
2 changes: 1 addition & 1 deletion scripts/verify_included_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'google.cloud.monitoring.__init__',
'google.cloud.pubsub.__init__',
'google.cloud.resource_manager.__init__',
'google.cloud.speech.__init__',
'google.cloud.storage.__init__',
'google.cloud.streaming.__init__',
'google.cloud.streaming.buffered_stream',
Expand All @@ -55,7 +56,6 @@
'google.cloud.translate.__init__',
'google.cloud.vision.__init__',
'google.cloud.vision.fixtures',
'google.cloud.speech.__init__',
])


Expand Down
12 changes: 6 additions & 6 deletions unit_tests/speech/_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
'alternatives': [
{
'transcript': 'hello',
'confidence': 0.784919
}
]
}
]
'confidence': 0.784919,
},
],
},
],
}

SYNC_RECOGNIZE_EMPTY_RESPONSE = {
'results': []
'results': [],
}
41 changes: 26 additions & 15 deletions unit_tests/speech/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 Google Inc. All Rights Reserved.
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,12 +14,11 @@

import unittest

_AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'


class TestClient(unittest.TestCase):
SAMPLE_RATE = 16000
HINTS = ['hi']
AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'

def _getTargetClass(self):
from google.cloud.speech.client import Client
Expand Down Expand Up @@ -78,9 +77,15 @@ def test_sync_recognize_content_with_optional_parameters(self):
profanity_filter=True,
speech_context=self.HINTS)

self.assertEqual(REQUEST,
client.connection._requested[0]['data'])
self.assertEqual(response[0]['transcript'], 'hello')
self.assertEqual(len(client.connection._requested), 1)
req = client.connection._requested[0]
self.assertEqual(len(req), 3)
self.assertEqual(req['data'], REQUEST)
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], 'syncrecognize')

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

def test_sync_recognize_source_uri_without_optional_parameters(self):
from google.cloud.speech.client import Encoding
Expand All @@ -93,7 +98,7 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
'sampleRate': 16000,
},
'audio': {
'uri': _AUDIO_SOURCE_URI,
'uri': self.AUDIO_SOURCE_URI,
}
}
credentials = _Credentials()
Expand All @@ -102,13 +107,19 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):

encoding = Encoding.FLAC

response = client.sync_recognize(None, _AUDIO_SOURCE_URI,
response = client.sync_recognize(None, self.AUDIO_SOURCE_URI,
encoding,
self.SAMPLE_RATE)

self.assertEqual(REQUEST,
client.connection._requested[0]['data'])
self.assertEqual(response[0]['transcript'], 'hello')
self.assertEqual(len(client.connection._requested), 1)
req = client.connection._requested[0]
self.assertEqual(len(req), 3)
self.assertEqual(req['data'], REQUEST)
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], 'syncrecognize')

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

def test_sync_recognize_without_content_or_source_uri(self):
from google.cloud.speech.client import Encoding
Expand All @@ -128,15 +139,15 @@ def test_sync_recognize_with_content_and_source_uri(self):
client = self._makeOne(credentials=credentials)

with self.assertRaises(ValueError):
client.sync_recognize(_AUDIO_CONTENT, _AUDIO_SOURCE_URI,
client.sync_recognize(_AUDIO_CONTENT, self.AUDIO_SOURCE_URI,
Encoding.FLAC, self.SAMPLE_RATE)

def test_sync_recognize_without_encoding(self):
credentials = _Credentials()
client = self._makeOne(credentials=credentials)

with self.assertRaises(ValueError):
client.sync_recognize(None, _AUDIO_SOURCE_URI, None,
client.sync_recognize(None, self.AUDIO_SOURCE_URI, None,
self.SAMPLE_RATE)

def test_sync_recognize_without_samplerate(self):
Expand All @@ -146,7 +157,7 @@ def test_sync_recognize_without_samplerate(self):
client = self._makeOne(credentials=credentials)

with self.assertRaises(ValueError):
client.sync_recognize(None, _AUDIO_SOURCE_URI, Encoding.FLAC,
client.sync_recognize(None, self.AUDIO_SOURCE_URI, Encoding.FLAC,
None)

def test_sync_recognize_with_empty_results(self):
Expand All @@ -158,7 +169,7 @@ def test_sync_recognize_with_empty_results(self):
client.connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)

with self.assertRaises(ValueError):
client.sync_recognize(None, _AUDIO_SOURCE_URI, Encoding.FLAC,
client.sync_recognize(None, self.AUDIO_SOURCE_URI, Encoding.FLAC,
self.SAMPLE_RATE)


Expand Down
2 changes: 1 addition & 1 deletion unit_tests/speech/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 Google Inc. All Rights Reserved.
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down