|
1 | 1 | <?php |
2 | | -# Copyright 2020 Google LLC |
| 2 | +# Copyright 2023 Google LLC |
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | # you may not use this file except in compliance with the License. |
5 | 5 | # You may obtain a copy of the License at |
|
15 | 15 | namespace Google\Cloud\Samples\Speech; |
16 | 16 |
|
17 | 17 | # [START speech_profanity_filter_gcs] |
18 | | -use Google\Cloud\Speech\V1\RecognitionAudio; |
19 | | -use Google\Cloud\Speech\V1\RecognitionConfig; |
20 | | -use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; |
21 | | -use Google\Cloud\Speech\V1\SpeechClient; |
| 18 | +use Google\Cloud\Speech\V2\Client\SpeechClient; |
| 19 | +use Google\Cloud\Speech\V2\RecognitionConfig; |
| 20 | +use Google\Cloud\Speech\V2\RecognitionFeatures; |
| 21 | +use Google\Cloud\Speech\V2\ExplicitDecodingConfig; |
| 22 | +use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding; |
| 23 | +use Google\Cloud\Speech\V2\RecognizeRequest; |
22 | 24 |
|
23 | 25 | /** |
| 26 | + * @param string $projectId The Google Cloud project ID. |
| 27 | + * @param string $location The location of the recognizer. |
| 28 | + * @param string $recognizerId The ID of the recognizer to use. |
24 | 29 | * @param string $uri The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name) |
25 | 30 | */ |
26 | | -function profanity_filter_gcs(string $uri) |
| 31 | +function profanity_filter_gcs(string $projectId, string $location, string $recognizerId, string $uri) |
27 | 32 | { |
28 | | - // change these variables if necessary |
29 | | - $encoding = AudioEncoding::LINEAR16; |
30 | | - $sampleRateHertz = 32000; |
31 | | - $languageCode = 'en-US'; |
32 | | - $profanityFilter = true; |
33 | | - |
34 | | - // set string as audio content |
35 | | - $audio = (new RecognitionAudio()) |
36 | | - ->setUri($uri); |
| 33 | + // create the speech client |
| 34 | + $speech = new SpeechClient(); |
| 35 | + |
| 36 | + $recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId); |
37 | 37 |
|
38 | | - // set config |
| 38 | + // When true, the profanity filter will be enabled. |
| 39 | + $features = new RecognitionFeatures([ |
| 40 | + 'profanity_filter' => true |
| 41 | + ]); |
| 42 | + |
| 43 | + // Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig} |
39 | 44 | $config = (new RecognitionConfig()) |
40 | | - ->setEncoding($encoding) |
41 | | - ->setSampleRateHertz($sampleRateHertz) |
42 | | - ->setLanguageCode($languageCode) |
43 | | - ->setProfanityFilter($profanityFilter); |
| 45 | + ->setFeatures($features) |
| 46 | + ->setExplicitDecodingConfig(new ExplicitDecodingConfig([ |
| 47 | + 'encoding' => AudioEncoding::LINEAR16, |
| 48 | + 'sample_rate_hertz' => 16000, |
| 49 | + 'audio_channel_count' => 1, |
| 50 | + ])); |
44 | 51 |
|
45 | | - // create the speech client |
46 | | - $client = new SpeechClient(); |
| 52 | + $request = (new RecognizeRequest()) |
| 53 | + ->setRecognizer($recognizerName) |
| 54 | + ->setConfig($config) |
| 55 | + ->setUri($uri); |
47 | 56 |
|
48 | 57 | # Detects speech in the audio file |
49 | | - $response = $client->recognize($config, $audio); |
| 58 | + $response = $speech->recognize($request); |
50 | 59 |
|
51 | 60 | # Print most likely transcription |
52 | 61 | foreach ($response->getResults() as $result) { |
53 | | - $transcript = $result->getAlternatives()[0]->getTranscript(); |
| 62 | + $alternatives = $result->getAlternatives(); |
| 63 | + $mostLikely = $alternatives[0]; |
| 64 | + $transcript = $mostLikely->getTranscript(); |
54 | 65 | printf('Transcript: %s' . PHP_EOL, $transcript); |
55 | 66 | } |
56 | 67 |
|
57 | | - $client->close(); |
| 68 | + $speech->close(); |
58 | 69 | } |
59 | 70 | # [END speech_profanity_filter_gcs] |
60 | 71 |
|
|
0 commit comments