[asterisk-commits] file: branch 1.4 r60361 - in /branches/1.4: apps/ include/asterisk/ res/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Apr 5 18:14:01 MST 2007


Author: file
Date: Thu Apr  5 20:14:00 2007
New Revision: 60361

URL: http://svn.digium.com/view/asterisk?view=rev&rev=60361
Log:
Add support for returning different types of results (ie: NBest).

Modified:
    branches/1.4/apps/app_speech_utils.c
    branches/1.4/include/asterisk/speech.h
    branches/1.4/res/res_speech.c

Modified: branches/1.4/apps/app_speech_utils.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/apps/app_speech_utils.c?view=diff&rev=60361&r1=60360&r2=60361
==============================================================================
--- branches/1.4/apps/app_speech_utils.c (original)
+++ branches/1.4/apps/app_speech_utils.c Thu Apr  5 20:14:00 2007
@@ -126,19 +126,28 @@
 	return speech;
 }
 
-/* Helper function to find a specific speech recognition result by number */
-static struct ast_speech_result *find_result(struct ast_speech_result *results, int num)
-{
-	struct ast_speech_result *result = NULL;
-	int i = 0;
-
-	result = results;
-	while (result) {
-		if (i == num)
+/* Helper function to find a specific speech recognition result by number and nbest alternative */
+static struct ast_speech_result *find_result(struct ast_speech_result *results, char *result_num)
+{
+	struct ast_speech_result *result = results;
+	char *tmp = NULL;
+	int nbest_num = 0, wanted_num = 0, i = 0;
+
+	if ((tmp = strchr(result_num, '/'))) {
+		*tmp++ = '\0';
+		nbest_num = atoi(result_num);
+		wanted_num = atoi(tmp);
+	} else {
+		wanted_num = atoi(result_num);
+	}
+
+	do {
+		if (result->nbest_num != nbest_num)
+			continue;
+		if (i == wanted_num)
 			break;
 		i++;
-		result = result->next;
-	}
+	} while ((result = result->next));
 
 	return result;
 }
@@ -151,7 +160,7 @@
 	struct ast_speech *speech = find_speech(chan);
 	char tmp[128] = "";
 
-	if (data == NULL || speech == NULL || !(result = find_result(speech->results, atoi(data))))
+	if (data == NULL || speech == NULL || !(result = find_result(speech->results, data)))
 		return -1;
 	
 	snprintf(tmp, sizeof(tmp), "%d", result->score);
@@ -164,7 +173,7 @@
 static struct ast_custom_function speech_score_function = {
         .name = "SPEECH_SCORE",
         .synopsis = "Gets the confidence score of a result.",
-        .syntax = "SPEECH_SCORE(result number)",
+        .syntax = "SPEECH_SCORE([nbest number/]result number)",
         .desc =
         "Gets the confidence score of a result.\n",
         .read = speech_score,
@@ -178,7 +187,7 @@
         struct ast_speech_result *result = NULL;
         struct ast_speech *speech = find_speech(chan);
 
-	if (data == NULL || speech == NULL || !(result = find_result(speech->results, atoi(data))))
+	if (data == NULL || speech == NULL || !(result = find_result(speech->results, data)))
                 return -1;
 
 	if (result->text != NULL)
@@ -190,7 +199,7 @@
 static struct ast_custom_function speech_text_function = {
         .name = "SPEECH_TEXT",
         .synopsis = "Gets the recognized text of a result.",
-        .syntax = "SPEECH_TEXT(result number)",
+        .syntax = "SPEECH_TEXT([nbest number/]result number)",
         .desc =
         "Gets the recognized text of a result.\n",
         .read = speech_text,
@@ -204,7 +213,7 @@
         struct ast_speech_result *result = NULL;
         struct ast_speech *speech = find_speech(chan);
 
-	if (data == NULL || speech == NULL || !(result = find_result(speech->results, atoi(data))))
+	if (data == NULL || speech == NULL || !(result = find_result(speech->results, data)))
                 return -1;
 
 	if (result->grammar != NULL)
@@ -216,7 +225,7 @@
 static struct ast_custom_function speech_grammar_function = {
         .name = "SPEECH_GRAMMAR",
         .synopsis = "Gets the matched grammar of a result if available.",
-        .syntax = "SPEECH_GRAMMAR(result number)",
+        .syntax = "SPEECH_GRAMMAR([nbest number/]result number)",
         .desc =
         "Gets the matched grammar of a result if available.\n",
         .read = speech_grammar,
@@ -244,6 +253,32 @@
 	"Changes a speech engine specific attribute.\n",
 	.read = NULL,
 	.write = speech_engine_write,
+};
+
+/*! \brief SPEECH_RESULTS_TYPE() Dialplan Function */
+static int speech_results_type_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
+{
+	struct ast_speech *speech = find_speech(chan);
+
+	if (data == NULL || speech == NULL)
+		return -1;
+
+	if (!strcasecmp(value, "normal"))
+		ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NORMAL);
+	else if (!strcasecmp(value, "nbest"))
+		ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NBEST);
+
+	return 0;
+}
+
+static struct ast_custom_function speech_results_type_function = {
+	.name = "SPEECH_RESULTS_TYPE",
+	.synopsis = "Sets the type of results that will be returned.",
+	.syntax = "SPEECH_RESULTS_TYPE()=results type",
+	.desc =
+	"Sets the type of results that will be returned. Valid options are normal or nbest.",
+	.read = NULL,
+	.write = speech_results_type_write,
 };
 
 /*! \brief SPEECH() Dialplan Function */
@@ -779,6 +814,7 @@
 	res |= ast_custom_function_unregister(&speech_text_function);
 	res |= ast_custom_function_unregister(&speech_grammar_function);
 	res |= ast_custom_function_unregister(&speech_engine_function);
+	res |= ast_custom_function_unregister(&speech_results_type_function);
 
 	ast_module_user_hangup_all();
 
@@ -803,6 +839,7 @@
 	res |= ast_custom_function_register(&speech_text_function);
 	res |= ast_custom_function_register(&speech_grammar_function);
 	res |= ast_custom_function_register(&speech_engine_function);
+	res |= ast_custom_function_register(&speech_results_type_function);
 
 	return res;
 }

Modified: branches/1.4/include/asterisk/speech.h
URL: http://svn.digium.com/view/asterisk/branches/1.4/include/asterisk/speech.h?view=diff&rev=60361&r1=60360&r2=60361
==============================================================================
--- branches/1.4/include/asterisk/speech.h (original)
+++ branches/1.4/include/asterisk/speech.h Thu Apr  5 20:14:00 2007
@@ -37,6 +37,11 @@
 #define AST_SPEECH_STATE_WAIT 2 /* Wait for results to become available */
 #define AST_SPEECH_STATE_DONE 3 /* Processing is done */
 
+enum ast_speech_results_type {
+	AST_SPEECH_RESULTS_TYPE_NORMAL = 0,
+	AST_SPEECH_RESULTS_TYPE_NBEST,
+};
+
 /* Speech structure */
 struct ast_speech {
 	/*! Structure lock */
@@ -53,6 +58,8 @@
 	void *data;
 	/*! Cached results */
 	struct ast_speech_result *results;
+	/*! Type of results we want */
+	enum ast_speech_results_type results_type;
 	/*! Pointer to the engine used by this speech structure */
 	struct ast_speech_engine *engine;
 };
@@ -79,6 +86,8 @@
 	int (*start)(struct ast_speech *speech);
 	/*! Change an engine specific setting */
 	int (*change)(struct ast_speech *speech, char *name, const char *value);
+	/*! Change the type of results we want back */
+	int (*change_results_type)(struct ast_speech *speech, enum ast_speech_results_type results_type);
 	/*! Try to get results */
 	struct ast_speech_result *(*get)(struct ast_speech *speech);
 	/*! Accepted formats by the engine */
@@ -92,6 +101,8 @@
 	char *text;
 	/*! Result score */
 	int score;
+	/*! NBest Alternative number if in NBest results type */
+	int nbest_num;
 	/*! Matched grammar */
 	char *grammar;
 	/*! List information */
@@ -120,6 +131,8 @@
 int ast_speech_write(struct ast_speech *speech, void *data, int len);
 /*! \brief Change an engine specific attribute */
 int ast_speech_change(struct ast_speech *speech, char *name, const char *value);
+/*! \brief Change the type of results we want */
+int ast_speech_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type);
 /*! \brief Change state of a speech structure */
 int ast_speech_change_state(struct ast_speech *speech, int state);
 /*! \brief Register a speech recognition engine */

Modified: branches/1.4/res/res_speech.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/res/res_speech.c?view=diff&rev=60361&r1=60360&r2=60361
==============================================================================
--- branches/1.4/res/res_speech.c (original)
+++ branches/1.4/res/res_speech.c Thu Apr  5 20:14:00 2007
@@ -293,6 +293,19 @@
 	return res;
 }
 
+/*! \brief Change the type of results we want */
+int ast_speech_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type)
+{
+	int res = 0;
+
+	speech->results_type = results_type;
+
+	if (speech->engine->change_results_type)
+		res = speech->engine->change_results_type(speech, results_type);
+
+	return res;
+}
+
 /*! \brief Register a speech recognition engine */
 int ast_speech_register(struct ast_speech_engine *engine)
 {



More information about the asterisk-commits mailing list