冒とくフィルタを有効にする

このページでは、Speech-to-Text を使用して音声データ内の冒とく的な単語を自動的に検出し、音声文字変換テキストで検査する方法について説明します。

RecognitionConfigprofanityFilter=true を設定することで、冒とくフィルタを有効にできます。有効な場合、Speech-to-Text は冒とく的な単語の検出を試みます。検出すると、該当箇所の先頭文字以外をアスタリスクに置き換えた文字(例: f***)を返します。このフィールドを false に設定するか、未設定のままにすると、Speech-to-Text は冒とく的な表現をフィルタリングしません。

次のサンプルは、Google Cloud Storage バケットに保存されている音声を冒とくフィルタで認識する方法を示しています。

Java

import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
import com.google.cloud.speech.v1.RecognizeResponse;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;
import java.util.List;

public class SpeechProfanityFilter {

  public void speechProfanityFilter() throws Exception {
    String uriPath = "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
   */
  public static void speechProfanityFilter(String gcsUri) throws Exception {
    // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
    try (SpeechClient speech = SpeechClient.create()) {

      // Configure remote file request
      RecognitionConfig config =
          RecognitionConfig.newBuilder()
              .setEncoding(AudioEncoding.FLAC)
              .setLanguageCode("en-US")
              .setSampleRateHertz(16000)
              .setProfanityFilter(true)
              .build();

      // Set the remote path for the audio file
      RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();

      // Use blocking call to get audio transcript
      RecognizeResponse response = speech.recognize(config, audio);
      List<SpeechRecognitionResult> results = response.getResultsList();

      for (SpeechRecognitionResult result : results) {
        // There can be several alternative transcripts for a given chunk of speech. Just use the
        // first (most likely) one here.
        SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
        System.out.printf("Transcription: %s\n", alternative.getTranscript());
      }
    }
  }
}

Node.js

// Filters profanity

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const gcsUri = 'gs://my-bucket/audio.raw';

async function syncRecognizeWithProfanityFilter() {
  // Imports the Google Cloud client library
  const speech = require('@google-cloud/speech');

  // Creates a client
  const client = new speech.SpeechClient();

  const audio = {
    uri: gcsUri,
  };

  const config = {
    encoding: 'FLAC',
    sampleRateHertz: 16000,
    languageCode: 'en-US',
    profanityFilter: true, // set this to true
  };
  const request = {
    audio: audio,
    config: config,
  };

  // Detects speech in the audio file
  const [response] = await client.recognize(request);
  const transcription = response.results
    .map(result => result.alternatives[0].transcript)
    .join('\n');
  console.log(`Transcription: ${transcription}`);
}
syncRecognizeWithProfanityFilter().catch(console.error);

Python

def sync_recognize_with_profanity_filter_gcs(gcs_uri):

    from google.cloud import speech

    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 i, result in enumerate(response.results):
        alternative = result.alternatives[0]
        print(u"Transcript: {}".format(alternative.transcript))