Skip to content

Commit 56e90d3

Browse files
feat: added a profanity filter sample (GoogleCloudPlatform#1236)
1 parent 6ff5735 commit 56e90d3

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

speech/src/profanity_filter.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
# Copyright 2020 Google LLC
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START 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;
24+
25+
use Google\Cloud\Speech\V1\SpeechClient;
26+
use Google\Cloud\Speech\V1\RecognitionAudio;
27+
use Google\Cloud\Speech\V1\RecognitionConfig;
28+
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
29+
30+
/** Uncomment and populate these variables in your code */
31+
// $audioFile = 'path to an audio file';
32+
33+
// change these variables if necessary
34+
$encoding = AudioEncoding::LINEAR16;
35+
$sampleRateHertz = 32000;
36+
$languageCode = 'en-US';
37+
$profanityFilter = true;
38+
39+
// get contents of a file into a string
40+
$content = file_get_contents($audioFile);
41+
42+
// set string as audio content
43+
$audio = (new RecognitionAudio())
44+
->setContent($content);
45+
46+
// set config
47+
$config = (new RecognitionConfig())
48+
->setEncoding($encoding)
49+
->setSampleRateHertz($sampleRateHertz)
50+
->setLanguageCode($languageCode)
51+
->setProfanityFilter($profanityFilter);
52+
53+
// create the speech client
54+
$client = new SpeechClient();
55+
56+
# Detects speech in the audio file
57+
$response = $client->recognize($config, $audio);
58+
59+
# Print most likely transcription
60+
foreach ($response->getResults() as $result) {
61+
$transcript = $result->getAlternatives()[0]->getTranscript();
62+
printf('Transcript: %s' . PHP_EOL, $transcript);
63+
}
64+
65+
$client->close();
66+
67+
# [END profanity_filter]

speech/src/profanity_filter_gcs.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
# Copyright 2020 Google LLC
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START profanity_filter_gcs]
16+
# Includes the autoloader for libraries installed with composer
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
// Include Google Cloud dependendencies using Composer
20+
require_once __DIR__ . '/../vendor/autoload.php';
21+
22+
if (count($argv) != 2) {
23+
return print("Usage: php profanity_filter_gcs.php AUDIO_FILE\n");
24+
}
25+
list($_, $audioFile) = $argv;
26+
27+
use Google\Cloud\Speech\V1\SpeechClient;
28+
use Google\Cloud\Speech\V1\RecognitionAudio;
29+
use Google\Cloud\Speech\V1\RecognitionConfig;
30+
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
31+
32+
/** The Cloud Storage object to transcribe */
33+
// $uri = 'The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)';
34+
35+
// change these variables if necessary
36+
$encoding = AudioEncoding::LINEAR16;
37+
$sampleRateHertz = 32000;
38+
$languageCode = 'en-US';
39+
$profanityFilter = true;
40+
41+
// set string as audio content
42+
$audio = (new RecognitionAudio())
43+
->setUri($audioFile);
44+
45+
// set config
46+
$config = (new RecognitionConfig())
47+
->setEncoding($encoding)
48+
->setSampleRateHertz($sampleRateHertz)
49+
->setLanguageCode($languageCode)
50+
->setProfanityFilter($profanityFilter);
51+
52+
// create the speech client
53+
$client = new SpeechClient();
54+
55+
# Detects speech in the audio file
56+
$response = $client->recognize($config, $audio);
57+
58+
# Print most likely transcription
59+
foreach ($response->getResults() as $result) {
60+
$transcript = $result->getAlternatives()[0]->getTranscript();
61+
printf('Transcript: %s' . PHP_EOL, $transcript);
62+
}
63+
64+
$client->close();
65+
66+
# [END profanity_filter_gcs]

speech/test/speechTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testTranscribe($command, $audioFile, $requireGrpc = false)
7676
if ($requireGrpc && !extension_loaded('grpc')) {
7777
self::markTestSkipped('Must enable grpc extension.');
7878
}
79-
if (!self::$bucketName && in_array($command, ['transcribe_gcs', 'transcribe_async_gcs'])) {
79+
if (!self::$bucketName && in_array($command, ['transcribe_gcs', 'transcribe_async_gcs', 'profanity_filter_gcs'])) {
8080
$this->requireEnv('GOOGLE_STORAGE_BUCKET');
8181
}
8282
$output = $this->runSnippet($command, [$audioFile]);
@@ -98,6 +98,8 @@ public function provideTranscribe()
9898
['transcribe_async', __DIR__ . '/data/audio32KHz.raw'],
9999
['transcribe_async_gcs', 'gs://' . self::$bucketName . '/speech/audio32KHz.raw'],
100100
['transcribe_async_words', __DIR__ . '/data/audio32KHz.raw'],
101+
['profanity_filter_gcs', 'gs://' . self::$bucketName . '/speech/audio32KHz.raw'],
102+
['profanity_filter', __DIR__ . '/data/audio32KHz.raw'],
101103
['streaming_recognize', __DIR__ . '/data/audio32KHz.raw', true],
102104
];
103105
}

0 commit comments

Comments
 (0)