为什么 enable_automatic_punctuation=true 在 python 中给出错误 3

why enable_automatic_punctuation=true gives an error in python 3

我想从 google-speech-api 中获取带有标点符号的单词转录本。我正在使用 python 3 和

当我 运行 我的代码与

中的代码示例完全相同时,我得到了这个错误

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/beta_snippets.py.

我收到一个错误

"ValueError: Protocol message RecognitionConfig has no "enableAutomaticPunctuation" field. ".

我能做些什么来克服这个问题。

def transcribe_file_with_auto_punctuation(path):
    client = speech.SpeechClient()

with io.open(path, 'rb') as audio_file:
    content = audio_file.read()
    audio = speech.types.RecognitionAudio(content=content)
    config = speech.types.RecognitionConfig(
    enableAutomaticPunctuation=True,
    encoding= speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
    languageCode= 'en-US',
    model= 'default')

response = client.recognize(config, audio)

for i, result in enumerate(response.results):
    alternative = result.alternatives[0]
    print('-' * 20)
    print('First alternative of result {}'.format(i))
    print('Transcript: {}'.format(alternative.transcript))

Python API 使用 snake_case 约定命名所有选项(小写单词与下划线连接),因此您想要的选项称为 enable_automatic_punctuation. Note that the same applies to the language_code option:

config = speech.types.RecognitionConfig(
    enable_automatic_punctuation=True,
    encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
    language_code='en-US',
    model='default')

您链接到的示例确实使用了 snake_case 名称。

请注意,自动标点符号是较新版本中的一项新功能v1p1beta1 release of the API, so make sure you import the right class. From the API Reference section

A new beta release, spelled v1p1beta1, is provided to provide for preview of upcoming features. In order to use this, you will want to import from google.cloud.speech_v1p1beta1 in lieu of google.cloud.speech.

并且该功能将来可能会从免费 API 中删除。正如 enableAutomaticPunctuation 的 v1p1beta1 文档所述:

This is currently offered as an experimental service, complimentary to all users. In the future this may be exclusively available as a premium feature.