启用脏话过滤器

本页面介绍如何使用 Speech-to-Text 自动检测音频数据中的亵渎性字词,并在转录内容中予以剔除。

您可以通过在 RecognitionFeatures 中设置 profanityFilter=true 来启用脏话过滤器。启用后,Speech-to-Text 将尝试检测亵渎性字词并在转录内容中仅返回第一个字母后跟星号(例如 f***)。如果此字段设置为 false 或未设置,Speech-to-Text 将不会尝试过滤脏话。

以下示例演示了如何启用脏话过滤器以识别存储在 Cloud Storage 存储桶中的音频。

Python

如需了解如何安装和使用 Speech-to-Text 客户端库,请参阅 Speech-to-Text 客户端库。如需了解详情,请参阅 Speech-to-Text Python API 参考文档

如需向 Speech-to-Text 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import speech

def sync_recognize_with_profanity_filter_gcs(gcs_uri: str) -> speech.RecognizeResponse:
    client = speech.SpeechClient()

    audio = {"uri": gcs_uri}

    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
        sample_rate_hertz=16000,
        language_code="en-US",
        profanity_filter=True,
    )

    response = client.recognize(config=config, audio=audio)

    for result in response.results:
        alternative = result.alternatives[0]
        print(f"Transcript: {alternative.transcript}")

    return response.results