From 4279e44a88a7103ad278b0272264f98a43b6a8dd Mon Sep 17 00:00:00 2001 From: "xiaochn.wang" Date: Fri, 25 May 2018 12:42:28 +0800 Subject: [PATCH] add native library source code about nltk Change-Id: Ia79db91a8333e9975a1669cecfe02b3d32353bac Signed-off-by: xiaochn.wang --- nltk_native_library/.cproject | 641 ++++++++++++++++++++++++++ nltk_native_library/.project | 47 ++ nltk_native_library/.tproject | 12 + nltk_native_library/inc/nltk_native_library.h | 35 ++ nltk_native_library/project_def.prop | 11 + nltk_native_library/src/nltk_native_library.c | 162 +++++++ 6 files changed, 908 insertions(+) create mode 100644 nltk_native_library/.cproject create mode 100644 nltk_native_library/.project create mode 100644 nltk_native_library/.tproject create mode 100644 nltk_native_library/inc/nltk_native_library.h create mode 100644 nltk_native_library/project_def.prop create mode 100644 nltk_native_library/src/nltk_native_library.c diff --git a/nltk_native_library/.cproject b/nltk_native_library/.cproject new file mode 100644 index 0000000..258ce2f --- /dev/null +++ b/nltk_native_library/.cproject @@ -0,0 +1,641 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nltk_native_library/.project b/nltk_native_library/.project new file mode 100644 index 0000000..7a28b5a --- /dev/null +++ b/nltk_native_library/.project @@ -0,0 +1,47 @@ + + + nltk_native_library + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + 1526457865371 + + 26 + + org.eclipse.ui.ide.multiFilter + 1.0-projectRelativePath-matches-false-false-*/.tpk + + + + 1526457865381 + + 6 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-project_def.prop + + + + diff --git a/nltk_native_library/.tproject b/nltk_native_library/.tproject new file mode 100644 index 0000000..468a834 --- /dev/null +++ b/nltk_native_library/.tproject @@ -0,0 +1,12 @@ + + + + + mobile-4.0 + + + + + + + diff --git a/nltk_native_library/inc/nltk_native_library.h b/nltk_native_library/inc/nltk_native_library.h new file mode 100644 index 0000000..581121b --- /dev/null +++ b/nltk_native_library/inc/nltk_native_library.h @@ -0,0 +1,35 @@ +#ifndef _NLTK_NATIVE_LIBRARY_H_ +#define _NLTK_NATIVE_LIBRARY_H_ + +/** + * This header file is included to define _EXPORT_. + */ +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// This method is exported from nltk_native_library.so +EXPORT_API void nltk_initialize(); +EXPORT_API void nltk_finalize(); +EXPORT_API PyObject* nltk_getModule(char* m_name); +EXPORT_API PyObject* nltk_getFunctionHandle(PyObject* m_module , char * f_name); +EXPORT_API PyObject* nltk_makeArgsFromPyObject(PyObject* pyobj); +EXPORT_API PyObject* nltk_makeArgsFromString(char* info); +EXPORT_API PyObject* nltk_callFunctionWithArgs(PyObject* m_func, PyObject* args); +EXPORT_API int nltk_getSizeFromList(PyObject* list); +EXPORT_API int nltk_getSizeFromTuple(PyObject* tuple); +EXPORT_API PyObject* nltk_getElementFromTupleByIndex(PyObject* tuple , int index ); +EXPORT_API PyObject* nltk_getElementFromListByIndex(PyObject* list, int index); +EXPORT_API char* nltk_getStringFromElement(PyObject* elm); +EXPORT_API char* nltk_getattrib(int z); + +#ifdef __cplusplus +} +#endif +#endif // _NLTK_NATIVE_LIBRARY_H_ + diff --git a/nltk_native_library/project_def.prop b/nltk_native_library/project_def.prop new file mode 100644 index 0000000..7082150 --- /dev/null +++ b/nltk_native_library/project_def.prop @@ -0,0 +1,11 @@ +APPNAME = nltk_native_library + +type = sharedLib +profile = mobile-4.0 + +USER_SRCS = src/nltk_native_library.c +USER_DEFS = +USER_INC_DIRS = inc +USER_OBJS = +USER_LIBS = +USER_EDCS = diff --git a/nltk_native_library/src/nltk_native_library.c b/nltk_native_library/src/nltk_native_library.c new file mode 100644 index 0000000..935bc1e --- /dev/null +++ b/nltk_native_library/src/nltk_native_library.c @@ -0,0 +1,162 @@ +/** + * This file contains the exported symbol. + */ +#include "nltk_native_library.h" + +// This is an example of an exported method. +void nltk_initialize() +{ + Py_Initialize(); +} + +void nltk_finalize() +{ + Py_Finalize(); +} + +PyObject* nltk_getModule(char* m_name) +{ + return PyImport_ImportModuleNoBlock(m_name); +} + +int nltk_getSizeFromList(PyObject* list) +{ + if PyList_Check(list) + { + return PyList_Size(list); + } + else + { + return -1; + } +} + +int nltk_getSizeFromTuple(PyObject* tuple) +{ + if PyTuple_Check(tuple) + { + return PyTuple_Size(tuple); + } + else + { + return -1; + } +} + +PyObject* nltk_getElementFromListByIndex(PyObject* list, int index) +{ + PyObject* element; + if PyList_Check(list) + { + if (index > (PyList_Size(list)-1) || (index < 0 )) + { + element = NULL; + } + else + { + PyObject *item = PyList_GetItem(list, index); + element = item; + } + } + else + { + element = NULL; + } + return element; +} + +char* nltk_getStringFromListElement(PyObject* elm) +{ + return PyString_AsString(elm); +} + +PyObject* nltk_getElementFromTupleByIndex(PyObject* tuple, int index) +{ + PyObject* element; + if PyTuple_Check(tuple) + { + if (index > (PyTuple_Size(tuple)-1) || (index < 0 )) + { + element = NULL; + } + else + { + PyObject *item = PyTuple_GetItem(tuple, index); + element = item; + } + } + else + { + element = NULL; + } + return element; +} + +char* nltk_getStringFromElement(PyObject* elm) +{ + char* ch = (char*) malloc(255); + strcpy(ch, PyString_AsString(elm)); + return ch; +} + +PyObject* nltk_getFunctionHandle(PyObject* m_module, char * f_name) +{ + return PyObject_GetAttrString(m_module, f_name); +} + +PyObject* nltk_makeArgsFromString(char* info) +{ + PyObject *pArgs; + //create args tuple struct to fill the arg one by one ,here , only create one string with 1 + pArgs = PyTuple_New(1); + PyTuple_SetItem(pArgs, 0, PyString_FromString(info)); + return pArgs; +} + +PyObject* nltk_makeArgsFromPyObject(PyObject* pyobj) +{ + PyObject *pArgs; + //create args tuple struct to fill the arg one by one ,here ,only create one python object with 1 + pArgs = PyTuple_New(1); + //set a string for item 0 of tuple + PyTuple_SetItem(pArgs, 0, pyobj); + return pArgs; +} + +PyObject* nltk_callFunctionWithArgs(PyObject* m_func, PyObject* args) +{ + return PyObject_CallObject(m_func, args); +} + +// This is an example of an method to get attribute of module. +char* nltk_getattrib(int z) +{ + PyObject *pModule, *pFunc; + PyObject *pArgs; + char* ch = (char*) malloc(255); + if (ch != NULL) + { + pModule = PyImport_ImportModule("site"); + if (pModule != NULL) + { + //create args tuple struct to fill the arg one by one ,here ,only create one param with 1 + pArgs = PyTuple_New(1); + //set a string for item 0 of tuple + PyTuple_SetItem(pArgs, 0, PyString_FromString("Hello World")); + //call word_tokenize func with args + pFunc = PyObject_GetAttrString(pModule, "USER_SITE"); + if (pFunc != NULL) { + strcpy(ch, PyString_AsString(pFunc)); + } else { + strcpy(ch, "attribute get error\n"); + } + } else { + strcpy(ch, "nltk module import error\n"); + } + return ch; + } + else + { + return NULL; + } +} -- 2.7.4