Enable the profanity filter

This page describes how to use Speech-to-Text to automatically detect profane words in your audio data and censor them in the transcript.

You can enable the profanity filter by setting profanityFilter=true in the RecognitionFeatures. If enabled, Speech-to-Text will attempt to detect profane words and return only the first letter followed by asterisks in the transcript (for example, f***). If this field is set to false or not set, Speech-to-Text will not attempt to filter profanities.

The following sample demonstrates how to enable the profanity filter to recognize audio stored in a Cloud Storage bucket.

Python

To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Python API reference documentation.

To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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