Skip to main content

Templates (rich transcript)

The template functionality allows that any audio transcription content to be enriched with extra content and made available through the richTranscript property. Templates are similar to Iara commands, but the focus is in the replacement (or enrichment) of content.

Basic operation

In general lines, a template is nothing more than a filter that transform a content of e.detail.transcript property to the e.detail.richTranscript property of the recognition result object.

This content transformation process is basically search for a string and replace it for another. The search is performed on e.detail.transcript, but the result of the replacement is placed on e.detail.richTranscript.

In this way, the template is compound by two main parts:

  • Activation text/command (string to be searched)
  • Content (string that will replace the activation text/command)

By default, any activation text/command will be prefixed internally by the string "iara texto". So the template whose activation text/command is "teste" will take action if the user speaks iara texto teste.

Tip: templates are, essentially, strings search and replacement functions, where the search is done in e.detail.transcript and the replacement result is placed in e.detail.richTranscript.

Templates can be highly customized, from the commands prefix as the form that the content is changed. The following sections demonstrate each one of these functionalities.

Basic usage

Templates are added through the recognizer property richTranscriptTemplates:

var recognition = new IaraSpeechRecognition();

var cmd = 'teste';
var content = 'conteúdo';

// Adds templates
recognition.richTranscriptTemplates.add(cmd, content);

recognition.init({
// ...
}).done(function(e) {
// Assuming that the user said "iara texto teste fígado",
// the following will be produced.
recognition.onresult = function(event) {
console.log(e.detail.transcript); // "iara texto teste fígado"
console.log(e.detail.richTranscript); // "conteúdo fígado"
}
});

In the example above, when the user say iara texto teste, this same string that exists in the property e.detail.transcript will be replaced by "conteúdo" in the property e.detail.richTranscript.

The content of a template will not be sanitized or changed before its usage, so it can even contain HTML tags:

var recognition = new IaraSpeechRecognition();

// Adds templates
recognition.richTranscriptTemplates.add(
'vias biliares',
'<em>vias biliares</em> foi <strong>enriquecido</strong>'
);

recognition.init({
// ...
}).done(function(e) {
// Assuming that the user said "iara texto vias biliares",
// the following will be produced.
recognition.onresult = function(event) {
console.log(e.detail.transcript); // "iara texto vias biliares"
console.log(e.detail.richTranscript); // "<em>vias biliares</em> foi <strong>enriquecido</strong>"
}
});

Activation callback

Can be detected if a template was activated or not through a callback passed as parameter of add() method:


function myCallback(resultEvent, context) {
console.log('Template "dilatação" invocado.');
console.log('Resultado do template "dilatação": ', resultEvent);
console.log('Contexto do template "dilatação": ', context);
}

var recognition = new IaraSpeechRecognition();

// Adiciona template com callback
recognition.richTranscriptTemplates.add('dilatação', 'Não há dilatação', myCallback);

recognition.init({
// ...
}).done(function(e) {
// Assuming that the user said "iara texto dilatação",
// the following will be produced.
recognition.onresult = function(event) {
console.log(e.detail.transcript); // "iara texto dilatação"
console.log(e.detail.richTranscript); // "Não há dilatação"
}
});

In the example above, the command "iara texto dilatação" activates the temple that will replace this string by "Não há dilatação". Additionally, when a template be activated, the callback passed as third parameter will be invoked.

The callback will receive as parameters the object resulting from the current transcription (object event of the onresult in the example above), and a parameter that contains the information about the context of the template being executed.

The parameter context received by the callback contains a lot of information about the template and the transcription. In the above example case, these would be its property and values:

// console.log(context)
{
// Text that will be searched in `transcript`
findText: 'iara texto dilatação',

// Text that will replace `findText`, if found.
replaceText: 'Não há dilatação',

// `richTranscript` property content without
// any template being applied.
originalRichTranscript: 'iara texto dilatação',

// Template extra configuration object.
config: {},

// reference to the callback function
callback: Function
}