Skip to content

Commit

Permalink
Added a new option 'rnl' to MRCPRecog() and SynthAndRecog() allowing …
Browse files Browse the repository at this point in the history
…to replace new lines in the NLSML instance

The option applies to the output produced by RECOG_INSTANCE() and specifies a character to replace the new lines with. Usage example:

"spl=en-US&plt=1&b=1&sct=1000&sint=15000&nit=10000&rnl=."
  • Loading branch information
achaloyan committed May 18, 2023
1 parent 294c1b6 commit f16eacb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
15 changes: 15 additions & 0 deletions app-unimrcp/app_datastore.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ app_session_t* app_datastore_session_add(app_datastore_t* app_datastore, const c
session->nlsml_result = NULL;
session->stop_barged_synth = FALSE;
session->instance_format = NLSML_INSTANCE_FORMAT_XML;
session->replace_new_lines = 0;
ast_log(LOG_DEBUG, "Add entry %s to datastore on %s\n", entry, ast_channel_name(app_datastore->chan));
apr_hash_set(app_datastore->session_table, entry, APR_HASH_KEY_STRING, session);
}
Expand Down Expand Up @@ -553,6 +554,17 @@ static const apr_xml_elem* recog_instance_find_elem(const apr_xml_elem *elem, co
return NULL;
}

static int recog_instance_replace_char(char *str, char find_char, char replace_char)
{
char *ptr = str;
int n = 0;
while ((ptr = strchr(ptr, find_char)) != NULL) {
*ptr++ = replace_char;
n++;
}
return n;
}

/* Helper function used to process XML data in NLSML instance */
static int recog_instance_process_xml(app_session_t *app_session, nlsml_instance_t *instance, const char *path, const char **text)
{
Expand Down Expand Up @@ -700,6 +712,9 @@ static int recog_instance(struct ast_channel *chan, const char *cmd, char *data,
return -1;

ast_copy_string(buf, text, len);
if (app_session->replace_new_lines) {
recog_instance_replace_char(buf, '\n', app_session->replace_new_lines);
}
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions app-unimrcp/app_datastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct app_session_t {
nlsml_result_t *nlsml_result; /* parsed NLSML result */
apt_bool_t stop_barged_synth; /* whether or not to always stop barged synthesis request */
enum nlsml_instance_format instance_format; /* NLSML instance format */
char replace_new_lines; /* replace new lines in NLSML instance */
};

typedef struct app_session_t app_session_t;
Expand Down
19 changes: 17 additions & 2 deletions app-unimrcp/app_mrcprecog.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<option name="dse"> <para>Datastore entry.</para></option>
<option name="vsp"> <para>Vendor-specific parameters.</para></option>
<option name="nif"> <para>NLSML instance format (either "xml" or "json") used by RECOG_INSTANCE().</para></option>
<option name="rnl"> <para>Replace new lines (0: disabled, otherwise: the character to replace new lines with) used by RECOG_INSTANCE().</para></option>
</optionlist>
</parameter>
</syntax>
Expand Down Expand Up @@ -160,7 +161,8 @@ enum mrcprecog_option_flags {
MRCPRECOG_INPUT_TIMERS = (1 << 8),
MRCPRECOG_PERSISTENT_LIFETIME = (1 << 9),
MRCPRECOG_DATASTORE_ENTRY = (1 << 10),
MRCPRECOG_INSTANCE_FORMAT = (1 << 11)
MRCPRECOG_INSTANCE_FORMAT = (1 << 11),
MRCPRECOG_REPLACE_NEW_LINES = (1 << 12)
};

/* The enumeration of option arguments. */
Expand All @@ -177,9 +179,10 @@ enum mrcprecog_option_args {
OPT_ARG_PERSISTENT_LIFETIME = 9,
OPT_ARG_DATASTORE_ENTRY = 10,
OPT_ARG_INSTANCE_FORMAT = 11,
OPT_ARG_REPLACE_NEW_LINES = 12,

/* This MUST be the last value in this enum! */
OPT_ARG_ARRAY_SIZE = 12
OPT_ARG_ARRAY_SIZE = 13
};

/* The enumeration of plocies for the use of input timers. */
Expand Down Expand Up @@ -951,6 +954,9 @@ static int mrcprecog_option_apply(mrcprecog_options_t *options, const char *key,
} else if (strcasecmp(key, "nif") == 0) {
options->flags |= MRCPRECOG_INSTANCE_FORMAT;
options->params[OPT_ARG_INSTANCE_FORMAT] = value;
} else if (strcasecmp(key, "rnl") == 0) {
options->flags |= MRCPRECOG_REPLACE_NEW_LINES;
options->params[OPT_ARG_REPLACE_NEW_LINES] = value;
} else {
ast_log(LOG_WARNING, "Unknown option: %s\n", key);
}
Expand Down Expand Up @@ -1235,6 +1241,15 @@ static int app_recog_exec(struct ast_channel *chan, ast_app_data data)
}
}

/* Check whether new lines shall be replaced */
if ((mrcprecog_options.flags & MRCPRECOG_REPLACE_NEW_LINES) == MRCPRECOG_REPLACE_NEW_LINES) {
if (!ast_strlen_zero(mrcprecog_options.params[OPT_ARG_REPLACE_NEW_LINES])) {
char ch = *mrcprecog_options.params[OPT_ARG_REPLACE_NEW_LINES];
ast_log(LOG_DEBUG, "(%s) Replace new lines: %c\n", name, ch);
app_session->replace_new_lines = ch;
}
}

const char *grammar_delimiters = ",";
/* Get grammar delimiters. */
if ((mrcprecog_options.flags & MRCPRECOG_GRAMMAR_DELIMITERS) == MRCPRECOG_GRAMMAR_DELIMITERS) {
Expand Down
19 changes: 17 additions & 2 deletions app-unimrcp/app_synthandrecog.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<option name="sbs"> <para>Always stop barged synthesis request.</para></option>
<option name="vsp"> <para>Vendor-specific parameters.</para></option>
<option name="nif"> <para>NLSML instance format (either "xml" or "json") used by RECOG_INSTANCE().</para></option>
<option name="rnl"> <para>Replace new lines (0: disabled, otherwise: the character to replace new lines with) used by RECOG_INSTANCE().</para></option>
</optionlist>
</parameter>
</syntax>
Expand Down Expand Up @@ -138,7 +139,8 @@ enum sar_option_flags {
SAR_PERSISTENT_LIFETIME = (1 << 7),
SAR_DATASTORE_ENTRY = (1 << 8),
SAR_STOP_BARGED_SYNTH = (1 << 9),
SAR_INSTANCE_FORMAT = (1 << 10)
SAR_INSTANCE_FORMAT = (1 << 10),
SAR_REPLACE_NEW_LINES = (1 << 11)
};

/* The enumeration of option arguments. */
Expand All @@ -154,9 +156,10 @@ enum sar_option_args {
OPT_ARG_DATASTORE_ENTRY = 8,
OPT_ARG_STOP_BARGED_SYNTH = 9,
OPT_ARG_INSTANCE_FORMAT = 10,
OPT_ARG_REPLACE_NEW_LINES = 11,

/* This MUST be the last value in this enum! */
OPT_ARG_ARRAY_SIZE = 11
OPT_ARG_ARRAY_SIZE = 12
};

/* The enumeration of plocies for the use of input timers. */
Expand Down Expand Up @@ -1165,6 +1168,9 @@ static int synthandrecog_option_apply(sar_options_t *options, const char *key, c
} else if (strcasecmp(key, "nif") == 0) {
options->flags |= SAR_INSTANCE_FORMAT;
options->params[OPT_ARG_INSTANCE_FORMAT] = value;
} else if (strcasecmp(key, "rnl") == 0) {
options->flags |= SAR_REPLACE_NEW_LINES;
options->params[OPT_ARG_REPLACE_NEW_LINES] = value;
} else {
ast_log(LOG_WARNING, "Unknown option: %s\n", key);
}
Expand Down Expand Up @@ -1525,6 +1531,15 @@ static int app_synthandrecog_exec(struct ast_channel *chan, ast_app_data data)
}
}

/* Check whether new lines shall be replaced */
if ((sar_options.flags & SAR_REPLACE_NEW_LINES) == SAR_REPLACE_NEW_LINES) {
if (!ast_strlen_zero(sar_options.params[OPT_ARG_REPLACE_NEW_LINES])) {
char ch = *sar_options.params[OPT_ARG_REPLACE_NEW_LINES];
ast_log(LOG_DEBUG, "(%s) Replace new lines: %c\n", recog_name, ch);
app_session->replace_new_lines = ch;
}
}

/* Get grammar delimiters. */
const char *grammar_delimiters = ",";
if ((sar_options.flags & SAR_GRAMMAR_DELIMITERS) == SAR_GRAMMAR_DELIMITERS) {
Expand Down

0 comments on commit f16eacb

Please sign in to comment.