bool DtmfSession::ParseNLSMLResult(mrcp_message_t* pMrcpMessage) const { apr_xml_elem* pInterpret; apr_xml_elem* pInstance; apr_xml_elem* pInput; apr_xml_doc* pDoc = nlsml_doc_load(&pMrcpMessage->body,pMrcpMessage->pool); if(!pDoc) return false; /* walk through interpreted results */ pInterpret = nlsml_first_interpret_get(pDoc); for(; pInterpret; pInterpret = nlsml_next_interpret_get(pInterpret)) { /* get instance and input */ nlsml_interpret_results_get(pInterpret,&pInstance,&pInput); if(pInstance) { /* process instance */ if(pInstance->first_cdata.first) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Interpreted Instance [%s]",pInstance->first_cdata.first->text); } } if(pInput) { /* process input */ if(pInput->first_cdata.first) { apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Interpreted Input [%s]",pInput->first_cdata.first->text); } } } return true; }
/** Get NLSML input result */ static const char* nlsml_input_get(mrcp_message_t *message) { apr_xml_elem *interpret; apr_xml_elem *instance; apr_xml_elem *input; apr_xml_doc *doc = nlsml_doc_load(&message->body,message->pool); if(!doc) { return NULL; } /* get interpreted result */ interpret = nlsml_first_interpret_get(doc); if(!interpret) { return NULL; } /* get instance and input */ nlsml_interpret_results_get(interpret,&instance,&input); if(!input || !input->first_cdata.first) { return NULL; } /* return input */ return input->first_cdata.first->text; }
/** \brief Build ast_speech_result based on the NLSML result */ static struct ast_speech_result* uni_recog_speech_result_build(const apt_str_t *nlsml_result, mrcp_version_e mrcp_version, apr_pool_t *pool) { apr_xml_doc *doc; /* xml document */ apr_xml_elem *interpret; /* <interpret> element */ apr_xml_elem *instance; /* <instance> element */ apr_xml_elem *input; /* <input> element */ apr_xml_elem *text_elem; /* the element which contains the target, interpreted text */ apr_xml_elem *elem; /* temp element */ const char *confidence; const char *grammar; struct ast_speech_result *speech_result; /* Load NLSML document */ doc = nlsml_doc_load(nlsml_result,pool); if(!doc) { ast_log(LOG_WARNING, "Failed to load NLSML document\n"); return NULL; } /* Get interpretation element */ interpret = nlsml_first_interpret_get(doc); if(!interpret) { ast_log(LOG_WARNING, "Missing <interpretation> element\n"); return NULL; } /* Get instance and input elements */ nlsml_interpret_results_get(interpret,&instance,&input); if(!instance || !input) { ast_log(LOG_WARNING, "Missing either <instance> or <input> element\n"); return NULL; } /* <input> element can also contain additional <input> element(s); if so, use the child one */ elem = input->first_child; if(elem && strcmp(elem->name,"input") == 0) { input = elem; } speech_result = ast_calloc(sizeof(struct ast_speech_result), 1); speech_result->text = NULL; speech_result->score = 0; speech_result->grammar = NULL; text_elem = NULL; elem = instance->first_child; if(elem && elem->first_cdata.first) { text_elem = elem; ast_log(LOG_DEBUG, "Found speech result in the child element of the <instance> element = %s\n",text_elem->first_cdata.first->text); } if(!text_elem) { if(instance->first_cdata.first) { text_elem = instance; ast_log(LOG_DEBUG, "Found speech result in the <instance> element = %s\n",text_elem->first_cdata.first->text); } } if(!text_elem) { if(input->first_cdata.first) { text_elem = input; ast_log(LOG_DEBUG, "Found speech result in the <input> element = %s\n",text_elem->first_cdata.first->text); } } if(text_elem && text_elem->first_cdata.first->text) { speech_result->text = strdup(text_elem->first_cdata.first->text); if(speech_result->text[0] == 10 && text_elem->first_cdata.first->next) { free(speech_result->text); speech_result->text = strdup(text_elem->first_cdata.first->next->text); if(speech_result->text[0] == 9) { char *skip = speech_result->text; while(*skip==9) skip++; skip = strdup(skip); free(speech_result->text); speech_result->text = skip; } } } confidence = nlsml_input_attrib_get(instance,"confidence",TRUE); if(!confidence) { confidence = nlsml_input_attrib_get(input,"confidence",TRUE); } if(confidence) { if(mrcp_version == MRCP_VERSION_2) { speech_result->score = (int)(atof(confidence) * 100); } else { speech_result->score = atoi(confidence); } } grammar = nlsml_input_attrib_get(interpret,"grammar",TRUE); if(grammar) { char *str = strstr(grammar,"session:"); if(str) { grammar = str + strlen("session:"); } if(grammar && *grammar != '\0') { speech_result->grammar = strdup(grammar); } } ast_log(LOG_NOTICE, "Interpreted text:%s score:%d grammar:%s\n", speech_result->text ? speech_result->text : "none", speech_result->score, speech_result->grammar ? speech_result->grammar : "none"); return speech_result; }