|
17 | 17 |
|
18 | 18 | class TestSpeechClient(unittest.TestCase): |
19 | 19 |
|
20 | | - def test_sync_recognize_local_file(self): |
21 | | - import io |
| 20 | + def _make_sync_request(self, content=None, source_uri=None, |
| 21 | + max_alternatives=None): |
| 22 | + from google.cloud.speech.encoding import Encoding |
22 | 23 | from google.cloud import speech |
| 24 | + |
23 | 25 | client = speech.Client() |
| 26 | + sample = client.sample(content=content, |
| 27 | + source_uri=source_uri, |
| 28 | + encoding=Encoding.LINEAR16, |
| 29 | + sample_rate=16000) |
| 30 | + result = client.sync_recognize(sample, |
| 31 | + language_code='en-US', |
| 32 | + max_alternatives=max_alternatives, |
| 33 | + profanity_filter=True, |
| 34 | + speech_context=['Google', |
| 35 | + 'cloud']) |
| 36 | + return result |
| 37 | + |
| 38 | + def test_sync_recognize_local_file(self): |
24 | 39 | file_name = 'system_tests/data/hello.wav' |
25 | 40 |
|
26 | | - with io.open(file_name, 'rb') as file_obj: |
27 | | - sample = client.sample(content=file_obj.read(), |
28 | | - encoding=speech.Encoding.LINEAR16, |
29 | | - sample_rate=16000) |
30 | | - res = client.sync_recognize(sample, |
31 | | - language_code='en-US', |
32 | | - max_alternatives=2, |
33 | | - profanity_filter=True, |
34 | | - speech_context=['Google', 'cloud']) |
35 | | - self.assertEqual(res[0].transcript, |
| 41 | + with open(file_name, 'rb') as file_obj: |
| 42 | + result = self._make_sync_request(content=file_obj.read(), |
| 43 | + max_alternatives=2) |
| 44 | + |
| 45 | + self.assertEqual(result[0]['transcript'], |
36 | 46 | 'hello thank you for using Google Cloud platform') |
37 | | - self.assertEqual(len(res), 2) |
| 47 | + self.assertGreater(result[0]['confidence'], .90) |
| 48 | + self.assertEqual(result[1]['transcript'], |
| 49 | + 'thank you for using Google Cloud platform') |
| 50 | + self.assertEqual(len(result), 2) |
38 | 51 |
|
39 | 52 | def test_sync_recognize_gcs_file(self): |
40 | | - from google.cloud import speech |
41 | | - client = speech.Client() |
42 | | - source_uri = 'gs://ferrous-arena-my-test-bucket/hello.wav' |
| 53 | + import os |
43 | 54 |
|
44 | | - sample = client.sample(source_uri=source_uri, |
45 | | - encoding=speech.Encoding.LINEAR16, |
46 | | - sample_rate=16000) |
47 | | - res = client.sync_recognize(sample, |
48 | | - language_code='en-US', |
49 | | - max_alternatives=1, |
50 | | - profanity_filter=True, |
51 | | - speech_context=['Google', 'cloud']) |
52 | | - self.assertEqual(res[0].transcript, |
| 55 | + source_uri = os.getenv('SPEECH_GCS_URI') |
| 56 | + result = self._make_sync_request(source_uri=source_uri, |
| 57 | + max_alternatives=1) |
| 58 | + self.assertEqual(result[0]['transcript'], |
53 | 59 | 'hello thank you for using Google Cloud platform') |
54 | | - self.assertEqual(len(res), 1) |
| 60 | + self.assertGreater(result[0]['confidence'], .90) |
| 61 | + self.assertEqual(len(result), 1) |
0 commit comments