bool RecogSession::ParseNLSMLResult(mrcp_message_t* pMrcpMessage) { nlsml_result_t *pResult = nlsml_result_parse(pMrcpMessage->body.buf, pMrcpMessage->body.length, pMrcpMessage->pool); if(!pResult) return false; nlsml_result_trace(pResult, pMrcpMessage->pool); return true; }
/** Get NLSML result */ static const char* nlsml_result_get(mrcp_message_t *message) { nlsml_interpretation_t *interpretation; nlsml_instance_t *instance; nlsml_result_t *result = nlsml_result_parse(message->body.buf, message->body.length, message->pool); if(!result) { return NULL; } /* get first interpretation */ interpretation = nlsml_first_interpretation_get(result); if(!interpretation) { return NULL; } /* get first instance */ instance = nlsml_interpretation_first_instance_get(interpretation); if(!instance) { return NULL; } nlsml_instance_swi_suppress(instance); return nlsml_instance_content_generate(instance, message->pool); }
/** \brief Build ast_speech_result based on the NLSML result */ static struct ast_speech_result* uni_recog_speech_result_build(uni_speech_t *uni_speech, const apt_str_t *nlsml_result, mrcp_version_e mrcp_version) { float confidence; const char *grammar; const char *text; struct ast_speech_result *speech_result; struct ast_speech_result *first_speech_result; nlsml_interpretation_t *interpretation; nlsml_instance_t *instance; nlsml_input_t *input; int interpretation_count; int instance_count; apr_pool_t *pool = mrcp_application_session_pool_get(uni_speech->session); nlsml_result_t *result = nlsml_result_parse(nlsml_result->buf, nlsml_result->length, pool); if(!result) { ast_log(LOG_WARNING, "(%s) Failed to parse NLSML result: %s\n",uni_speech->name,nlsml_result->buf); return NULL; } #if 1 /* enable/disable debug output of parsed results */ nlsml_result_trace(result, pool); #endif first_speech_result = NULL; #if AST_VERSION_AT_LEAST(1,6,0) AST_LIST_HEAD_NOLOCK(, ast_speech_result) speech_results; AST_LIST_HEAD_INIT_NOLOCK(&speech_results); #else struct ast_speech_result *last_speech_result = NULL; #endif interpretation_count = 0; interpretation = nlsml_first_interpretation_get(result); while(interpretation) { input = nlsml_interpretation_input_get(interpretation); if(!input) { ast_log(LOG_WARNING, "(%s) Failed to get NLSML input\n",uni_speech->name); continue; } instance_count = 0; instance = nlsml_interpretation_first_instance_get(interpretation); if(!instance) { ast_log(LOG_WARNING, "(%s) Failed to get NLSML instance\n",uni_speech->name); continue; } confidence = nlsml_interpretation_confidence_get(interpretation); grammar = nlsml_interpretation_grammar_get(interpretation); if(grammar) { const char session_token[] = "session:"; char *str = strstr(grammar,session_token); if(str) { grammar = str + sizeof(session_token) - 1; } } do { nlsml_instance_swi_suppress(instance); text = nlsml_instance_content_generate(instance, pool); speech_result = ast_calloc(sizeof(struct ast_speech_result), 1); if(text) speech_result->text = strdup(text); speech_result->score = confidence * 100; if(grammar) speech_result->grammar = strdup(grammar); speech_result->nbest_num = interpretation_count; if(!first_speech_result) first_speech_result = speech_result; #if AST_VERSION_AT_LEAST(1,6,0) AST_LIST_INSERT_TAIL(&speech_results, speech_result, list); #else speech_result->next = last_speech_result; last_speech_result = speech_result; #endif ast_log(LOG_NOTICE, "(%s) Speech result[%d/%d]: %s, score: %d, grammar: %s\n", uni_speech->name, interpretation_count, instance_count, speech_result->text, speech_result->score, speech_result->grammar); instance_count++; instance = nlsml_interpretation_next_instance_get(interpretation, instance); } while(instance); interpretation_count++; interpretation = nlsml_next_interpretation_get(result, interpretation); } return first_speech_result; }