ae5d97a9c31cfbe282ac1077ae67ba9fdb9b866f
[platform/core/uifw/nlp.git] / service / src / nltk.c
1 #include "nltk.h"
2 #include "nlp_log.h"
3
4 static PyObject* globe_nltk = NULL;
5 static PyObject* globe_lang = NULL;
6
7 void nltk_initialize()
8 {
9     PENTER();
10     Py_Initialize();
11 }
12
13 void nltk_finalize()
14 {
15     PENTER();
16     Py_Finalize();
17 }
18
19 void nltk_load()
20 {
21     globe_nltk = nltk_get_module("nltk");
22     if (globe_nltk)
23         PINFO("nltk library loaded success: ");
24     else
25         PERR("Failed to get nltk module");
26
27     globe_lang = nltk_get_module("langdetect");
28     if (globe_lang)
29         PINFO("langdetect library loaded success: ");
30     else
31         PERR("Failed to get langdetect module");
32 }
33
34 PyObject* nltk_word_tokenize(const char* sentence)
35 {
36     PyObject* args = NULL;
37     PyObject* func = NULL;
38     PyObject* lists = NULL;
39     args = nltk_make_args_from_string(sentence);
40     func = nltk_get_function_handle(globe_nltk, "word_tokenize");
41     lists = nltk_call_function_with_args(func, args);
42     Py_DECREF(args);
43     if (func)
44         Py_DECREF(func);
45     return lists;
46 }
47
48 PyObject* nltk_pos_tag(const char* sentence)
49 {
50     PyObject* args = NULL;
51     PyObject* func = NULL;
52     PyObject* wt_result = NULL;
53     PyObject* result = NULL;
54     wt_result = nltk_word_tokenize(sentence);
55     func = nltk_get_function_handle(globe_nltk, "pos_tag");
56     args = nltk_make_args_from_pyobject(wt_result);
57     result = nltk_call_function_with_args(func, args);
58
59     if (args)
60         Py_DECREF(args);
61
62     if (func)
63         Py_DECREF(func);
64
65     if (wt_result)
66         Py_DECREF(wt_result);
67
68     return result;
69 }
70
71 PyObject* nltk_ne_chunk(const char* sentence)
72 {
73     PyObject* args = NULL;
74     PyObject* pt_result = NULL;
75     PyObject* tmp_result = NULL;
76     PyObject* result = NULL;
77     PyObject* func = NULL;
78     PyObject* lv_func = NULL;
79     pt_result = nltk_pos_tag(sentence);
80     args = nltk_make_args_from_pyobject(pt_result);
81     func = nltk_get_function_handle(globe_nltk, "ne_chunk");
82     tmp_result = nltk_call_function_with_args(func, args);
83     lv_func = nltk_get_function_handle(tmp_result, "leaves");
84     result = nltk_call_function_with_args(lv_func, NULL);
85
86     if (args)
87         Py_DECREF(args);
88
89     if (func)
90         Py_DECREF(func);
91
92     if (pt_result)
93         Py_DECREF(pt_result);
94
95     if (tmp_result)
96         Py_DECREF(tmp_result);
97
98     if (lv_func)
99         Py_DECREF(lv_func);
100
101     return result;
102 }
103
104 PyObject* nltk_language_detect(const char* sentence)
105 {
106     PyObject* args = NULL;
107     PyObject* func = NULL;
108     PyObject* result = NULL;
109     args = nltk_make_args_from_string(sentence);
110     func = nltk_get_function_handle(globe_lang,"detect");
111     result = nltk_call_function_with_args(func, args);
112     Py_DECREF(args);
113     if (func)
114         Py_DECREF(func);
115
116     return result;
117 }
118
119 PyObject* nltk_get_module(const char* name)
120 {
121     PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
122     return PyImport_ImportModuleNoBlock(name);
123 }
124
125 unsigned int nltk_get_size_from_list(PyObject* list)
126 {
127     int result = 0;
128
129     if (PyList_Check(list))
130     {
131         result = PyList_Size(list);
132         if (result < 0)
133             result = 0;
134
135         return (unsigned int)result;
136     }
137     else
138     {
139         return 0;
140     }
141 }
142
143 unsigned int nltk_get_size_from_tuple(PyObject* tuple)
144 {
145     int result = 0;
146
147     if (PyTuple_Check(tuple))
148     {
149         result = PyTuple_Size(tuple);
150         if (result < 0)
151             result = 0;
152
153         return (unsigned int)result;
154     }
155     else
156     {
157         return 0;
158     }
159 }
160
161 PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index)
162 {
163     PyObject* element;
164     if (PyList_Check(list))
165     {
166         if (index > (PyList_Size(list)-1) || (index < 0 ))
167         {
168             element = NULL;
169         }
170         else
171         {
172             PyObject *item = PyList_GetItem(list, index);
173             element = item;
174         }
175     }
176     else
177     {
178         element = NULL;
179     }
180     return element;
181 }
182
183 PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
184 {
185     PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
186     PyObject* element;
187     if (PyTuple_Check(tuple))
188     {
189         if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
190         {
191             element = NULL;
192         }
193         else
194         {
195             PyObject *item = PyTuple_GetItem(tuple, index);
196             element = item;
197         }
198     }
199     else
200     {
201         element = NULL;
202     }
203     return element;
204 }
205
206 char* nltk_get_string_from_element(PyObject* elm)
207 {
208     PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
209     char* ch = (char*) malloc(BUF_LEN_256);
210     if (ch == NULL)
211     {
212         PERR("malloc failed");
213         return ch;
214     }
215     memset(ch, 0, BUF_LEN_256);
216
217     const char *tmp_str = PyUnicode_AsUTF8(elm);
218     if (tmp_str == NULL) {
219         PERR("failed to get char from PyObject");
220         free(ch);
221         return NULL;
222     }
223
224     strncpy(ch, tmp_str, BUF_LEN_256-1);
225     return ch;
226 }
227
228 PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
229 {
230     PRET_VM(!module, NULL, "Input parameter [module] is NULL!");
231     PRET_VM(!func_name, NULL, "Input parameter [func_name] is NULL!");
232     return PyObject_GetAttrString(module, func_name);
233 }
234
235 PyObject* nltk_make_args_from_string(const char* info)
236 {
237     PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
238     PyObject *pArgs;
239     //create args tuple struct to fill the arg one by one  ,here , only create one string with 1
240     pArgs = PyTuple_New(1);
241     PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(info));
242     return pArgs;
243 }
244
245 PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
246 {
247     PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
248     PyObject *pArgs;
249     //create args tuple struct to fill the arg one by one  ,here ,only create one python object with 1
250     pArgs = PyTuple_New(1);
251     //set a string for item 0 of tuple
252     PyTuple_SetItem(pArgs, 0, pyobj);
253     return pArgs;
254 }
255
256 PyObject* nltk_make_args_from_strings(char* info, char* tag)
257 {
258     PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
259     PyObject *pArgs;
260     //create args tuple struct to fill the arg one by one  ,here , only create one string with 1
261     pArgs = PyTuple_New(2);
262     PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(info));
263     PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(tag));
264     return pArgs;
265 }
266
267 PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
268 {
269     PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
270     return PyObject_CallObject(func, args);
271 }