mirror of
https://github.com/donl/plivo-node.git
synced 2026-05-25 22:07:10 -06:00
Added ssml.
This commit is contained in:
parent
abed1ca604
commit
809fa5bcf3
6 changed files with 179 additions and 51 deletions
12
TODO
Normal file
12
TODO
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
1. Polly.*
|
||||
|
||||
2. How to validate languages with voice
|
||||
|
||||
3. Male female voices for polly+voice combination
|
||||
|
||||
4. Promise, chain will break
|
||||
|
||||
5. Will childs be there in ssml for validation?
|
||||
|
||||
6.
|
||||
|
|
@ -3,6 +3,7 @@ import _snakeCase from 'lodash/snakeCase';
|
|||
import _mapKeys from 'lodash/mapKeys';
|
||||
import _mapValues from 'lodash/mapValues';
|
||||
import _map from 'lodash/map';
|
||||
import { parseString } from 'xml2js';
|
||||
|
||||
function recursivelyRenameObject(object, renameFunc) {
|
||||
if (!(object instanceof Object)) {
|
||||
|
|
@ -54,38 +55,114 @@ export function camelCaseRequestWrapper(requestFunc) {
|
|||
}
|
||||
}
|
||||
|
||||
export function validateVoiceForSsml(voice, language) {
|
||||
export function validateVoiceForSsml(content, voice, language) {
|
||||
|
||||
var voiceParts = voice.split('.');
|
||||
if (['MAN', 'WOMAN'].indexOf(voice) != -1) {
|
||||
return { success: true };
|
||||
} else if (voiceParts.length != 2 || voiceParts[0] != 'Polly') {
|
||||
return {
|
||||
success: false, msg: "Invalid voice " + voice + '.'
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
||||
var voiceParts = voice.split('.');
|
||||
if (['MAN', 'WOMAN'].indexOf(voice) != -1) {
|
||||
resolve({ success: true });
|
||||
return;
|
||||
} else if (voiceParts.length != 2 || voiceParts[0] != 'Polly') {
|
||||
resolve({
|
||||
success: false, msg: "Invalid voice " + voice + '.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// If voice is polly, validate accent based on language specified
|
||||
|
||||
// List of valid Polly voices
|
||||
var languageWiseVoices = {
|
||||
'Australian English': ['Nicole', 'Russell'],
|
||||
'Brazilian Portuguese': ['Vitória', 'Ricardo'],
|
||||
'Canadian French': ['Chantal'],
|
||||
'Canadian French': ['Chantal'],
|
||||
'Danish': ['Naja', 'Mads'],
|
||||
'Dutch': ['Lotte', 'Ruben'],
|
||||
'French': ['Léa', 'Céline', 'Mathieu'],
|
||||
'German': ['Vicki', 'Hans'],
|
||||
'Hindi': ['Aditi'],
|
||||
'Icelandic': ['Dóra', 'Karl'],
|
||||
'Indian English': ['Raveena ', 'Aditi'],
|
||||
'Italian': ['Carla', 'Giorgio'],
|
||||
'Japanese': ['Mizuki', 'Takumi'],
|
||||
'Korean': ['Seoyeon'],
|
||||
'Mandarin Chinese': ['Zhiyu'],
|
||||
'Norwegian': ['Liv'],
|
||||
'Polish': ['Ewa', 'Maja', 'Jacek', 'Jan'],
|
||||
'Portuguese-Iberic': ['Inês', 'Cristiano'],
|
||||
'Romanian': ['Carmen'],
|
||||
'Russian': ['Tatyana', 'Maxim'],
|
||||
'Spanish-Castilian': ['Conchita', 'Enrique'],
|
||||
'Swedish': ['Astrid'],
|
||||
'Turkish': ['Filiz'],
|
||||
'UK English': ['Amy', 'Emma', 'Brian'],
|
||||
'US English': ['Joanna', 'Salli', 'Kendra', 'Kimberly', 'Ivy', 'Matthew', 'Justin', 'Joey'],
|
||||
'US Spanish': ['Penélope', 'Miguel'],
|
||||
'Welsh': ['Gwyneth'],
|
||||
'Welsh English': ['Geraint'],
|
||||
};
|
||||
}
|
||||
|
||||
// If voice is polly, validate accent based on language specified
|
||||
// Validate supported languages.
|
||||
if (!languageWiseVoices[language]) {
|
||||
resolve({
|
||||
success: false, msg: "Invalid language."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// List of valid Polly voices
|
||||
var languageWiseVoices = {
|
||||
'Australian English': ['Nicole', 'Russell'],
|
||||
'Brazilian Portuguese': ['Vitória', 'Ricardo']
|
||||
};
|
||||
// Validate voice for given language.
|
||||
if (languageWiseVoices[language].indexOf(voiceParts[1]) == -1) {
|
||||
console.log('==><? thrown here');
|
||||
resolve({
|
||||
success: false,
|
||||
msg: "XML Validation Error: <Speak> voice ‘" + voice + "’ is not valid. Refer <link> for list of supported voices."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate supported languages.
|
||||
if (!languageWiseVoices[language]) {
|
||||
return {
|
||||
success: false, msg: "Invalid language."
|
||||
};
|
||||
}
|
||||
// Validate xml now
|
||||
var contenxtXml = "<root>" + content + "</root>";
|
||||
parseString(contenxtXml, function (err, xmlJson) {
|
||||
if (err || !xmlJson) {
|
||||
// console.log('XMl parsing error validation result', err);
|
||||
resolve({
|
||||
success: false,
|
||||
msg: "Invalid SSML xml structure. Content must be a valid xml. Parsing Error: " + err
|
||||
})
|
||||
}
|
||||
xmlJson = xmlJson.root;
|
||||
|
||||
// Validate voice for given language.
|
||||
if (languageWiseVoices[language].indexOf(voiceParts[1]) == -1) {
|
||||
return {
|
||||
success: false, msg: "XML Validation Error: <Speak> voice ‘" + voice + "’ is not valid. Refer <link> for list of supported voices."
|
||||
};
|
||||
}
|
||||
// Validate supported ssml tags
|
||||
var validXmlTags = ['break', 'emphasis', 'lang', 'p', 'phoneme', 'prosody', 's', 'say-as', 'sub', 'w'];
|
||||
|
||||
var invalidXmlTag = null;
|
||||
for (var key in xmlJson) {
|
||||
// console.log('xml json key', key);
|
||||
if (key !== '_' && key !== '$') {
|
||||
if (validXmlTags.indexOf(key) == -1) {
|
||||
invalidXmlTag = key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (invalidXmlTag) {
|
||||
resolve({
|
||||
success: false,
|
||||
msg: "SSML tag `" + invalidXmlTag + "` is not supported."
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
||||
resolve({
|
||||
success: true
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
16
package-lock.json
generated
16
package-lock.json
generated
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "plivo",
|
||||
"version": "4.0.5",
|
||||
"version": "4.1.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
@ -4669,6 +4669,11 @@
|
|||
"integrity": "sha512-1HwIYD/8UlOtFS3QO3w7ey+SdSDFE4HRNLZoZRYVQefrOY3l17epswImeB1ijgJFQJodIaHcwkp3r/myBjFVbg==",
|
||||
"dev": true
|
||||
},
|
||||
"sax": {
|
||||
"version": "1.2.4",
|
||||
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
|
||||
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
|
||||
},
|
||||
"semver": {
|
||||
"version": "4.3.6",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz",
|
||||
|
|
@ -5565,6 +5570,15 @@
|
|||
"mkdirp": "^0.5.1"
|
||||
}
|
||||
},
|
||||
"xml2js": {
|
||||
"version": "0.4.19",
|
||||
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz",
|
||||
"integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==",
|
||||
"requires": {
|
||||
"sax": ">=0.6.0",
|
||||
"xmlbuilder": "~9.0.1"
|
||||
}
|
||||
},
|
||||
"xmlbuilder": {
|
||||
"version": "9.0.7",
|
||||
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz",
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@
|
|||
"request": "^2.81.0",
|
||||
"uri-parser": "^1.0.0",
|
||||
"utf8": "^2.1.2",
|
||||
"xml2js": "^0.4.19",
|
||||
"xmlbuilder": "^9.0.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
test/ssml.js
21
test/ssml.js
|
|
@ -5,8 +5,21 @@ import { PlivoGenericResponse } from '../lib/base.js';
|
|||
|
||||
let client = new Client('sampleid', 'sammpletoken', 'sampleproxy');
|
||||
|
||||
let response = new Response();
|
||||
let speak_body = "Hello, world!";
|
||||
response.addSpeak(speak_body, { voice: 'SRD' });
|
||||
console.log(response.toXML());
|
||||
describe('SsmlInterface', function () {
|
||||
it('test', function () {
|
||||
|
||||
let response = new Response();
|
||||
let speak_body = ' Here is a number <w role="amazon: VBD">read</w> \
|
||||
as a cardinal number: \
|
||||
<say-ass interpret-as="cardinal">12345</say-ass>. \
|
||||
Here is a word spelled out: \
|
||||
<say-as interpret-as="spell-out">hello</say-as>.';
|
||||
|
||||
// response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' });
|
||||
response.addSpeak(speak_body, { language: 'Spanish-Castilian', voice: 'Polly.Conchita' });
|
||||
console.log(response.toXML());
|
||||
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue