Ejemplo n.º 1
0
/** Get specified atrribute of <input> */
APT_DECLARE(const char *) nlsml_input_attrib_get(const apr_xml_elem *input, const char *attrib, apt_bool_t recursive)
{
	const apr_xml_attr *xml_attr;
	for(xml_attr = input->attr; xml_attr; xml_attr = xml_attr->next) {
		if(strcasecmp(xml_attr->name,attrib) == 0) {
			return xml_attr->value;
		}
	}

	if(recursive && input->parent) {
		return nlsml_input_attrib_get(input->parent,attrib,recursive);
	}

	return NULL;
}
Ejemplo n.º 2
0
/** \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;
}