Stay organized with collectionsSave and categorize content based on your preferences.
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 settingprofanityFilter=truein
theRecognitionConfig.
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 tofalseor 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 Google Cloud Storage bucket.
importcom.google.cloud.speech.v1.RecognitionAudio;importcom.google.cloud.speech.v1.RecognitionConfig;importcom.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;importcom.google.cloud.speech.v1.RecognizeResponse;importcom.google.cloud.speech.v1.SpeechClient;importcom.google.cloud.speech.v1.SpeechRecognitionAlternative;importcom.google.cloud.speech.v1.SpeechRecognitionResult;importjava.util.List;publicclassSpeechProfanityFilter{publicvoidspeechProfanityFilter()throwsException{StringuriPath="gs://cloud-samples-tests/speech/brooklyn.flac";speechProfanityFilter(uriPath);}/*** Transcribe a remote audio file with multi-channel recognition** @param gcsUri the path to the audio file*/publicstaticvoidspeechProfanityFilter(StringgcsUri)throwsException{// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALStry(SpeechClientspeech=SpeechClient.create()){// Configure remote file requestRecognitionConfigconfig=RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setLanguageCode("en-US").setSampleRateHertz(16000).setProfanityFilter(true).build();// Set the remote path for the audio fileRecognitionAudioaudio=RecognitionAudio.newBuilder().setUri(gcsUri).build();// Use blocking call to get audio transcriptRecognizeResponseresponse=speech.recognize(config,audio);List<SpeechRecognitionResult>results=response.getResultsList();for(SpeechRecognitionResultresult:results){// There can be several alternative transcripts for a given chunk of speech. Just use the// first (most likely) one here.SpeechRecognitionAlternativealternative=result.getAlternativesList().get(0);System.out.printf("Transcription: %s\n",alternative.getTranscript());}}}}
// Filters profanity/*** TODO(developer): Uncomment these variables before running the sample.*/// const gcsUri = 'gs://my-bucket/audio.raw';asyncfunctionsyncRecognizeWithProfanityFilter(){// Imports the Google Cloud client libraryconstspeech=require('@google-cloud/speech');// Creates a clientconstclient=newspeech.SpeechClient();constaudio={uri:gcsUri,};constconfig={encoding:'FLAC',sampleRateHertz:16000,languageCode:'en-US',profanityFilter:true,// set this to true};constrequest={audio:audio,config:config,};// Detects speech in the audio fileconst[response]=awaitclient.recognize(request);consttranscription=response.results.map(result=>result.alternatives[0].transcript).join('\n');console.log(`Transcription:${transcription}`);}syncRecognizeWithProfanityFilter().catch(console.error);
fromgoogle.cloudimportspeechfromgoogle.cloud.speechimportRecognizeResponsedefsync_recognize_with_profanity_filter_gcs(audio_uri:str)->RecognizeResponse:"""Recognizes speech from an audio file in Cloud Storage and filters out profane language.Args:audio_uri (str): The Cloud Storage URI of the input audio, e.g., gs://[BUCKET]/[FILE]Returns:cloud_speech.RecognizeResponse: The full response object which includes the transcription results."""# Define the audio sourceaudio={"uri":audio_uri}client=speech.SpeechClient()config=speech.RecognitionConfig(encoding=speech.RecognitionConfig.AudioEncoding.FLAC,# Audio formatsample_rate_hertz=16000,language_code="en-US",# Enable profanity filterprofanity_filter=True,)response=client.recognize(config=config,audio=audio)forresultinresponse.results:alternative=result.alternatives[0]print(f"Transcript:{alternative.transcript}")returnresponse.results
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Enable the profanity filter\n\nThis page describes how to use Speech-to-Text to automatically detect\nprofane words in your audio data and censor them in the transcript.\n\nYou can enable the profanity filter by setting `profanityFilter`=`true` in\nthe [`RecognitionConfig`](/speech-to-text/docs/reference/rest/v1/RecognitionConfig).\nIf enabled, Speech-to-Text will attempt to detect profane words and return\nonly the first letter followed by asterisks in the transcript (for example,\nf\\*\\*\\*). If this field is set to `false` or not set, Speech-to-Text will\nnot attempt to filter profanities.\n\nThe following sample demonstrates how to enable the profanity filter to\nrecognize audio stored in a Google Cloud Storage bucket. \n\n### Java\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nFor more information, see the\n[Speech-to-Text Java API\nreference documentation](/java/docs/reference/google-cloud-speech/latest/overview).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionAudio.html;\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.html;\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.html.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding.html;\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognizeResponse.html;\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechClient.html;\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechRecognitionAlternative.html;\n import com.google.cloud.speech.v1.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechRecognitionResult.html;\n import java.util.List;\n\n public class SpeechProfanityFilter {\n\n public void speechProfanityFilter() throws Exception {\n String uriPath = \"gs://cloud-samples-tests/speech/brooklyn.flac\";\n speechProfanityFilter(uriPath);\n }\n\n /**\n * Transcribe a remote audio file with multi-channel recognition\n *\n * @param gcsUri the path to the audio file\n */\n public static void speechProfanityFilter(String gcsUri) throws Exception {\n // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS\n try (https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechClient.html speech = https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechClient.html.create()) {\n\n // Configure remote file request\n https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.html config =\n https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.html.newBuilder()\n .https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.Builder.html#com_google_cloud_speech_v1_RecognitionConfig_Builder_setEncoding_com_google_cloud_speech_v1_RecognitionConfig_AudioEncoding_(https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding.html.FLAC)\n .setLanguageCode(\"en-US\")\n .https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.Builder.html#com_google_cloud_speech_v1_RecognitionConfig_Builder_setSampleRateHertz_int_(16000)\n .https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionConfig.Builder.html#com_google_cloud_speech_v1_RecognitionConfig_Builder_setProfanityFilter_boolean_(true)\n .build();\n\n // Set the remote path for the audio file\n https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionAudio.html audio = https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognitionAudio.html.newBuilder().setUri(gcsUri).build();\n\n // Use blocking call to get audio transcript\n https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognizeResponse.html response = speech.recognize(config, audio);\n List\u003cSpeechRecognitionResult\u003e results = response.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.RecognizeResponse.html#com_google_cloud_speech_v1_RecognizeResponse_getResultsList__();\n\n for (https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechRecognitionResult.html result : results) {\n // There can be several alternative transcripts for a given chunk of speech. Just use the\n // first (most likely) one here.\n https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechRecognitionAlternative.html alternative = result.getAlternativesList().get(0);\n System.out.printf(\"Transcription: %s\\n\", alternative.https://cloud.google.com/java/docs/reference/google-cloud-speech/latest/com.google.cloud.speech.v1.SpeechRecognitionAlternative.html#com_google_cloud_speech_v1_SpeechRecognitionAlternative_getTranscript__());\n }\n }\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nFor more information, see the\n[Speech-to-Text Node.js API\nreference documentation](/nodejs/docs/reference/speech/latest).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Filters profanity\n\n /**\n * TODO(developer): Uncomment these variables before running the sample.\n */\n // const gcsUri = 'gs://my-bucket/audio.raw';\n\n async function syncRecognizeWithProfanityFilter() {\n // Imports the Google Cloud client library\n const speech = require('https://cloud.google.com/nodejs/docs/reference/speech/latest/overview.html');\n\n // Creates a client\n const client = new speech.https://cloud.google.com/nodejs/docs/reference/speech/latest/overview.html();\n\n const audio = {\n uri: gcsUri,\n };\n\n const config = {\n encoding: 'FLAC',\n sampleRateHertz: 16000,\n languageCode: 'en-US',\n profanityFilter: true, // set this to true\n };\n const request = {\n audio: audio,\n config: config,\n };\n\n // Detects speech in the audio file\n const [response] = await client.recognize(request);\n const transcription = response.results\n .map(result =\u003e result.alternatives[0].transcript)\n .join('\\n');\n console.log(`Transcription: ${transcription}`);\n }\n syncRecognizeWithProfanityFilter().catch(console.error);\n\n### Python\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nFor more information, see the\n[Speech-to-Text Python API\nreference documentation](/python/docs/reference/speech/latest).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import speech\n from google.cloud.speech import RecognizeResponse\n\n\n def sync_recognize_with_profanity_filter_gcs(audio_uri: str) -\u003e RecognizeResponse:\n \"\"\"Recognizes speech from an audio file in Cloud Storage and filters out profane language.\n Args:\n audio_uri (str): The Cloud Storage URI of the input audio, e.g., gs://[BUCKET]/[FILE]\n Returns:\n cloud_speech.RecognizeResponse: The full response object which includes the transcription results.\n \"\"\"\n # Define the audio source\n audio = {\"uri\": audio_uri}\n\n client = speech.SpeechClient()\n config = speech.https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v1.types.RecognitionConfig.html(\n encoding=speech.RecognitionConfig.AudioEncoding.FLAC, # Audio format\n sample_rate_hertz=16000,\n language_code=\"en-US\",\n # Enable profanity filter\n profanity_filter=True,\n )\n\n response = client.https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v1.services.speech.SpeechClient.html#google_cloud_speech_v1_services_speech_SpeechClient_recognize(config=config, audio=audio)\n\n for result in response.results:\n alternative = result.alternatives[0]\n print(f\"Transcript: {alternative.transcript}\")\n\n return response.results\n\n\u003cbr /\u003e"]]