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;
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);
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;