Skip to content
Permalink
Browse files Browse the repository at this point in the history
Added function to replace characters with space and use it to sanitiz…
…e text

Change-Id: I3d14230444fefa0ce739c498eee6d100bcfe276a
  • Loading branch information
pasindud committed Sep 24, 2018
1 parent 3a449a3 commit c145d46
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
13 changes: 5 additions & 8 deletions merlin_model_server/api.js
Expand Up @@ -33,14 +33,9 @@ router.get('/alignment', (req, res) => res.status(200).send([]));
/** GET synthesize voice based on a voice model */
router.get('/tts', (req, res) => {
const { text, type } = req.query;
// TODO(twattanavekin): Test Unicode text.
const escapedText = JSON.stringify(text).slice(1, -1);
const sanitizedText = utils.replaceCharactersWithSpaces(text);

// Example command:
// echo "hello" | ${SYNTH_SCRIPT}

const cmd = `echo "${escapedText}" | ${SYNTH_SCRIPT}`;
console.log(`Running command - \n${cmd}`);
console.log(`Synthesizing ${sanitizedText}`);

const options = {
maxBuffer: 1024 * 1000, // 1 mb buffer
Expand All @@ -49,7 +44,7 @@ router.get('/tts', (req, res) => {
encoding: 'buffer',
};

exec(cmd, options, (err, stdout, stderr) => {
const child = exec(SYNTH_SCRIPT, options, (err, stdout, stderr) => {
const errMsg = utils.getExecErrorMessage(err, stderr);
if (errMsg) {
return res.status(500).send(errMsg);
Expand All @@ -59,6 +54,8 @@ router.get('/tts', (req, res) => {
const data = type === 'base64' ? stdout.toString('base64') : stdout;
return res.send(data);
});
child.stdin.write(sanitizedText);
child.stdin.end();
});

module.exports = router;
4 changes: 3 additions & 1 deletion merlin_model_server/jasmine.json
@@ -1,6 +1,8 @@
{
"spec_dir": "tests",
"spec_files": [],
"spec_files": [
"UtilsTests.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
24 changes: 24 additions & 0 deletions merlin_model_server/settings.js
@@ -0,0 +1,24 @@
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Module containing settings for merlin server.
*/
const settings = {
// Merlin synthesis pipeline might not handle these characters as expected
// and also they are typically not needed during tts synthesis.
// Therefore we replace them with spaces.
CHARACTERS_TO_REPLACE_WITH_SPACES: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~',
};
module.exports = settings;
43 changes: 43 additions & 0 deletions merlin_model_server/tests/UtilsTests.js
@@ -0,0 +1,43 @@
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const utils = require('../utils');

describe('Testing utils replaceCharactersWithSpaces method', () => {
it('The function should return characters replaced with spaces and trim', () => {
const testData = [
['`<text>`', 'text'],
['`text`', 'text'],
['$(text)', 'text'],
['"\$(text\)', 'text'],
['\"\$(text\)', 'text'],
['${{{text}', 'text'],
['\$\{text\}', 'text'],
['\${text"}', 'text'],
['"${{text"}', 'text'],
['"${text"}', 'text'],
["\"ශ්රී ලංකාව'\"", 'ශ්රී ලංකාව'],
['"$text"', 'text'],
['$text', 'text'],
[",./;'[]*\-=", ''],
['echo `<text>`', 'echo text'],
['echo $("text")', 'echo text'],
['echo \$\{text\}', 'echo text'],
];

testData.forEach(function(entry) {
expect(utils.replaceCharactersWithSpaces(entry[0])).toEqual(entry[1]);
});
});
});
13 changes: 13 additions & 0 deletions merlin_model_server/utils.js
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const settings = require('./settings');

/**
* Returns a error message for the given parameters.
* @param {String} err error from the exec call.
Expand All @@ -27,7 +29,18 @@ const getExecErrorMessage = (err, stderr) => {
return errMsg;
};

/**
* Returns the original string with characters
* specified in settings replaced with spaces.
* @text {String} text to be replaced.
*/
const replaceCharactersWithSpaces = (text) => {
const re = new RegExp(`[${settings.CHARACTERS_TO_REPLACE_WITH_SPACES}]`, 'g');
const newText = text.replace(re, ' ').trim();
return newText;
};

module.exports = {
getExecErrorMessage,
replaceCharactersWithSpaces,
};

0 comments on commit c145d46

Please sign in to comment.