From: zhangzg Date: Tue, 17 Jul 2018 07:35:58 +0000 (+0800) Subject: improve nltk service code X-Git-Tag: submit/tizen/20180821.231644~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=98b12c51a0d7233a5ab708d7b9aa6f25764318e2;p=platform%2Fcore%2Fuifw%2Fnlp.git improve nltk service code 1.remove useless code 2.use enum to describe nltk cmd Change-Id: I309cad0c80e9e72149d4db49fe6554e9db94ef26 --- diff --git a/service/inc/service.h b/service/inc/service.h index f62d0ce..1930dc0 100644 --- a/service/inc/service.h +++ b/service/inc/service.h @@ -9,6 +9,16 @@ #endif #define LOG_TAG "nltk_service" +typedef enum { + NLTK_CMD_NONE = -1, + NLTK_CMD_TOKENIZE, + NLTK_CMD_POSTAG, + NLTK_CMD_NECHUNK, + NLTK_CMD_LEMMATIZE, + NLTK_CMD_LANGDETECT, + NLTK_CMD_MAX +}NLTK_CMDS; + // This method is exported from nltk_native_library.so void nltk_initialize(); void nltk_finalize(); diff --git a/service/src/service.c b/service/src/service.c index b325d26..11f5abb 100644 --- a/service/src/service.c +++ b/service/src/service.c @@ -12,7 +12,6 @@ message_port_cb(int local_port_id, const char *remote_app_id, const char *remote const char *command = NULL; bundle *reply = bundle_create(); bundle_get_str(message, "command", &command); - //dlog_print(DLOG_INFO, LOG_TAG, "service get message from client, remote port is %s",remote_port); char *info = NULL; PyObject* args = NULL; PyObject* lists = NULL; @@ -31,119 +30,126 @@ message_port_cb(int local_port_id, const char *remote_app_id, const char *remote PyObject* pos_elm_tuple = NULL; bundle_get_str(message, "info", &info); dlog_print(DLOG_INFO, LOG_TAG, "service get message from client, remote command is %s , remote info is %s",command ,info); - int static i = -1; - if (!strcmp(command, "word_tokenize")) - {i = 1;dlog_print(DLOG_INFO, LOG_TAG, "service get a command is: %s", command);} - else if (!strcmp(command, "pos_tag" )) - {i = 2;dlog_print(DLOG_INFO, LOG_TAG, "service get a command is: %s", command);} - else if (!strcmp(command, "ne_chunk")) - {i = 3;dlog_print(DLOG_INFO, LOG_TAG, "service get a command is: %s", command);} - else if (!strcmp(command, "lemmatize")) - {i = 4;dlog_print(DLOG_INFO, LOG_TAG, "service get a command is: %s", command);} - else if (!strcmp(command, "langdetect")) - {i = 5;dlog_print(DLOG_INFO, LOG_TAG, "service get a command is: %s", command);} - switch (i) { - case 1: + + NLTK_CMDS cmd = NLTK_CMD_NONE; + if (command) { - args = nltk_make_args_from_string(info); - lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args); - len = nltk_get_size_from_list(lists); - char *s_token[128] = {NULL,}; - for(int i = 0 ;i < len ;i++) + if (!strcmp(command, "word_tokenize")) { - s_token[i] = (char*)malloc(128*sizeof(char)); - strcpy(s_token[i] ,nltk_get_string_from_element(nltk_get_element_from_list_by_index(lists, i))); + cmd = NLTK_CMD_TOKENIZE; } - bundle_add_str(reply, "command", "word_tokenize"); - bundle_add_str_array(reply, "return", s_token, len); - dlog_print(DLOG_INFO, LOG_TAG, "word_tokenize process done"); - } - break; - case 2: - { - char *s_tag[128] = {NULL,}; - char *s_token[128] = {NULL,}; - args = nltk_make_args_from_string(info); - lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args); - postag_module = nltk_get_function_handle(globe_nltk,"pos_tag"); - args = nltk_make_args_from_pyobject(lists); - postag_result = nltk_call_function_with_args(postag_module, args); - len = nltk_get_size_from_list(postag_result); - for(int i = 0 ;i < len ;i++) + else if (!strcmp(command, "pos_tag" )) { - s_token[i] = (char*)malloc(128*sizeof(char)); - s_tag[i] = (char*)malloc(128*sizeof(char)); - pos_elm_tuple = nltk_get_element_from_list_by_index(postag_result, i); - strcpy(s_tag[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0))); - strcpy(s_token[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1))); + cmd = NLTK_CMD_POSTAG; } - bundle_add_str(reply, "command", "pos_tag"); - bundle_add_str_array(reply, "return_tag", s_tag, len); - bundle_add_str_array(reply, "return_token", s_token, len); - dlog_print(DLOG_INFO, LOG_TAG, "pos_tag process done"); - } - break; - case 3: - { - char *s_tag[128] = {NULL,}; - char *s_token[128] = {NULL,}; - args = nltk_make_args_from_string(info); - lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args); - postag_module = nltk_get_function_handle(globe_nltk,"pos_tag"); - args = nltk_make_args_from_pyobject(lists); - postag_result = nltk_call_function_with_args(postag_module, args); - args = nltk_make_args_from_pyobject(postag_result); - ne_module = nltk_get_function_handle(globe_nltk,"ne_chunk"); - ne_result = nltk_call_function_with_args(ne_module, args); - lv_module = nltk_get_function_handle(ne_result, "leaves"); - lv_result = nltk_call_function_with_args(lv_module, NULL); - len = nltk_get_size_from_list(lv_result); - for(int i = 0 ;i < len ;i++) + else if (!strcmp(command, "ne_chunk")) { - s_token[i] = (char*)malloc(128*sizeof(char)); - s_tag[i] = (char*)malloc(128*sizeof(char)); - pos_elm_tuple = nltk_get_element_from_list_by_index(lv_result, i); - strcpy(s_tag[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0))); - strcpy(s_token[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1))); + cmd = NLTK_CMD_NECHUNK; + } + else if (!strcmp(command, "lemmatize")) + { + cmd = NLTK_CMD_LEMMATIZE; + } + else if (!strcmp(command, "langdetect")) + { + cmd = NLTK_CMD_LANGDETECT; } - bundle_add_str(reply, "command", "ne_chunk"); - bundle_add_str_array(reply, "return_tag", s_tag, len); - bundle_add_str_array(reply, "return_token", s_token, len); - dlog_print(DLOG_INFO, LOG_TAG, "ne_chunk process done"); - } - break; - case 4: - { - char *buf[1] = {NULL,}; - buf[0] = (char*)malloc(128*sizeof(char)); - args = nltk_make_args_from_string(info); - lm_module = nltk_get_function_handle(globe_lemm,"WordNetLemmatizer"); - lm_object = nltk_call_function_with_args(lm_module, NULL); - lm_module = nltk_get_function_handle(lm_object, "lemmatize"); - lm_result = nltk_call_function_with_args(lm_module, args); - strcpy(buf[0] , nltk_get_string_from_element(lm_result)); - bundle_add_str(reply, "command", "lemmatize"); - bundle_add_str_array(reply, "return_token", buf, 1); - dlog_print(DLOG_INFO, LOG_TAG, "lemmatize process done"); } - break; - case 5: + + switch (cmd) { - char *buf[1] = {NULL,}; - buf[0] = (char*)malloc(128*sizeof(char)); - args = nltk_make_args_from_string(info); - ld_module = nltk_get_function_handle(globe_lang,"detect"); - ld_result = nltk_call_function_with_args(ld_module, args); - strcpy(buf[0] , nltk_get_string_from_element(ld_result)); - bundle_add_str(reply, "command", "langdetect"); - bundle_add_str_array(reply, "return_token", buf, 1); - dlog_print(DLOG_INFO, LOG_TAG, "langdetect process done"); - } - break; - default: - bundle_add_str(reply, "command", "Exception happens"); - break; + case NLTK_CMD_TOKENIZE: + args = nltk_make_args_from_string(info); + lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args); + len = nltk_get_size_from_list(lists); + char *tokens[128] = {NULL,}; + for(int i = 0 ;i < len ;i++) + { + tokens[i] = (char*)malloc(128*sizeof(char)); + strcpy(tokens[i] ,nltk_get_string_from_element(nltk_get_element_from_list_by_index(lists, i))); + } + bundle_add_str(reply, "command", "word_tokenize"); + bundle_add_str_array(reply, "return", tokens, len); + dlog_print(DLOG_INFO, LOG_TAG, "word_tokenize process done"); + break; + case NLTK_CMD_POSTAG: + args = nltk_make_args_from_string(info); + lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args); + postag_module = nltk_get_function_handle(globe_nltk,"pos_tag"); + args = nltk_make_args_from_pyobject(lists); + postag_result = nltk_call_function_with_args(postag_module, args); + len = nltk_get_size_from_list(postag_result); + char *tag[128] = {NULL,}; + char *token[128] = {NULL,}; + for(int i = 0 ;i < len ;i++) + { + token[i] = (char*)malloc(128*sizeof(char)); + tag[i] = (char*)malloc(128*sizeof(char)); + pos_elm_tuple = nltk_get_element_from_list_by_index(postag_result, i); + strcpy(tag[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0))); + strcpy(token[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1))); + } + bundle_add_str(reply, "command", "pos_tag"); + bundle_add_str_array(reply, "return_tag", tag, len); + bundle_add_str_array(reply, "return_token", token, len); + dlog_print(DLOG_INFO, LOG_TAG, "pos_tag process done"); + break; + case NLTK_CMD_NECHUNK: + args = nltk_make_args_from_string(info); + lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args); + postag_module = nltk_get_function_handle(globe_nltk,"pos_tag"); + args = nltk_make_args_from_pyobject(lists); + postag_result = nltk_call_function_with_args(postag_module, args); + args = nltk_make_args_from_pyobject(postag_result); + ne_module = nltk_get_function_handle(globe_nltk,"ne_chunk"); + ne_result = nltk_call_function_with_args(ne_module, args); + lv_module = nltk_get_function_handle(ne_result, "leaves"); + lv_result = nltk_call_function_with_args(lv_module, NULL); + len = nltk_get_size_from_list(lv_result); + char *s_tag[128] = {NULL,}; + char *s_token[128] = {NULL,}; + for(int i = 0 ;i < len ;i++) + { + s_token[i] = (char*)malloc(128*sizeof(char)); + s_tag[i] = (char*)malloc(128*sizeof(char)); + pos_elm_tuple = nltk_get_element_from_list_by_index(lv_result, i); + strcpy(s_tag[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0))); + strcpy(s_token[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1))); + } + bundle_add_str(reply, "command", "ne_chunk"); + bundle_add_str_array(reply, "return_tag", s_tag, len); + bundle_add_str_array(reply, "return_token", s_token, len); + dlog_print(DLOG_INFO, LOG_TAG, "ne_chunk process done"); + break; + case NLTK_CMD_LEMMATIZE: + args = nltk_make_args_from_string(info); + lm_module = nltk_get_function_handle(globe_lemm,"WordNetLemmatizer"); + lm_object = nltk_call_function_with_args(lm_module, NULL); + lm_module = nltk_get_function_handle(lm_object, "lemmatize"); + lm_result = nltk_call_function_with_args(lm_module, args); + char *lem_buf[1] = {NULL,}; + lem_buf[0] = (char*)malloc(128*sizeof(char)); + strcpy(lem_buf[0] , nltk_get_string_from_element(lm_result)); + bundle_add_str(reply, "command", "lemmatize"); + bundle_add_str_array(reply, "return_token", lem_buf, 1); + dlog_print(DLOG_INFO, LOG_TAG, "lemmatize process done"); + break; + case NLTK_CMD_LANGDETECT: + args = nltk_make_args_from_string(info); + ld_module = nltk_get_function_handle(globe_lang,"detect"); + ld_result = nltk_call_function_with_args(ld_module, args); + char *lang_buf[1] = {NULL,}; + lang_buf[0] = (char*)malloc(128*sizeof(char)); + strcpy(lang_buf[0] , nltk_get_string_from_element(ld_result)); + bundle_add_str(reply, "command", "langdetect"); + bundle_add_str_array(reply, "return_token", lang_buf, 1); + dlog_print(DLOG_INFO, LOG_TAG, "langdetect process done"); + break; + default: + bundle_add_str(reply, "command", "Exception happens"); + break; } + int ret = message_port_send_message(remote_app_id, remote_port, reply ); if (ret != MESSAGE_PORT_ERROR_NONE) { dlog_print(DLOG_INFO, LOG_TAG, "Port send message error: %d", ret); @@ -294,11 +300,6 @@ PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index) return element; } -char* nltk_getStringFromListElement(PyObject* elm) -{ - return PyString_AsString(elm); -} - PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index) { PyObject* element;