eb3e0d0b2558fdc07d5d9a92368474cf031c8a00
[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 int nltk_get_size_from_list(PyObject* list)
126 {
127     if (PyList_Check(list))
128     {
129         return PyList_Size(list);
130     }
131     else
132     {
133         return 0;
134     }
135 }
136
137 int nltk_get_size_from_tuple(PyObject* tuple)
138 {
139     if (PyTuple_Check(tuple))
140     {
141         return PyTuple_Size(tuple);
142     }
143     else
144     {
145         return 0;
146     }
147 }
148
149 PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index)
150 {
151     PyObject* element;
152     if (PyList_Check(list))
153     {
154         if (index > (PyList_Size(list)-1) || (index < 0 ))
155         {
156             element = NULL;
157         }
158         else
159         {
160             PyObject *item = PyList_GetItem(list, index);
161             element = item;
162         }
163     }
164     else
165     {
166         element = NULL;
167     }
168     return element;
169 }
170
171 PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
172 {
173     PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
174     PyObject* element;
175     if (PyTuple_Check(tuple))
176     {
177         if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
178         {
179             element = NULL;
180         }
181         else
182         {
183             PyObject *item = PyTuple_GetItem(tuple, index);
184             element = item;
185         }
186     }
187     else
188     {
189         element = NULL;
190     }
191     return element;
192 }
193
194 char* nltk_get_string_from_element(PyObject* elm)
195 {
196     PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
197     char* ch = (char*) malloc(BUF_LEN_256);
198     if (ch == NULL)
199     {
200         PERR("malloc failed");
201         return ch;
202     }
203     memset(ch, 0, BUF_LEN_256);
204
205     const char *tmp_str = PyUnicode_AsUTF8(elm);
206     if (tmp_str == NULL) {
207         PERR("failed to get char from PyObject");
208         free(ch);
209         return NULL;
210     }
211
212     strncpy(ch, tmp_str, BUF_LEN_256-1);
213     return ch;
214 }
215
216 PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
217 {
218     PRET_VM(!module, NULL, "Input parameter [module] is NULL!");
219     PRET_VM(!func_name, NULL, "Input parameter [func_name] is NULL!");
220     return PyObject_GetAttrString(module, func_name);
221 }
222
223 PyObject* nltk_make_args_from_string(const char* info)
224 {
225     PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
226     PyObject *pArgs;
227     //create args tuple struct to fill the arg one by one  ,here , only create one string with 1
228     pArgs = PyTuple_New(1);
229     PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(info));
230     return pArgs;
231 }
232
233 PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
234 {
235     PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
236     PyObject *pArgs;
237     //create args tuple struct to fill the arg one by one  ,here ,only create one python object with 1
238     pArgs = PyTuple_New(1);
239     //set a string for item 0 of tuple
240     PyTuple_SetItem(pArgs, 0, pyobj);
241     return pArgs;
242 }
243
244 PyObject* nltk_make_args_from_strings(char* info, char* tag)
245 {
246     PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
247     PyObject *pArgs;
248     //create args tuple struct to fill the arg one by one  ,here , only create one string with 1
249     pArgs = PyTuple_New(2);
250     PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(info));
251     PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(tag));
252     return pArgs;
253 }
254
255 PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
256 {
257     PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
258     return PyObject_CallObject(func, args);
259 }