3 #include <service_app.h>
11 static Ecore_Timer *service_close_timer = NULL;
15 rpc_port_message_notify_cb_h cb;
18 static struct client_s *__create_client(const char *id,
19 rpc_port_message_notify_cb_h cb)
22 struct client_s *handle;
24 handle = calloc(1, sizeof(struct client_s));
26 PERR("Out of memory");
30 handle->id = strdup(id);
32 PERR("Out of memory");
37 rpc_port_message_notify_cb_clone(cb, &handle->cb);
39 PERR("Out of memory");
48 static Eina_Bool timer_cb(void *data)
50 PLOG("time runs out");
51 service_close_timer = NULL;
53 return ECORE_CALLBACK_CANCEL;
58 if (service_close_timer != NULL) {
59 ecore_timer_del(service_close_timer);
60 service_close_timer = NULL;
67 service_close_timer = ecore_timer_add(sec,timer_cb,NULL);
68 if (service_close_timer == NULL)
69 PLOG("failed to create timer");
72 static void __destroy_client(gpointer data)
75 struct client_s *handle = data;
81 rpc_port_message_notify_cb_destroy(handle->cb);
88 static void __messeage_create(rpc_port_stub_message_context_h context,
94 rpc_port_stub_message_context_get_sender(context, &sender);
98 PLOG("[__RPC_PORT__] sender(%s)", sender);
102 static void __message_terminate(rpc_port_stub_message_context_h context,
108 rpc_port_stub_message_context_get_sender(context, &sender);
112 PLOG("[__RPC_PORT__] sender(%s)", sender);
116 static int __message_register(rpc_port_stub_message_context_h context,
117 const char *name, rpc_port_message_notify_cb_h cb,
120 struct client_s *client;
122 PLOG("[__RPC_PORT__] name(%s)", name);
123 client = __create_client(name, cb);
126 rpc_port_stub_message_context_set_tag(context, client);
130 static int __message_send(rpc_port_stub_message_context_h context,
131 bundle *msg, void *user_data)
134 struct client_s *sender_client = NULL;
136 char *message = NULL;
137 bundle_get_str(msg, "command", &message);
138 rpc_port_stub_message_context_get_tag(context, (void *)&sender_client);
139 PLOG("[__RPC_PORT__] name(%s), msg(%s)", sender_client->id, message);
141 bundle *reply = bundle_create();
143 char *request_id = NULL;
144 char *tmp_str = NULL;
145 unsigned static int len = 0;
146 bundle_get_str(msg, "request_id", &request_id);
147 bundle_get_str(msg, "info", &info);
148 NLTK_CMDS cmd = NLTK_CMD_NONE;
151 if (!strcmp(message, "word_tokenize"))
153 cmd = NLTK_CMD_TOKENIZE;
155 else if (!strcmp(message, "pos_tag" ))
157 cmd = NLTK_CMD_POSTAG;
159 else if (!strcmp(message, "ne_chunk"))
161 cmd = NLTK_CMD_NECHUNK;
163 else if (!strcmp(message, "lemmatize"))
165 cmd = NLTK_CMD_LEMMATIZE;
167 else if (!strcmp(message, "langdetect"))
169 cmd = NLTK_CMD_LANGDETECT;
173 cmd = NLTK_CMD_UNKNOWN;
178 case NLTK_CMD_TOKENIZE:
180 PyObject* wt_lists = NULL;
181 wt_lists = nltk_word_tokenize(info);
182 len = nltk_get_size_from_list(wt_lists);
183 char *tokens[BUF_LEN_128] = {NULL,};
184 for(int i = 0 ;i < len ;i++)
186 tokens[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
189 memset(tokens[i], 0, BUF_LEN_128);
190 tmp_str = nltk_get_string_from_element(nltk_get_element_from_list_by_index(wt_lists, i));
193 strncpy(tokens[i], tmp_str, BUF_LEN_128-1);
199 PERR("malloc failed");
202 bundle_add_str(reply, "command", "word_tokenize");
203 bundle_add_str_array(reply, "return_token", (const char **)tokens, len);
204 for(int j = 0 ;j < len ;j++)
209 PINFO("word_tokenize process done");
212 case NLTK_CMD_POSTAG:
214 PyObject* pt_result = NULL;
215 PyObject* pt_elm_tuple = NULL;
216 pt_result = nltk_pos_tag(info);
217 len = nltk_get_size_from_list(pt_result);
218 char *tag[BUF_LEN_128] = {NULL,};
219 char *token[BUF_LEN_128] = {NULL,};
220 for(int i = 0 ;i < len ;i++)
222 token[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
223 tag[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
224 pt_elm_tuple = nltk_get_element_from_list_by_index(pt_result, i);
227 memset(tag[i], 0, BUF_LEN_128);
228 tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pt_elm_tuple, 0));
231 strncpy(tag[i], tmp_str, BUF_LEN_128-1);
237 PERR("malloc failed");
241 memset(token[i], 0, BUF_LEN_128);
242 tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pt_elm_tuple, 1));
245 strncpy(token[i], tmp_str, BUF_LEN_128-1);
251 PERR("malloc failed");
253 Py_DECREF(pt_elm_tuple);
255 bundle_add_str(reply, "command", "pos_tag");
256 bundle_add_str_array(reply, "return_tag", (const char **)tag, len);
257 bundle_add_str_array(reply, "return_token", (const char **)token, len);
258 for(int j = 0 ;j < len ;j++)
263 Py_DECREF(pt_result);
264 PINFO("pos_tag process done");
267 case NLTK_CMD_NECHUNK:
269 PyObject* ne_result = NULL;
270 PyObject* ne_elm_tuple = NULL;
271 ne_result = nltk_ne_chunk(info);
272 len = nltk_get_size_from_list(ne_result);
273 char *s_tag[BUF_LEN_128] = {NULL,};
274 char *s_token[BUF_LEN_128] = {NULL,};
275 for(int i = 0 ;i < len ;i++)
277 s_token[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
278 s_tag[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
279 ne_elm_tuple = nltk_get_element_from_list_by_index(ne_result, i);
282 memset(s_tag[i], 0, BUF_LEN_128);
283 tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(ne_elm_tuple, 0));
286 strncpy(s_tag[i], tmp_str, BUF_LEN_128-1);
292 PERR("malloc failed");
296 memset(s_token[i], 0, BUF_LEN_128);
297 tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(ne_elm_tuple, 1));
300 strncpy(s_token[i], tmp_str, BUF_LEN_128-1);
306 PERR("malloc failed");
308 Py_DECREF(ne_elm_tuple);
310 bundle_add_str(reply, "command", "ne_chunk");
311 bundle_add_str_array(reply, "return_tag", (const char **)s_tag, len);
312 bundle_add_str_array(reply, "return_token", (const char **)s_token, len);
313 for(int j = 0 ;j < len ;j++)
318 Py_DECREF(ne_result);
319 PINFO("ne_chunk process done");
322 case NLTK_CMD_LEMMATIZE:
324 PyObject* pt_result = NULL;
325 PyObject* pt_elm_tuple = NULL;
326 pt_result = nltk_pos_tag(info);
327 len = nltk_get_size_from_list(pt_result);
328 char *tag[BUF_LEN_128] = {NULL,};
329 char *token[BUF_LEN_128] = {NULL,};
330 for(int i = 0 ;i < len ;i++)
332 token[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
333 tag[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
334 pt_elm_tuple = nltk_get_element_from_list_by_index(pt_result, i);
337 memset(tag[i], 0, BUF_LEN_128);
338 tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pt_elm_tuple, 0));
341 strncpy(tag[i], tmp_str, BUF_LEN_128-1);
346 PERR("malloc failed");
350 memset(token[i], 0, BUF_LEN_128);
351 tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pt_elm_tuple, 1));
354 if (!strncmp(tmp_str,"NN", 2)) {
357 else if (!strncmp(tmp_str, "VB", 2)){
360 else if (!strncmp(tmp_str, "JJ", 2)){
363 else if (!strncmp(tmp_str, "R", 1)){
369 strncpy(token[i], tmp_str, BUF_LEN_128-1);
374 PERR("malloc failed");
378 char *lem_buf[BUF_LEN_128] = {NULL,};
379 for(int i = 0 ;i < len ;i++)
381 PyObject* lm_result = NULL;
382 if (strcmp(token[i], "e")) {
383 lm_result = nltk_lemmatize(token[i], tag[i]);
384 lem_buf[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
387 memset(lem_buf[i], 0, BUF_LEN_128);
388 tmp_str = nltk_get_string_from_element(lm_result);
391 strncpy(lem_buf[i], tmp_str, BUF_LEN_128-1);
396 PERR("malloc failed");
398 if (lm_result != NULL)
399 Py_DECREF(lm_result);
403 lem_buf[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
406 strncpy(lem_buf[i], tag[i], BUF_LEN_128-1);
410 PERR("malloc failed");
415 bundle_add_str(reply, "command", "lemmatize");
416 bundle_add_str_array(reply, "return_tag", (const char **)lem_buf, len);
417 bundle_add_str_array(reply, "return_token", (const char **)tag, len);
418 for(int j = 0 ;j < len ;j++)
425 PINFO("lemmatize process done");
428 case NLTK_CMD_LANGDETECT:
430 PyObject* ld_result = NULL;
431 ld_result = nltk_language_detect(info);
432 char *lang_buf[1] = {NULL,};
433 lang_buf[0] = (char*)malloc(BUF_LEN_128*sizeof(char));
434 if(lang_buf[0]!=NULL)
436 memset(lang_buf[0], 0, BUF_LEN_128);
437 tmp_str = nltk_get_string_from_element(ld_result);
440 strncpy(lang_buf[0], tmp_str, BUF_LEN_128-1);
443 bundle_add_str(reply, "command", "langdetect");
444 bundle_add_str_array(reply, "return_token", (const char **)lang_buf, 1);
445 PINFO("langdetect process done");
450 PERR("malloc failed");
452 Py_DECREF(ld_result);
456 PWARNING("unknown command, command = [%s]", message);
457 bundle_add_str(reply, "command", "Exception happens");
460 bundle_add_str(reply, "request_id", request_id);
461 rpc_port_message_notify_cb_invoke(sender_client->cb, sender_client->id, reply);
467 static void __message_unregister(rpc_port_stub_message_context_h context,
470 struct client_s *client = NULL;
472 rpc_port_stub_message_context_get_tag(context, (void *)&client);
475 rpc_port_stub_message_context_set_tag(context, NULL);
476 PLOG("[__RPC_PORT__] name(%s)", client->id);
477 __destroy_client(client);
481 bool service_app_create(void *data)
484 globe_nltk = nltk_get_module("nltk");
485 PINFO("nltk library loaded success: ");
486 globe_lemm = nltk_get_module("nltk.stem");
487 PINFO("nltk stem library loaded success: ");
488 globe_lang = nltk_get_module("langdetect");
489 PINFO("langdetect library loaded success: ");
493 rpc_port_stub_message_callback_s callback = {
497 __message_unregister,
501 ret = rpc_port_stub_message_register(&callback, NULL);
503 dlog_print(DLOG_INFO, LOG_TAG,"Failed to register message");
505 dlog_print(DLOG_INFO, LOG_TAG,"successfully to register message");
510 void service_app_terminate(void *data)
512 // Todo: add your code here.
517 void service_app_control(app_control_h app_control, void *data)
519 // Todo: add your code here.
520 PINFO("service_app_control");
525 service_app_lang_changed(app_event_info_h event_info, void *user_data)
527 /*APP_EVENT_LANGUAGE_CHANGED*/
532 service_app_region_changed(app_event_info_h event_info, void *user_data)
534 /*APP_EVENT_REGION_FORMAT_CHANGED*/
538 service_app_low_battery(app_event_info_h event_info, void *user_data)
540 /*APP_EVENT_LOW_BATTERY*/
544 service_app_low_memory(app_event_info_h event_info, void *user_data)
546 /*APP_EVENT_LOW_MEMORY*/
549 int main(int argc, char* argv[])
552 service_app_lifecycle_callback_s event_callback;
553 app_event_handler_h handlers[5] = {NULL, };
555 event_callback.create = service_app_create;
556 event_callback.terminate = service_app_terminate;
557 event_callback.app_control = service_app_control;
558 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
559 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
560 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
561 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
563 return service_app_main(argc, argv, &event_callback, ad);
566 void nltk_initialize()
578 PyObject* nltk_word_tokenize(char* sentence)
580 PyObject* args = NULL;
581 PyObject* func = NULL;
582 PyObject* lists = NULL;
583 args = nltk_make_args_from_string(sentence);
584 func = nltk_get_function_handle(globe_nltk, "word_tokenize");
585 lists = nltk_call_function_with_args(func, args);
591 PyObject* nltk_pos_tag(char* sentence)
593 PyObject* args = NULL;
594 PyObject* func = NULL;
595 PyObject* wt_result = NULL;
596 PyObject* result = NULL;
597 wt_result = nltk_word_tokenize(sentence);
598 func = nltk_get_function_handle(globe_nltk, "pos_tag");
599 args = nltk_make_args_from_pyobject(wt_result);
600 result = nltk_call_function_with_args(func, args);
603 Py_DECREF(wt_result);
607 PyObject* nltk_ne_chunk(char* sentence)
609 PyObject* args = NULL;
610 PyObject* pt_result = NULL;
611 PyObject* tmp_result = NULL;
612 PyObject* result = NULL;
613 PyObject* func = NULL;
614 PyObject* lv_func = NULL;
615 pt_result = nltk_pos_tag(sentence);
616 args = nltk_make_args_from_pyobject(pt_result);
617 func = nltk_get_function_handle(globe_nltk, "ne_chunk");
618 tmp_result = nltk_call_function_with_args(func, args);
619 lv_func = nltk_get_function_handle(tmp_result, "leaves");
620 result = nltk_call_function_with_args(lv_func, NULL);
623 Py_DECREF(pt_result);
624 Py_DECREF(tmp_result);
629 PyObject* nltk_lemmatize(char* tag, char* token)
631 PyObject* args = NULL;
632 PyObject* wn_func = NULL;
633 PyObject* func = NULL;
634 PyObject* wn_result = NULL;
635 PyObject* result = NULL;
636 args = nltk_make_args_from_strings(token, tag);
637 wn_func = nltk_get_function_handle(globe_lemm,"WordNetLemmatizer");
638 wn_result = nltk_call_function_with_args(wn_func, NULL);
639 func = nltk_get_function_handle(wn_result, "lemmatize");
640 result = nltk_call_function_with_args(func, args);
644 Py_DECREF(wn_result);
648 PyObject* nltk_language_detect(char* sentence)
650 PyObject* args = NULL;
651 PyObject* func = NULL;
652 PyObject* result = NULL;
653 args = nltk_make_args_from_string(sentence);
654 func = nltk_get_function_handle(globe_lang,"detect");
655 result = nltk_call_function_with_args(func, args);
661 PyObject* nltk_get_module(char* name)
663 PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
664 return PyImport_ImportModuleNoBlock(name);
667 int nltk_get_size_from_list(PyObject* list)
669 if PyList_Check(list)
671 return PyList_Size(list);
679 int nltk_get_size_from_tuple(PyObject* tuple)
681 if PyTuple_Check(tuple)
683 return PyTuple_Size(tuple);
691 PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index)
694 if PyList_Check(list)
696 if (index > (PyList_Size(list)-1) || (index < 0 ))
702 PyObject *item = PyList_GetItem(list, index);
713 PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
715 PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
717 if PyTuple_Check(tuple)
719 if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
725 PyObject *item = PyTuple_GetItem(tuple, index);
736 char* nltk_get_string_from_element(PyObject* elm)
738 PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
739 char* ch = (char*) malloc(BUF_LEN_256);
742 PERR("malloc failed");
745 memset(ch, 0, BUF_LEN_256);
746 strncpy(ch, PyString_AsString(elm), BUF_LEN_256-1);
750 PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
752 PRET_VM(!module, NULL, "Input parameter [module] is NULL!");
753 PRET_VM(!func_name, NULL, "Input parameter [func_name] is NULL!");
754 return PyObject_GetAttrString(module, func_name);
757 PyObject* nltk_make_args_from_string(char* info)
759 PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
761 //create args tuple struct to fill the arg one by one ,here , only create one string with 1
762 pArgs = PyTuple_New(1);
763 PyTuple_SetItem(pArgs, 0, PyString_FromString(info));
767 PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
769 PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
771 //create args tuple struct to fill the arg one by one ,here ,only create one python object with 1
772 pArgs = PyTuple_New(1);
773 //set a string for item 0 of tuple
774 PyTuple_SetItem(pArgs, 0, pyobj);
778 PyObject* nltk_make_args_from_strings(char* info, char* tag)
780 PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
782 //create args tuple struct to fill the arg one by one ,here , only create one string with 1
783 pArgs = PyTuple_New(2);
784 PyTuple_SetItem(pArgs, 0, PyString_FromString(info));
785 PyTuple_SetItem(pArgs, 1, PyString_FromString(tag));
789 PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
791 PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
792 return PyObject_CallObject(func, args);
795 // This is an example of an method to get attribute of module.
796 char* nltk_get_attribute(int z)
798 PyObject *pModule, *pFunc;
800 char* ch = (char*) malloc(BUF_LEN_256);
803 memset(ch, 0, BUF_LEN_256);
804 pModule = PyImport_ImportModule("site");
807 //create args tuple struct to fill the arg one by one ,here ,only create one param with 1
808 pArgs = PyTuple_New(1);
809 //set a string for item 0 of tuple
810 PyTuple_SetItem(pArgs, 0, PyString_FromString("Hello World"));
811 //call word_tokenize func with args
812 pFunc = PyObject_GetAttrString(pModule, "USER_SITE");
814 strncpy(ch, PyString_AsString(pFunc), BUF_LEN_256-1);
816 strncpy(ch, "attribute get error\n", BUF_LEN_256-1);
819 strncpy(ch, "nltk module import error\n", BUF_LEN_256-1);
825 PERR("malloc failed");