Fix build warnning
[platform/core/uifw/nlp.git] / service / src / service.c
1 #include <tizen.h>
2 #include <dlfcn.h>
3 #include <service_app.h>
4 #include <glib.h>
5 #include <Ecore.h>
6 #include "service.h"
7 #include "nlp_log.h"
8 #include "message.h"
9
10 int sec = 60.0;
11 static Ecore_Timer *service_close_timer = NULL;
12
13 struct client_s {
14     char *id;
15     rpc_port_message_notify_cb_h cb;
16 };
17
18 static struct client_s *__create_client(const char *id,
19         rpc_port_message_notify_cb_h cb)
20 {
21     PENTER();
22     struct client_s *handle;
23
24     handle = calloc(1, sizeof(struct client_s));
25     if (!handle) {
26         PERR("Out of memory");
27         return NULL;
28     }
29
30     handle->id = strdup(id);
31     if (!handle->id) {
32         PERR("Out of memory");
33         free(handle);
34         return NULL;
35     }
36
37     rpc_port_message_notify_cb_clone(cb, &handle->cb);
38     if (!handle->cb) {
39         PERR("Out of memory");
40         free(handle->id);
41         free(handle);
42         return NULL;
43     }
44
45     return handle;
46 }
47
48 static Eina_Bool timer_cb(void *data)
49 {
50     PLOG("time runs out");
51     service_close_timer = NULL;
52     service_app_exit();
53     return ECORE_CALLBACK_CANCEL;
54 }
55
56 void stop_timer()
57 {
58     if (service_close_timer != NULL) {
59         ecore_timer_del(service_close_timer);
60         service_close_timer = NULL;
61     }
62 }
63
64 void start_timer()
65 {
66     stop_timer();
67     service_close_timer = ecore_timer_add(sec,timer_cb,NULL);
68     if (service_close_timer == NULL)
69         PLOG("failed to create timer");
70 }
71
72 static void __destroy_client(gpointer data)
73 {
74     PENTER();
75     struct client_s *handle = data;
76
77     if (!handle)
78         return;
79
80     if (handle->cb)
81         rpc_port_message_notify_cb_destroy(handle->cb);
82     if (handle->id)
83         free(handle->id);
84     free(handle);
85 }
86
87
88 static void __messeage_create(rpc_port_stub_message_context_h context,
89         void *user_data)
90 {
91     PENTER();
92     char *sender = NULL;
93
94     rpc_port_stub_message_context_get_sender(context, &sender);
95     if (!sender)
96         return;
97
98     PLOG("[__RPC_PORT__] sender(%s)", sender);
99     free(sender);
100 }
101
102 static void __message_terminate(rpc_port_stub_message_context_h context,
103         void *user_data)
104 {
105     PENTER();
106     char *sender = NULL;
107
108     rpc_port_stub_message_context_get_sender(context, &sender);
109     if (!sender)
110         return;
111
112     PLOG("[__RPC_PORT__] sender(%s)", sender);
113     free(sender);
114 }
115
116 static int __message_register(rpc_port_stub_message_context_h context,
117         const char *name, rpc_port_message_notify_cb_h cb,
118         void *user_data)
119 {
120     struct client_s *client;
121
122     PLOG("[__RPC_PORT__] name(%s)", name);
123     client = __create_client(name, cb);
124     if (!client)
125         return -1;
126     rpc_port_stub_message_context_set_tag(context, client);
127     return 0;
128 }
129
130 static int __message_send(rpc_port_stub_message_context_h context,
131          bundle *msg, void *user_data)
132 {
133     stop_timer();
134     struct client_s *sender_client = NULL;
135
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);
140
141     bundle *reply = bundle_create();
142     char *info = NULL;
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;
149     if (message)
150     {
151         if (!strcmp(message, "word_tokenize"))
152         {
153             cmd = NLTK_CMD_TOKENIZE;
154         }
155         else if (!strcmp(message, "pos_tag" ))
156         {
157             cmd = NLTK_CMD_POSTAG;
158         }
159         else if (!strcmp(message, "ne_chunk"))
160         {
161             cmd = NLTK_CMD_NECHUNK;
162         }
163         else if (!strcmp(message, "lemmatize"))
164         {
165             cmd = NLTK_CMD_LEMMATIZE;
166         }
167         else if (!strcmp(message, "langdetect"))
168         {
169             cmd = NLTK_CMD_LANGDETECT;
170         }
171         else
172         {
173             cmd = NLTK_CMD_UNKNOWN;
174         }
175     }
176     switch (cmd)
177     {
178         case NLTK_CMD_TOKENIZE:
179         {
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++)
185             {
186                 tokens[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
187                 if(tokens[i]!=NULL)
188                 {
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));
191                     if(tmp_str!=NULL)
192                     {
193                         strncpy(tokens[i], tmp_str, BUF_LEN_128-1);
194                         free(tmp_str);
195                     }
196                 }
197                 else
198                 {
199                     PERR("malloc failed");
200                 }
201             }
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++)
205             {
206                 free(tokens[j]);
207             }
208             Py_DECREF(wt_lists);
209             PINFO("word_tokenize process done");
210             break;
211         }
212         case NLTK_CMD_POSTAG:
213         {
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++)
221             {
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);
225                 if(tag[i]!=NULL)
226                 {
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));
229                     if(tmp_str!=NULL)
230                     {
231                         strncpy(tag[i], tmp_str, BUF_LEN_128-1);
232                         free(tmp_str);
233                     }
234                 }
235                 else
236                 {
237                     PERR("malloc failed");
238                 }
239                 if(token[i]!=NULL)
240                 {
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));
243                     if(tmp_str!=NULL)
244                     {
245                         strncpy(token[i], tmp_str, BUF_LEN_128-1);
246                         free(tmp_str);
247                     }
248                 }
249                 else
250                 {
251                     PERR("malloc failed");
252                 }
253                 Py_DECREF(pt_elm_tuple);
254             }
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++)
259             {
260                 free(tag[j]);
261                 free(token[j]);
262             }
263             Py_DECREF(pt_result);
264             PINFO("pos_tag process done");
265             break;
266         }
267         case NLTK_CMD_NECHUNK:
268         {
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++)
276             {
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);
280                 if(s_tag[i]!=NULL)
281                 {
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));
284                     if(tmp_str!=NULL)
285                     {
286                         strncpy(s_tag[i], tmp_str, BUF_LEN_128-1);
287                         free(tmp_str);
288                     }
289                 }
290                 else
291                 {
292                     PERR("malloc failed");
293                 }
294                 if(s_token[i]!=NULL)
295                 {
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));
298                     if(tmp_str!=NULL)
299                     {
300                         strncpy(s_token[i], tmp_str, BUF_LEN_128-1);
301                         free(tmp_str);
302                     }
303                 }
304                 else
305                 {
306                     PERR("malloc failed");
307                 }
308                 Py_DECREF(ne_elm_tuple);
309             }
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++)
314             {
315                 free(s_tag[j]);
316                 free(s_token[j]);
317             }
318             Py_DECREF(ne_result);
319             PINFO("ne_chunk process done");
320             break;
321         }
322         case NLTK_CMD_LEMMATIZE:
323         {
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++)
331             {
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);
335                 if(tag[i]!=NULL)
336                 {
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));
339                     if(tmp_str!=NULL)
340                     {
341                         strncpy(tag[i], tmp_str, BUF_LEN_128-1);
342                     }
343                 }
344                 else
345                 {
346                     PERR("malloc failed");
347                 }
348                 if(token[i]!=NULL)
349                 {
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));
352                     if(tmp_str!=NULL)
353                     {
354                         if (!strncmp(tmp_str,"NN", 2)) {
355                             tmp_str = "n";
356                         }
357                         else if (!strncmp(tmp_str, "VB", 2)){
358                             tmp_str = "v";
359                         }
360                         else if (!strncmp(tmp_str, "JJ", 2)){
361                             tmp_str = "a";
362                         }
363                         else if (!strncmp(tmp_str, "R", 1)){
364                             tmp_str = "r";
365                         }
366                         else{
367                             tmp_str = "e";
368                         }
369                         strncpy(token[i], tmp_str, BUF_LEN_128-1);
370                     }
371                 }
372                 else
373                 {
374                     PERR("malloc failed");
375                 }
376             }
377
378             char *lem_buf[BUF_LEN_128] = {NULL,};
379             for(int i = 0 ;i < len ;i++)
380             {
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));
385                     if(lem_buf[i]!=NULL)
386                     {
387                         memset(lem_buf[i], 0, BUF_LEN_128);
388                         tmp_str = nltk_get_string_from_element(lm_result);
389                         if(tmp_str!=NULL)
390                         {
391                             strncpy(lem_buf[i], tmp_str, BUF_LEN_128-1);
392                         }
393                     }
394                     else
395                     {
396                         PERR("malloc failed");
397                     }
398                     if (lm_result != NULL)
399                         Py_DECREF(lm_result);
400                 }
401                 else
402                 {
403                     lem_buf[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
404                     if(lem_buf[i]!=NULL)
405                     {
406                         strncpy(lem_buf[i], tag[i], BUF_LEN_128-1);
407                     }
408                     else
409                     {
410                         PERR("malloc failed");
411                     }
412                 }
413             }
414
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++)
419             {
420                 free(tag[j]);
421                 free(token[j]);
422                 free(lem_buf[j]);
423             }
424             free(tmp_str);
425             PINFO("lemmatize process done");
426             break;
427         }
428         case NLTK_CMD_LANGDETECT:
429         {
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)
435             {
436                 memset(lang_buf[0], 0, BUF_LEN_128);
437                 tmp_str = nltk_get_string_from_element(ld_result);
438                 if(tmp_str!=NULL)
439                 {
440                     strncpy(lang_buf[0], tmp_str, BUF_LEN_128-1);
441                     free(tmp_str);
442                 }
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");
446                 free(lang_buf[0]);
447             }
448             else
449             {
450                 PERR("malloc failed");
451             }
452             Py_DECREF(ld_result);
453             break;
454         }
455         default:
456             PWARNING("unknown command, command = [%s]", message);
457             bundle_add_str(reply, "command", "Exception happens");
458             break;
459     }
460     bundle_add_str(reply, "request_id", request_id);
461     rpc_port_message_notify_cb_invoke(sender_client->cb, sender_client->id, reply);
462     bundle_free(reply);
463     start_timer();
464     return 0;
465 }
466
467 static void __message_unregister(rpc_port_stub_message_context_h context,
468         void *user_data)
469 {
470     struct client_s *client = NULL;
471
472     rpc_port_stub_message_context_get_tag(context, (void *)&client);
473     if (client == NULL)
474         return;
475     rpc_port_stub_message_context_set_tag(context, NULL);
476     PLOG("[__RPC_PORT__] name(%s)", client->id);
477     __destroy_client(client);
478 }
479
480
481 bool service_app_create(void *data)
482 {
483     nltk_initialize();
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: ");
490     start_timer();
491
492     int ret;
493     rpc_port_stub_message_callback_s callback = {
494         __messeage_create,
495         __message_terminate,
496         __message_register,
497         __message_unregister,
498         __message_send
499     };
500
501     ret = rpc_port_stub_message_register(&callback, NULL);
502     if (ret != 0)
503         dlog_print(DLOG_INFO, LOG_TAG,"Failed to register message");
504     else
505         dlog_print(DLOG_INFO, LOG_TAG,"successfully to register message");
506
507     return true;
508 }
509
510 void service_app_terminate(void *data)
511 {
512     // Todo: add your code here.
513     nltk_finalize();
514     return;
515 }
516
517 void service_app_control(app_control_h app_control, void *data)
518 {
519     // Todo: add your code here.
520     PINFO("service_app_control");
521     return;
522 }
523
524 static void
525 service_app_lang_changed(app_event_info_h event_info, void *user_data)
526 {
527     /*APP_EVENT_LANGUAGE_CHANGED*/
528     return;
529 }
530
531 static void
532 service_app_region_changed(app_event_info_h event_info, void *user_data)
533 {
534     /*APP_EVENT_REGION_FORMAT_CHANGED*/
535 }
536
537 static void
538 service_app_low_battery(app_event_info_h event_info, void *user_data)
539 {
540     /*APP_EVENT_LOW_BATTERY*/
541 }
542
543 static void
544 service_app_low_memory(app_event_info_h event_info, void *user_data)
545 {
546     /*APP_EVENT_LOW_MEMORY*/
547 }
548
549 int main(int argc, char* argv[])
550 {
551     char ad[50] = {0,};
552     service_app_lifecycle_callback_s event_callback;
553     app_event_handler_h handlers[5] = {NULL, };
554
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);
562
563     return service_app_main(argc, argv, &event_callback, ad);
564 }
565
566 void nltk_initialize()
567 {
568     PENTER();
569     Py_Initialize();
570 }
571
572 void nltk_finalize()
573 {
574     PENTER();
575     Py_Finalize();
576 }
577
578 PyObject* nltk_word_tokenize(char* sentence)
579 {
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);
586     Py_DECREF(args);
587     Py_DECREF(func);
588     return lists;
589 }
590
591 PyObject* nltk_pos_tag(char* sentence)
592 {
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);
601     Py_DECREF(args);
602     Py_DECREF(func);
603     Py_DECREF(wt_result);
604     return result;
605 }
606
607 PyObject* nltk_ne_chunk(char* sentence)
608 {
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);
621     Py_DECREF(args);
622     Py_DECREF(func);
623     Py_DECREF(pt_result);
624     Py_DECREF(tmp_result);
625     Py_DECREF(lv_func);
626     return result;
627 }
628
629 PyObject* nltk_lemmatize(char* tag, char* token)
630 {
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);
641     Py_DECREF(args);
642     Py_DECREF(wn_func);
643     Py_DECREF(func);
644     Py_DECREF(wn_result);
645     return result;
646 }
647
648 PyObject* nltk_language_detect(char* sentence)
649 {
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);
656     Py_DECREF(args);
657     Py_DECREF(func);
658     return result;
659 }
660
661 PyObject* nltk_get_module(char* name)
662 {
663     PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
664     return PyImport_ImportModuleNoBlock(name);
665 }
666
667 int nltk_get_size_from_list(PyObject* list)
668 {
669     if PyList_Check(list)
670     {
671         return PyList_Size(list);
672     }
673     else
674     {
675         return -1;
676     }
677 }
678
679 int nltk_get_size_from_tuple(PyObject* tuple)
680 {
681     if PyTuple_Check(tuple)
682     {
683         return PyTuple_Size(tuple);
684     }
685     else
686     {
687         return -1;
688     }
689 }
690
691 PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index)
692 {
693     PyObject* element;
694     if PyList_Check(list)
695     {
696         if (index > (PyList_Size(list)-1) || (index < 0 ))
697         {
698             element = NULL;
699         }
700         else
701         {
702             PyObject *item = PyList_GetItem(list, index);
703             element = item;
704         }
705     }
706     else
707     {
708         element = NULL;
709     }
710     return element;
711 }
712
713 PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
714 {
715     PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
716     PyObject* element;
717     if PyTuple_Check(tuple)
718     {
719         if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
720         {
721             element = NULL;
722         }
723         else
724         {
725             PyObject *item = PyTuple_GetItem(tuple, index);
726             element = item;
727         }
728     }
729     else
730     {
731         element = NULL;
732     }
733     return element;
734 }
735
736 char* nltk_get_string_from_element(PyObject* elm)
737 {
738     PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
739     char* ch = (char*) malloc(BUF_LEN_256);
740     if(ch == NULL)
741     {
742         PERR("malloc failed");
743         return ch;
744     }
745     memset(ch, 0, BUF_LEN_256);
746     strncpy(ch, PyString_AsString(elm), BUF_LEN_256-1);
747     return ch;
748 }
749
750 PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
751 {
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);
755 }
756
757 PyObject* nltk_make_args_from_string(char* info)
758 {
759     PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
760     PyObject *pArgs;
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));
764     return pArgs;
765 }
766
767 PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
768 {
769     PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
770     PyObject *pArgs;
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);
775     return pArgs;
776 }
777
778 PyObject* nltk_make_args_from_strings(char* info, char* tag)
779 {
780     PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
781     PyObject *pArgs;
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));
786     return pArgs;
787 }
788
789 PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
790 {
791     PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
792     return PyObject_CallObject(func, args);
793 }
794
795 // This is an example of an method to get attribute of module.
796 char* nltk_get_attribute(int z)
797 {
798     PyObject *pModule, *pFunc;
799     PyObject *pArgs;
800     char* ch = (char*) malloc(BUF_LEN_256);
801     if (ch != NULL)
802     {
803         memset(ch, 0, BUF_LEN_256);
804         pModule = PyImport_ImportModule("site");
805         if (pModule != NULL)
806         {
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");
813         if (pFunc != NULL) {
814             strncpy(ch, PyString_AsString(pFunc), BUF_LEN_256-1);
815             } else {
816             strncpy(ch, "attribute get error\n", BUF_LEN_256-1);
817             }
818         } else {
819             strncpy(ch, "nltk module import error\n", BUF_LEN_256-1);
820         }
821         return ch;
822     }
823     else
824     {
825         PERR("malloc failed");
826         return NULL;
827     }
828 }