Skip to content

Commit 0482612

Browse files
chore: added a sample that shows the use of the profanity filter (#678)
1 parent 9a9193a commit 0482612

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

speech/profanityFilter.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
'use strict';
16+
17+
function main(gcsUri) {
18+
// [START syncRecognizeWithProfanityFilter]
19+
// Filters profanity
20+
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.
23+
*/
24+
// const gcsUri = 'gs://my-bucket/audio.raw';
25+
26+
async function syncRecognizeWithProfanityFilter() {
27+
// Imports the Google Cloud client library
28+
const speech = require('@google-cloud/speech');
29+
30+
// Creates a client
31+
const client = new speech.SpeechClient();
32+
33+
const audio = {
34+
uri: gcsUri,
35+
};
36+
37+
const config = {
38+
encoding: 'FLAC',
39+
sampleRateHertz: 16000,
40+
languageCode: 'en-US',
41+
profanityFilter: true, // set this to true
42+
};
43+
const request = {
44+
audio: audio,
45+
config: config,
46+
};
47+
48+
// Detects speech in the audio file
49+
const [response] = await client.recognize(request);
50+
const transcription = response.results
51+
.map(result => result.alternatives[0].transcript)
52+
.join('\n');
53+
console.log(`Transcription: ${transcription}`);
54+
}
55+
syncRecognizeWithProfanityFilter().catch(console.error);
56+
// [END syncRecognizeWithProfanityFilter]
57+
}
58+
59+
main(...process.argv.slice(2));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 Google LLC
2+
//
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+
// https://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+
/* eslint-disable */
16+
17+
'use strict';
18+
19+
const {assert} = require('chai');
20+
const {describe, it} = require('mocha');
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
const storageUri = 'gs://cloud-samples-tests/speech/brooklyn.flac';
25+
const text = 'how old is the Brooklyn Bridge';
26+
27+
describe('profanityFilter', () => {
28+
it('should run profanityFilter', async () => {
29+
const stdout = execSync(`node profanityFilter.js ${storageUri}`)
30+
assert.match(stdout, /Transcription:/ );
31+
});
32+
});

0 commit comments

Comments
 (0)