Skip to content

Commit 6ae4e75

Browse files
happyhumantelpirion
authored andcommitted
Added the sample for Word Level Confidence [(#1567)](#1567)
* Added the sample for Word Level Confidence * Added the extra line * Added parameter comment * Removed the line with blank space
1 parent d9e07fb commit 6ae4e75

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

speech/snippets/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ To run this sample:
233233
python beta_snippets.py diarization resources/commercial_mono.wav
234234
python beta_snippets.py multi-channel resources/commercial_mono.wav
235235
python beta_snippets.py multi-language resources/multi.wav en-US es
236+
python beta_snippets.py word-level-conf resources/commercial_mono.wav
236237
237238
positional arguments:
238239
command

speech/snippets/beta_snippets.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
python beta_snippets.py diarization resources/commercial_mono.wav
2525
python beta_snippets.py multi-channel resources/commercial_mono.wav
2626
python beta_snippets.py multi-language resources/multi.wav en-US es
27+
python beta_snippets.py word-level-conf resources/commercial_mono.wav
2728
"""
2829

2930
import argparse
@@ -240,6 +241,39 @@ def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
240241
# [END speech_transcribe_multilanguage]
241242

242243

244+
def transcribe_file_with_word_level_confidence(speech_file):
245+
"""Transcribe the given audio file synchronously with
246+
word level confidence."""
247+
# [START speech_transcribe_word_level_confidence]
248+
from google.cloud import speech_v1p1beta1 as speech
249+
client = speech.SpeechClient()
250+
251+
# TODO(developer): Uncomment and set to a path to your audio file.
252+
# speech_file = 'path/to/file.wav'
253+
254+
with open(speech_file, 'rb') as audio_file:
255+
content = audio_file.read()
256+
257+
audio = speech.types.RecognitionAudio(content=content)
258+
259+
config = speech.types.RecognitionConfig(
260+
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
261+
sample_rate_hertz=16000,
262+
language_code='en-US',
263+
enable_word_confidence=True)
264+
265+
response = client.recognize(config, audio)
266+
267+
for i, result in enumerate(response.results):
268+
alternative = result.alternatives[0]
269+
print('-' * 20)
270+
print('First alternative of result {}'.format(i))
271+
print(u'Transcript: {}'.format(alternative.transcript))
272+
print(u'First Word and Confidence: ({}, {})'.format(
273+
alternative.words[0].word, alternative.words[0].confidence))
274+
# [END speech_transcribe_word_level_confidence]
275+
276+
243277
if __name__ == '__main__':
244278
parser = argparse.ArgumentParser(
245279
description=__doc__,
@@ -248,9 +282,11 @@ def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
248282
parser.add_argument(
249283
'path', help='File for audio file to be recognized')
250284
parser.add_argument(
251-
'first', help='First language in audio file to be recognized')
285+
'first', help='First language in audio file to be recognized',
286+
nargs='?')
252287
parser.add_argument(
253-
'second', help='Second language in audio file to be recognized')
288+
'second', help='Second language in audio file to be recognized',
289+
nargs='?')
254290

255291
args = parser.parse_args()
256292

@@ -266,3 +302,5 @@ def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
266302
transcribe_file_with_multichannel(args.path)
267303
elif args.command == 'multi-language':
268304
transcribe_file_with_multilanguage(args.path, args.first, args.second)
305+
elif args.command == 'word-level-conf':
306+
transcribe_file_with_word_level_confidence(args.path)

speech/snippets/beta_snippets_test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
transcribe_file_with_enhanced_model,
2020
transcribe_file_with_metadata,
2121
transcribe_file_with_multichannel,
22-
transcribe_file_with_multilanguage)
22+
transcribe_file_with_multilanguage,
23+
transcribe_file_with_word_level_confidence)
2324

2425
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
2526

@@ -70,3 +71,11 @@ def test_transcribe_multilanguage_file(capsys):
7071
out, err = capsys.readouterr()
7172

7273
assert 'how are you doing estoy bien e tu' in out
74+
75+
76+
def test_transcribe_word_level_confidence(capsys):
77+
transcribe_file_with_word_level_confidence(
78+
os.path.join(RESOURCES, 'Google_Gnome.wav'))
79+
out, err = capsys.readouterr()
80+
81+
assert 'OK Google stream stranger things from Netflix to my TV' in out

0 commit comments

Comments
 (0)