4 static PyObject* globe_nltk = NULL;
5 static PyObject* globe_lang = NULL;
21 globe_nltk = nltk_get_module("nltk");
23 PINFO("nltk library loaded success: ");
25 PERR("Failed to get nltk module");
27 globe_lang = nltk_get_module("langdetect");
29 PINFO("langdetect library loaded success: ");
31 PERR("Failed to get langdetect module");
34 PyObject* nltk_word_tokenize(const char* sentence)
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);
48 PyObject* nltk_pos_tag(const char* sentence)
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);
71 PyObject* nltk_ne_chunk(const char* sentence)
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);
96 Py_DECREF(tmp_result);
104 PyObject* nltk_language_detect(const char* sentence)
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);
119 PyObject* nltk_get_module(const char* name)
121 PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
122 return PyImport_ImportModuleNoBlock(name);
125 unsigned int nltk_get_size_from_list(PyObject* list)
129 if (PyList_Check(list))
131 result = PyList_Size(list);
135 return (unsigned int)result;
143 unsigned int nltk_get_size_from_tuple(PyObject* tuple)
147 if (PyTuple_Check(tuple))
149 result = PyTuple_Size(tuple);
153 return (unsigned int)result;
161 PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index)
164 if (PyList_Check(list))
166 if (index > (PyList_Size(list)-1) || (index < 0 ))
172 PyObject *item = PyList_GetItem(list, index);
183 PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
185 PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
187 if (PyTuple_Check(tuple))
189 if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
195 PyObject *item = PyTuple_GetItem(tuple, index);
206 char* nltk_get_string_from_element(PyObject* elm)
208 PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
209 char* ch = (char*) malloc(BUF_LEN_256);
212 PERR("malloc failed");
215 memset(ch, 0, BUF_LEN_256);
217 const char *tmp_str = PyUnicode_AsUTF8(elm);
218 if (tmp_str == NULL) {
219 PERR("failed to get char from PyObject");
224 strncpy(ch, tmp_str, BUF_LEN_256-1);
228 PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
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);
235 PyObject* nltk_make_args_from_string(const char* info)
237 PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
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));
245 PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
247 PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
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);
256 PyObject* nltk_make_args_from_strings(char* info, char* tag)
258 PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
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));
267 PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
269 PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
270 return PyObject_CallObject(func, args);