|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -# [START speech_profanity_filter] |
16 | | - |
17 | | -# Includes the autoloader for libraries installed with composer |
18 | | -require __DIR__ . '/../vendor/autoload.php'; |
19 | | - |
20 | | -if (count($argv) != 2) { |
21 | | - return print("Usage: php profanity_filter.php AUDIO_FILE\n"); |
22 | | -} |
23 | | -list($_, $audioFile) = $argv; |
| 15 | +namespace Google\Cloud\Samples\Speech; |
24 | 16 |
|
| 17 | +# [START speech_profanity_filter] |
25 | 18 | use Google\Cloud\Speech\V1\SpeechClient; |
26 | 19 | use Google\Cloud\Speech\V1\RecognitionAudio; |
27 | 20 | use Google\Cloud\Speech\V1\RecognitionConfig; |
28 | 21 | use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; |
29 | 22 |
|
30 | | -/** Uncomment and populate these variables in your code */ |
31 | | -// $audioFile = 'path to an audio file'; |
| 23 | +/** |
| 24 | + * @param string $audioFile path to an audio file |
| 25 | + */ |
| 26 | +function profanity_filter(string $audioFile) |
| 27 | +{ |
| 28 | + // change these variables if necessary |
| 29 | + $encoding = AudioEncoding::LINEAR16; |
| 30 | + $sampleRateHertz = 32000; |
| 31 | + $languageCode = 'en-US'; |
| 32 | + $profanityFilter = true; |
32 | 33 |
|
33 | | -// change these variables if necessary |
34 | | -$encoding = AudioEncoding::LINEAR16; |
35 | | -$sampleRateHertz = 32000; |
36 | | -$languageCode = 'en-US'; |
37 | | -$profanityFilter = true; |
| 34 | + // get contents of a file into a string |
| 35 | + $content = file_get_contents($audioFile); |
38 | 36 |
|
39 | | -// get contents of a file into a string |
40 | | -$content = file_get_contents($audioFile); |
| 37 | + // set string as audio content |
| 38 | + $audio = (new RecognitionAudio()) |
| 39 | + ->setContent($content); |
41 | 40 |
|
42 | | -// set string as audio content |
43 | | -$audio = (new RecognitionAudio()) |
44 | | - ->setContent($content); |
| 41 | + // set config |
| 42 | + $config = (new RecognitionConfig()) |
| 43 | + ->setEncoding($encoding) |
| 44 | + ->setSampleRateHertz($sampleRateHertz) |
| 45 | + ->setLanguageCode($languageCode) |
| 46 | + ->setProfanityFilter($profanityFilter); |
45 | 47 |
|
46 | | -// set config |
47 | | -$config = (new RecognitionConfig()) |
48 | | - ->setEncoding($encoding) |
49 | | - ->setSampleRateHertz($sampleRateHertz) |
50 | | - ->setLanguageCode($languageCode) |
51 | | - ->setProfanityFilter($profanityFilter); |
| 48 | + // create the speech client |
| 49 | + $client = new SpeechClient(); |
52 | 50 |
|
53 | | -// create the speech client |
54 | | -$client = new SpeechClient(); |
| 51 | + # Detects speech in the audio file |
| 52 | + $response = $client->recognize($config, $audio); |
55 | 53 |
|
56 | | -# Detects speech in the audio file |
57 | | -$response = $client->recognize($config, $audio); |
| 54 | + # Print most likely transcription |
| 55 | + foreach ($response->getResults() as $result) { |
| 56 | + $transcript = $result->getAlternatives()[0]->getTranscript(); |
| 57 | + printf('Transcript: %s' . PHP_EOL, $transcript); |
| 58 | + } |
58 | 59 |
|
59 | | -# Print most likely transcription |
60 | | -foreach ($response->getResults() as $result) { |
61 | | - $transcript = $result->getAlternatives()[0]->getTranscript(); |
62 | | - printf('Transcript: %s' . PHP_EOL, $transcript); |
| 60 | + $client->close(); |
63 | 61 | } |
64 | | - |
65 | | -$client->close(); |
66 | | - |
67 | 62 | # [END speech_profanity_filter] |
| 63 | + |
| 64 | +// The following 2 lines are only needed to run the samples |
| 65 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 66 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments