Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 18105bc

Browse filesBrowse files
authored
speech: canonical sample for speechcontext ga of classes (GoogleCloudPlatform#2756)
* speech: canonical sample for speechcontext ga of classes * speech: fixing adaptation per Noah's comments
1 parent 80cc4e5 commit 18105bc
Copy full SHA for 18105bc

File tree

Expand file treeCollapse file tree

3 files changed

+70
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+70
-1
lines changed

‎speech/cloud-client/requirements.txt

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-speech==1.2.0
1+
google-cloud-speech==1.3.1
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def transcribe_context_classes(storage_uri):
17+
"""Provides "hints" to the speech recognizer to
18+
favor specific classes of words in the results."""
19+
# [START speech_context_classes]
20+
from google.cloud import speech
21+
client = speech.SpeechClient()
22+
23+
# storage_uri = 'gs://YOUR_BUCKET_ID/path/to/your/file.wav'
24+
audio = speech.types.RecognitionAudio(uri=storage_uri)
25+
26+
# SpeechContext: to configure your speech_context see:
27+
# https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#speechcontext
28+
# Full list of supported phrases (class tokens) here:
29+
# https://cloud.google.com/speech-to-text/docs/class-tokens
30+
speech_context = speech.types.SpeechContext(phrases=['$TIME'])
31+
32+
# RecognitionConfig: to configure your encoding and sample_rate_hertz, see:
33+
# https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#recognitionconfig
34+
config = speech.types.RecognitionConfig(
35+
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
36+
sample_rate_hertz=8000,
37+
language_code='en-US',
38+
speech_contexts=[speech_context])
39+
40+
response = client.recognize(config, audio)
41+
42+
for i, result in enumerate(response.results):
43+
alternative = result.alternatives[0]
44+
print('-' * 20)
45+
print('First alternative of result {}'.format(i))
46+
print('Transcript: {}'.format(alternative.transcript))
47+
# [END speech_context_classes]
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2020, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import transcribe_context_classes
15+
16+
17+
def test_transcribe_context_classes(capsys):
18+
transcribe_context_classes.transcribe_context_classes(
19+
'gs://cloud-samples-data/speech/commercial_mono.wav')
20+
out, _ = capsys.readouterr()
21+
22+
assert 'First alternative of result ' in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.