Apply text matching algorithm to daemon
authorSuyeon Hwang <stom.hwang@samsung.com>
Tue, 4 Jul 2017 08:33:55 +0000 (17:33 +0900)
committersooyeon.kim <sooyeon.kim@samsung.com>
Mon, 26 Mar 2018 12:46:38 +0000 (21:46 +0900)
If there is only background type result or no result,
daemon finds the similar commands list using text matchin alogrithm.
The algorithm is only applied to foreground and widget type commands.

Priority order of the command type on this patch :
System and Exclusive > ASR result consumming > Foreground and Widget >
Partial matched Commands(Foreground and Widget only) > System Background >
Background

Change-Id: I2eeb8c4cff06ce8b5de6f16a30a185d7ae71c11d
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/vcd_client_data.c
server/vcd_client_data.h
server/vcd_server.c [changed mode: 0755->0644]

index 505ab6f7cde0f2706ee739e8a178e0e3b8a57855..4ae7bb33f8068fbec8690efe08986b8619f61cf3 100644 (file)
@@ -22,6 +22,8 @@
 #include "vcd_config.h"
 #include "vcd_main.h"
 
+#include "voice_control_command_expand.h"
+
 /* Client list */
 static GSList* g_client_list = NULL;
 
@@ -767,6 +769,66 @@ vcd_recognition_mode_e vcd_client_get_recognition_mode()
        return g_recognition_mode;
 }
 
+int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list)
+{
+       GSList *item = NULL;
+
+       if (NULL == list) {
+               SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid list parameter");
+               return VCD_ERROR_INVALID_PARAMETER;
+       }
+
+       switch (type) {
+       case VC_COMMAND_TYPE_SYSTEM:
+               item = g_cur_cmd_list.system_cmds;
+               break;
+       case VC_COMMAND_TYPE_FOREGROUND:
+               item = g_cur_cmd_list.foreground_cmds;
+               break;
+       case VC_COMMAND_TYPE_WIDGET:
+               item = g_cur_cmd_list.widget_cmds;
+               break;
+       case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
+               item = g_cur_cmd_list.system_background_cmds;
+               break;
+       case VC_COMMAND_TYPE_BACKGROUND:
+               item = g_cur_cmd_list.background_cmds;
+               break;
+       case VC_COMMAND_TYPE_EXCLUSIVE:
+               item = g_cur_cmd_list.exclusive_system_cmds;
+               break;
+       default:
+               SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid type parameter");
+               return VCD_ERROR_INVALID_PARAMETER;
+       }
+
+       while (NULL != item) {
+               vc_cmd_h temp_cmd = NULL;
+               vc_cmd_h target_cmd = (vc_cmd_h)item->data;
+
+               temp_cmd = (vc_cmd_h)__command_copy((vc_cmd_s*)target_cmd);
+
+               if (NULL == temp_cmd) {
+                       SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to copy the command");
+                       return VCD_ERROR_OUT_OF_MEMORY;
+               }
+
+               if (0 != vc_cmd_list_add(list, temp_cmd)) {
+                       SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add the command in the list");
+                       if (NULL != temp_cmd) {
+                               vc_cmd_destroy(temp_cmd);
+                               temp_cmd = NULL;
+                       }
+
+                       return VCD_ERROR_OPERATION_FAILED;
+               }
+
+               item = item->next;
+       }
+
+       return 0;
+}
+
 int __show_client_list()
 {
        GSList *iter = NULL;
index ae578836035fd66079c44a261e07274401d10051..3ee5f0d2a8029f7169c52677105a22c3c9f7a796 100644 (file)
@@ -96,6 +96,8 @@ int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode);
 
 vcd_recognition_mode_e vcd_client_get_recognition_mode();
 
+int vcd_client_append_cmd_from_type(int type, vc_cmd_list_h list);
+
 /*
 * Manager API
 */
old mode 100755 (executable)
new mode 100644 (file)
index a525662..9c976a5
@@ -100,7 +100,8 @@ static Eina_Bool __restart_engine(void *data)
 
        SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
 
-       if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
+       vcd_recognition_mode_e mode = vcd_client_get_recognition_mode();
+       if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == mode || VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == mode) {
                vcd_config_set_service_state(VCD_STATE_RECORDING);
                vcdc_send_service_state(VCD_STATE_RECORDING);
        }
@@ -466,9 +467,10 @@ static void* __recorder_stop(void *data)
 int vcd_send_result(vce_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed_result, const char* nlu_result, const char* msg, int* user_info, void *user_data)
 {
        int ret = 0;
+       vcd_recognition_mode_e recognition_mode = vcd_client_get_recognition_mode();
 
        if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
-               if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
+               if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != recognition_mode) {
                        SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
                        return VCD_ERROR_INVALID_STATE;
                }
@@ -480,40 +482,8 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
                event, all_result, non_fixed_result, msg, count);
 
-       SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
+       SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
 
-       if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
-               if (VCE_RESULT_EVENT_REJECTED == event) {
-                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
-                       /* If no result and restart option is ON */
-                       /* Send reject message */
-                       bool temp = vcd_client_manager_get_exclusive();
-                       vc_info_parser_set_result(all_result, event, msg, NULL, temp);
-                       ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
-                       if (0 != ret) {
-                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
-                       }
-
-                       g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
-                       return ret;
-               }
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
-               //vcd_recorder_stop();
-               ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
-       } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
-               /* Restart option is ON */
-               g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
-               if (VCE_RESULT_EVENT_REJECTED == event) {
-                       bool temp = vcd_client_manager_get_exclusive();
-                       vc_info_parser_set_result(all_result, event, msg, NULL, temp);
-                       ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
-                       if (0 != ret) {
-                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
-                       }
-                       return ret;
-               }
-       }
 #if 1
        /* if nlu_result is exist, Add command handle(is_action) into result list */
        /* Normal result */
@@ -523,7 +493,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        vc_cmd_list_h vc_cmd_list = NULL;
 
        if (0 != vc_cmd_list_create(&vc_cmd_list)) {
-               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
+               SLOG(LOG_INFO, TAG_VCD, "[Server] Fail to create command list");
                vcd_client_manager_set_exclusive(false);
                vcd_config_set_service_state(VCD_STATE_READY);
                vcdc_send_service_state(VCD_STATE_READY);
@@ -536,6 +506,7 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        int* filtered_id = (int*)calloc(count, sizeof(int));
        if (!filtered_id) {
                SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
+               vc_cmd_list_destroy(vc_cmd_list, true);
                return VCD_ERROR_OUT_OF_MEMORY;
        }
        int filtered_count = 0;
@@ -597,6 +568,8 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
                                                                }
                                                        }
 
+                                                       // Set state manually, because the result is already handled in ASR result by client.
+                                                       // So, the daemon does not need to send the result to client.
                                                        vcd_client_manager_set_exclusive(false);
 
                                                        vcd_config_set_service_state(VCD_STATE_READY);
@@ -688,72 +661,170 @@ int vcd_send_result(vce_result_event_e event, int* result_id, int count, const c
        int result_count = 0;
        vc_cmd_list_get_count(vc_cmd_list, &result_count);
 
+       /* Handle the result list */
        if (0 == result_count) {
                /* No result */
                if (NULL != all_result) {
+                       vc_cmd_list_h fg_priority_cmd_list = NULL;
                        SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
+
+                       if (0 != vc_cmd_list_create(&fg_priority_cmd_list)) {
+                               SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create command list handle");
+                               vc_cmd_list_destroy(vc_cmd_list, true);
+                               return VCD_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       /* Get the list of foreground and widget type commands */
+                       vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_FOREGROUND, fg_priority_cmd_list);
+                       vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_WIDGET, fg_priority_cmd_list);
+
+                       /* Matching algorithm */
+                       vc_cmd_get_partially_matched_cmd_list(all_result, fg_priority_cmd_list, vc_cmd_list, VC_SEARCH_CHAR_LEVEL);
+
+                       vc_cmd_list_destroy(fg_priority_cmd_list, true);
                } else {
                        SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is NULL");
                }
-               bool temp = vcd_client_manager_get_exclusive();
-               vc_info_parser_set_result(all_result, event, msg, NULL, temp);
 
-               int pid = vcd_client_widget_get_foreground_pid();
-               if (-1 != pid) {
-                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
-                       /* Send to hide tooltip */
-                       vcdc_send_show_tooltip(pid, false);
-               }
-
-               if (-1 != vcd_client_manager_get_pid()) {
-                       /* Manager client is available */
-                       if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
-                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
-                       }
-               }
+               vc_cmd_list_get_count(vc_cmd_list, &result_count);
 
-               vcd_client_manager_set_exclusive(false);
+               // After running partial matching algorithm, if there is no result.
+               if (0 == result_count) {
+                       bool temp = vcd_client_manager_get_exclusive();
+                       vc_info_parser_set_result(all_result, event, msg, NULL, temp);
 
-               return VCD_ERROR_NONE;
-       } else {
-               if (false == vcd_client_manager_get_exclusive()) {
                        int pid = vcd_client_widget_get_foreground_pid();
                        if (-1 != pid) {
                                SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
+                               /* Send to hide tooltip */
                                vcdc_send_show_tooltip(pid, false);
                        }
 
-                       vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
-
                        if (-1 != vcd_client_manager_get_pid()) {
                                /* Manager client is available */
                                if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
                                        SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
                                }
-                       } else {
-                               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
-                               /* Send result to client */
-                               ecore_timer_add(0, __vcd_send_selected_result, NULL);
                        }
-               } else {
-                       /* exclusive command */
-                       vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
 
-                       if (-1 != vcd_client_manager_get_pid()) {
-                               /* Manager client is available */
-                               if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
-                                       SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
+                       vcd_client_manager_set_exclusive(false);
+                       vc_cmd_list_destroy(vc_cmd_list, true);
+
+                       return VCD_ERROR_NONE;
+               }
+
+               // Partial matching algorithm find some commands
+               top_priority = VC_COMMAND_PRIORITY_FOREGROUND;
+               event = VC_RESULT_EVENT_RESULT_SUCCESS;
+       }
+
+       // There are more than one result.
+       if (false == vcd_client_manager_get_exclusive()) {
+               /* Foreground, Widget, Background, System, System-Background */
+               if (top_priority >= VC_COMMAND_PRIORITY_BACKGROUND) {
+                       vc_cmd_list_h fg_priority_cmd_list = NULL;
+                       vc_cmd_list_h temp_list = NULL;
+
+                       if (0 != vc_cmd_list_create(&fg_priority_cmd_list)) {
+                               SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create command list handle");
+                               vc_cmd_list_destroy(vc_cmd_list, true);
+                               return VCD_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       if (0 != vc_cmd_list_create(&temp_list)) {
+                               SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create command list handle");
+                               vc_cmd_list_destroy(fg_priority_cmd_list, true);
+                               vc_cmd_list_destroy(vc_cmd_list, true);
+                               return VCD_ERROR_OUT_OF_MEMORY;
+                       }
+
+                       /* Get the list of foreground and widget type commands */
+                       vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_FOREGROUND, fg_priority_cmd_list);
+                       vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_WIDGET, fg_priority_cmd_list);
+
+                       /* Matching algorithm */
+                       vc_cmd_get_partially_matched_cmd_list(all_result, fg_priority_cmd_list, temp_list, VC_SEARCH_CHAR_LEVEL);
+                       vc_cmd_list_destroy(fg_priority_cmd_list, true);
+
+                       vc_cmd_list_get_count(temp_list, &result_count);
+
+                       if (0 < result_count) {
+                               if (0 != vc_cmd_list_destroy(vc_cmd_list, true)) {
+                                       SLOG(LOG_WARN, TAG_VCD, "[WARNING] Fail to destroy list");
                                }
-                       } else {
-                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
+                               vc_cmd_list = temp_list;
+                               event = VC_RESULT_EVENT_RESULT_SUCCESS;
                        }
+               }
 
-                       vcd_client_manager_set_exclusive(false);
+               int pid = vcd_client_widget_get_foreground_pid();
+               if (-1 != pid) {
+                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
+                       vcdc_send_show_tooltip(pid, false);
+               }
+
+               vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
+
+               if (-1 != vcd_client_manager_get_pid()) {
+                       /* Manager client is available */
+                       if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
+                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
+                       }
+               } else {
+                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
+                       ecore_timer_add(0, __vcd_send_selected_result, NULL);
+               }
+       } else {
+               /* exclusive command */
+               vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
+
+               if (-1 != vcd_client_manager_get_pid()) {
+                       /* Manager client is available */
+                       if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
+                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
+                       }
+               } else {
+                       SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
                }
+
+               vcd_client_manager_set_exclusive(false);
        }
 
        vc_cmd_list_destroy(vc_cmd_list, true);
 
+       if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == recognition_mode) {
+               if (VCE_RESULT_EVENT_REJECTED == event) {
+                       SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
+                       /* If no result and restart option is ON */
+                       /* Send reject message */
+                       bool temp = vcd_client_manager_get_exclusive();
+                       vc_info_parser_set_result(all_result, event, msg, NULL, temp);
+                       ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
+                       if (0 != ret) {
+                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
+                       }
+
+                       g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
+                       return ret;
+               }
+               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
+               //vcd_recorder_stop();
+               ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
+       } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == recognition_mode) {
+               SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
+               /* Restart option is ON */
+               g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
+               if (VCE_RESULT_EVENT_REJECTED == event) {
+                       bool temp = vcd_client_manager_get_exclusive();
+                       vc_info_parser_set_result(all_result, event, msg, NULL, temp);
+                       ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
+                       if (0 != ret) {
+                               SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
+                       }
+                       return ret;
+               }
+       }
+
        return VCD_ERROR_NONE;
 
 #else
@@ -2599,4 +2670,3 @@ int vcd_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_f
 
        return ret;
 }
-