Git Product home page Git Product logo

ssml-check's Introduction

SSML-Check

SSML-Check will verify that a given input is valid SSML. It is built on top of ssml-check-core, which provides core syntax validation of SSML. This library extends this functionality by looking into files accessed by the input SSML such as audio files to make sure they conform with platform expectations.

Usage

This library exposes two functions which allow you to check and optionally correct a given SSML string

Check

The first is check which verifies whether the given input is a valid SSML string on either the Amazon Alexa or Google Assistant platform (or both). This function returns a Promise with an array of errors indicating how the input fails validation, or a Promise of undefined if there are no errors.

check(ssml, options)

The arguments to this function are:

  • ssml - The SSML to check
  • options - Options for evaluating the SSML as noted below

The options structure is composed of the following fields with the following default values:

{
  platform:all,              // The voice platform to evaluate this SSML against.
                             // Valid values are "all", "amazon", or "google".
  locale:undefined,          // The locale you want to check against, used for certain
                             // locale-specific attributes like amazon:emotion
  validateAudioFiles:false,  // Whether to validate audio files against
                             // acceptable formats (bit rate, sample rate, etc)
  getPositions:false,        // If set, the index of the tag will be returned as the position
                             // field within the error object
  unsupportedTags:undefined, // An array of tags that will be flagged as invalid
                             // For example, ['prosody']
}

The return value is a Promise resolving to an array of errors that were encountered in processing the SSML, or undefined if no errors were encountered. The format of each error object is as follows:

{
  type,       // The type of error encountered ("tag," "audio" or a specific error)
  tag,        // The tag that had an error (set if type is "tag")
  attribute,  // The attribute that had an error (set if type is "tag")
  value,      // The attribute value that was in error (set if type is "tag" or "audio")
  detail,     // Further details about the error (set if type is "audio")
  position,   // The position of the start of the tag within the input string (set if getPositions is true)
}

The current version of ssml-check will check for the following:

  • Valid XML format
  • All tags are valid tags for their platform with valid attributes and values
  • No more than five audio tags in the response
  • Note invalid & character
  • Valid audio file format (HTTPS, MP3, length, bit rate, sample rate), if validateAudioFiles is set

Examples

const ssmlCheck = require('ssml-check');
ssmlCheck.check('<speak><prosody rate="5%">Hello world</prosody></speak>')
.then((errors) => {
  if (errors) {
    console.log(JSON.stringify(errors));
  } else {
    console.log('SSML is clean');
  }
});

will output [{"type":"tag","tag":"prosody","attribute":"rate","value":"5%"}]

const ssmlCheck = require('ssml-check');
ssmlCheck.check('<speak><audio src="https://foo.mp3"/></speak>', {validateAudioFiles: true})
.then((errors) => {
  if (errors) {
    console.log(JSON.stringify(errors));
  } else {
    console.log('SSML is clean');
  }
});

will output [{"type":"audio","value":"https://foo.mp3","detail":"Can't access file"}]

verifyAndFix

The second function is verifyAndFix which returns a Promise of an object containing an array of caught SSML errors (similar to check) and, if possible, corrected SSML as noted below.

verifyAndFix(ssml, options)

The arguments to this function, including the options structure, are the same as for check.

The return value is a Promise resolving to an object with the following fields:

{
  fixedSSML,  // A fixed SSML string if errors are found that can be corrected for
              // This field will be undefined if the SSML cannot be corrected
  errors,     // An array of errors. The format of each object in this array is as
              // defined above for the check function. This field is undefined
              // if there are no errors.    
}

If there are no errors, then the Promise will contain an empty object.

The current version of ssml-check will correct the following errors:

  • If more than five audio tags are in the response, elements after the first five are removed
  • If an invalid audio file is found when the validateAudioFiles option is set, the element will be removed
  • If an invalid tag is found, the element will be removed
  • If an invalid attribute is found, it will be removed (in the case of the src attribute for audio, if this is missing or invalid the element will be removed)
  • If an invalid value is found for an attribute within a valid tag, the value will be corrected as best possible. For example, adding a leading + to values that require it like prosody's pitch attribute, adjusting the value to be within an acceptable range, or substituting a default value if necessary

Examples

const ssmlCheck = require('ssml-check');
ssmlCheck.verifyAndFix('<speak><audio src="https://foo.mp3"/>This & that</speak>', {validateAudioFiles: true})
.then((result) => {
  if (result.fixedSSML) {
    console.log(result.fixedSSML);
  } else if (result.errors) {
    console.log(JSON.stringify(result.errors));
  } else {
    console.log('SSML is clean');
  }
});

will output <speak>This &amp; that</speak>

const ssmlCheck = require('ssml-check');
ssmlCheck.verifyAndFix('<speak><tag><prosody rate="60">Hello world</prosody></tag></speak>')
.then((result) => {
  if (result.fixedSSML) {
    console.log(result.fixedSSML);
  } else if (result.errors) {
    console.log(JSON.stringify(result.errors));
  } else {
    console.log('SSML is clean');
  }
});

will output <speak><prosody rate="60%">Hello world</prosody></speak>

Contributions

We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:

  • Reporting a bug
  • Discussing the current state of the code
  • Submitting a fix
  • Proposing new features

When contributing to this repository, please first discuss the change you wish to make by raising an issue or sending an e-mail with to the owners of this repository.

ssml-check's People

Contributors

gsdriver avatar armonge avatar freisms avatar

Stargazers

zoffy avatar Shibu Barman avatar MwP avatar Martin Hassman avatar Shikhir Arora avatar Kevin avatar lowki avatar Devin Ozel avatar Johannes Waigel avatar Maik Hummel avatar Jan König avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

ssml-check's Issues

say-as - format property is not correctly validated on the Google-platform

According to google documenation:

The format attribute is a sequence of time field character codes. Supported field character codes in format are {h,m, s, Z, 12, 24} for hour, minute (of the hour), second (of the minute), time zone, 12-hour time, and 24-hour time respectively. If the field code appears once for hour, minute, or second then the number of digits expected are 1, 2, and 2 respectively. If the field code is repeated then the number of expected digits is the number of times the code is repeated. Fields in the time text may be separated by punctuation and/or spaces. If hour, minute, or second are not specified in the format or there are no matching digits then the field is treated as a zero value. The default format is "hms12".

The validation only does allow some of the possibilities offered by Google:

} else if (attribute === 'format') {
              if (['mdy', 'dmy', 'ymd', 'md', 'dm', 'ym',
                  'my', 'd', 'm', 'y'].indexOf(element.attributes.format) === -1) {
                errors.push(createTagError(element, attribute));
}

Audio check only supports MP3 file

Describe the bug
Audio check currently only works for MP3 files, not for OGG files as supported on Google

To Reproduce
This SSML should be valid when validateAudioFiles is true and platform is google:


a cat purring
PURR (sound didn't load)

Environment (please complete the following information):

  • Platform: Google
  • Version 0.2.3

Split audio check into separate library

Audio check comes with some additional libraries dependencies. Consider whether this should be split into a different two libraries, e.g. create ssml-check-core that doesn't perform audio checks (and which ssml-check could do audio checks on top of)

speed-Attribute validation appears to be wrong

According to the google documentation the values are followed by "%", which isn't valid with the given regex:

Documentation:

The ratio output playback rate relative to the normal input rate expressed as a percentage. The format is a positive Real Number followed by %. The currently supported range is [50% (slow - half speed), 200% (fast - double speed)]. Values outside that range may (or may not) be adjusted to be within it.

Validation:

} else if ((platform === 'google') && (attribute === 'speed')) {
              if (!element.attributes.speed.match(/^(\+)?[0-9]+(\.[0-9]+)?$/g)) {
                errors.push(createTagError(element, attribute));
}

Maybe try something like ^\\d+% instead.

Typescript declaration files

Is your feature request related to a problem? Please describe.
I've been using ssml-check with a couple typescript project and while it works great the fact that there's no declaration files means a couple of niceties about typescript like getting options autocompletion in IDE and stuff are lost

Describe the solution you'd like
I would like to contribute a typescript declaration file in the form of a an index.d.ts and a change in the package.json file to reference this types

Describe alternatives you've considered
Alternatively i could also publish a new package with the name @types/ssml-check to http://definitelytyped.org/ but i believe it would be best to have the typescript declarations in the same package

Additional context
https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
Example: dynamodb-local package
https://github.com/rynop/dynamodb-local/blob/master/package.json#L37
https://github.com/rynop/dynamodb-local/blob/master/index.d.ts

Specific SSML tags are not working.

Describe the bug
Specific SSML tags are not working like amazon:domain, amazon:breath.
eg:
<amazon:domain name=""conversational"">Hi! Hello World</amazon:domain>
<amazon:breath duration="medium" volume="x-loud"/> Agents are assisting other callers

  1. Since we are not using locale id in our scenario, its always entering into the if condition of locale id in amazon domain function.
  2. In ssml-check-core/index.js, we have an if constraint for creating an element.name. But this condition is failing for amazon:domain and amazon:breath tag due to not condition. ( !(((platform === 'amazon') && (validAmazonTags.indexOf(element.name) !== -1)))
    we tried by removing that NOT condition, it worked. but it failed in above mentioned scenario 1.

To Reproduce
<amazon:domain name=""conversational"">Hi! Hello World</amazon:domain>
<amazon:breath duration="medium" volume="x-loud"/> Agents are assisting other callers

Expected behavior

  1. Since we are not using locale id in our scenario, its always entering into the if condition of locale id in amazon domain function.
  2. In ssml-check-core/index.js, we have an if constraint for creating an element.name. But this condition is failing for amazon:domain and amazon:breath tag due to not condition. ( !(((platform === 'amazon') && (validAmazonTags.indexOf(element.name) !== -1)))
    we tried by removing that NOT condition, it worked. but it failed in above mentioned scenario 1.

Environment (please complete the following information):

  • Platform: Amazon
  • Version 0.4.4(ssml-check)

Additional context
Add any other context about the problem here.

Add fix functionality

Add checkAndFix function which would, to the extent possible, correct an invalid SSML response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.