2 * This file contains the exported symbol.
4 #include "nltk_native_library.h"
6 // This is an example of an exported method.
17 PyObject* nltk_getModule(char* m_name)
19 return PyImport_ImportModuleNoBlock(m_name);
22 int nltk_getSizeFromList(PyObject* list)
26 return PyList_Size(list);
34 int nltk_getSizeFromTuple(PyObject* tuple)
36 if PyTuple_Check(tuple)
38 return PyTuple_Size(tuple);
46 PyObject* nltk_getElementFromListByIndex(PyObject* list, int index)
51 if (index > (PyList_Size(list)-1) || (index < 0 ))
57 PyObject *item = PyList_GetItem(list, index);
68 char* nltk_getStringFromListElement(PyObject* elm)
70 return PyString_AsString(elm);
73 PyObject* nltk_getElementFromTupleByIndex(PyObject* tuple, int index)
76 if PyTuple_Check(tuple)
78 if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
84 PyObject *item = PyTuple_GetItem(tuple, index);
95 char* nltk_getStringFromElement(PyObject* elm)
97 char* ch = (char*) malloc(255);
98 strcpy(ch, PyString_AsString(elm));
102 PyObject* nltk_getFunctionHandle(PyObject* m_module, char * f_name)
104 return PyObject_GetAttrString(m_module, f_name);
107 PyObject* nltk_makeArgsFromString(char* info)
110 //create args tuple struct to fill the arg one by one ,here , only create one string with 1
111 pArgs = PyTuple_New(1);
112 PyTuple_SetItem(pArgs, 0, PyString_FromString(info));
116 PyObject* nltk_makeArgsFromPyObject(PyObject* pyobj)
119 //create args tuple struct to fill the arg one by one ,here ,only create one python object with 1
120 pArgs = PyTuple_New(1);
121 //set a string for item 0 of tuple
122 PyTuple_SetItem(pArgs, 0, pyobj);
126 PyObject* nltk_callFunctionWithArgs(PyObject* m_func, PyObject* args)
128 return PyObject_CallObject(m_func, args);
131 // This is an example of an method to get attribute of module.
132 char* nltk_getattrib(int z)
134 PyObject *pModule, *pFunc;
136 char* ch = (char*) malloc(255);
139 pModule = PyImport_ImportModule("site");
142 //create args tuple struct to fill the arg one by one ,here ,only create one param with 1
143 pArgs = PyTuple_New(1);
144 //set a string for item 0 of tuple
145 PyTuple_SetItem(pArgs, 0, PyString_FromString("Hello World"));
146 //call word_tokenize func with args
147 pFunc = PyObject_GetAttrString(pModule, "USER_SITE");
149 strcpy(ch, PyString_AsString(pFunc));
151 strcpy(ch, "attribute get error\n");
154 strcpy(ch, "nltk module import error\n");