src/service.c
src/message.c
src/main.c
+ src/nltk.c
)
#include
--- /dev/null
+#ifndef __NLTK_H__
+#define __NLTK_H__
+
+#include <Python.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define BUF_LEN_128 128
+#define BUF_LEN_256 256
+
+void nltk_initialize();
+void nltk_finalize();
+void nltk_load();
+
+PyObject* nltk_word_tokenize(const char* sentence);
+PyObject* nltk_pos_tag(const char* sentence);
+PyObject* nltk_ne_chunk(const char* sentence);
+PyObject* nltk_language_detect(const char* sentence);
+PyObject* nltk_get_module(const char* name);
+
+int nltk_get_size_from_list(PyObject* list);
+int nltk_get_size_from_tuple(PyObject* tuple);
+PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index);
+PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index);
+char* nltk_get_string_from_element(PyObject* elm);
+PyObject* nltk_get_function_handle(PyObject* module, char * func_name);
+PyObject* nltk_make_args_from_string(const char* info);
+PyObject* nltk_make_args_from_pyobject(PyObject* pyobj);
+PyObject* nltk_make_args_from_strings(char* info, char* tag);
+PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __NLTK_H__ */
#ifndef __service_H__
#define __service_H__
-#include <Python.h>
#include <app_control.h>
-#include <service_app.h>
-
-#define BUF_LEN_128 128
-#define BUF_LEN_256 256
#ifdef __cplusplus
extern "C" {
#endif
-typedef enum {
- NLTK_CMD_NONE = -1,
- NLTK_CMD_TOKENIZE,
- NLTK_CMD_POSTAG,
- NLTK_CMD_NECHUNK,
- NLTK_CMD_LANGDETECT,
- NLTK_CMD_UNKNOWN
-} NLTK_CMDS;
-
-void nltk_initialize();
-void nltk_finalize();
-void nltk_load();
-
-PyObject* nltk_word_tokenize(const char* sentence);
-PyObject* nltk_pos_tag(const char* sentence);
-PyObject* nltk_ne_chunk(const char* sentence);
-PyObject* nltk_language_detect(const char* sentence);
-PyObject* nltk_get_module(const char* name);
-PyObject* nltk_get_function_handle(PyObject* module , char * func_name);
-PyObject* nltk_make_args_from_pyobject(PyObject* pyobj);
-PyObject* nltk_make_args_from_string(const char* info);
-PyObject* nltk_make_args_from_strings(char* info, char* tag);
-PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args);
-int nltk_get_size_from_list(PyObject* list);
-int nltk_get_size_from_tuple(PyObject* tuple);
-PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index);
-PyObject* nltk_get_element_from_list_by_index(PyObject* list, int index);
-char* nltk_get_string_from_element(PyObject* elm);
-
bool service_app_create(void *data);
void service_app_terminate(void *data);
void service_app_control(app_control_h app_control, void *data);
--- /dev/null
+#include "nltk.h"
+#include "nlp_log.h"
+
+static PyObject* globe_nltk = NULL;
+static PyObject* globe_lang = NULL;
+
+void nltk_initialize()
+{
+ PENTER();
+ Py_Initialize();
+}
+
+void nltk_finalize()
+{
+ PENTER();
+ Py_Finalize();
+}
+
+void nltk_load()
+{
+ globe_nltk = nltk_get_module("nltk");
+ if (globe_nltk)
+ PINFO("nltk library loaded success: ");
+ else
+ PERR("Failed to get nltk module");
+
+ globe_lang = nltk_get_module("langdetect");
+ if (globe_lang)
+ PINFO("langdetect library loaded success: ");
+ else
+ PERR("Failed to get langdetect module");
+}
+
+PyObject* nltk_word_tokenize(const char* sentence)
+{
+ PyObject* args = NULL;
+ PyObject* func = NULL;
+ PyObject* lists = NULL;
+ args = nltk_make_args_from_string(sentence);
+ func = nltk_get_function_handle(globe_nltk, "word_tokenize");
+ lists = nltk_call_function_with_args(func, args);
+ Py_DECREF(args);
+ if (func)
+ Py_DECREF(func);
+ return lists;
+}
+
+PyObject* nltk_pos_tag(const char* sentence)
+{
+ PyObject* args = NULL;
+ PyObject* func = NULL;
+ PyObject* wt_result = NULL;
+ PyObject* result = NULL;
+ wt_result = nltk_word_tokenize(sentence);
+ func = nltk_get_function_handle(globe_nltk, "pos_tag");
+ args = nltk_make_args_from_pyobject(wt_result);
+ result = nltk_call_function_with_args(func, args);
+
+ if (args)
+ Py_DECREF(args);
+
+ if (func)
+ Py_DECREF(func);
+
+ if (wt_result)
+ Py_DECREF(wt_result);
+
+ return result;
+}
+
+PyObject* nltk_ne_chunk(const char* sentence)
+{
+ PyObject* args = NULL;
+ PyObject* pt_result = NULL;
+ PyObject* tmp_result = NULL;
+ PyObject* result = NULL;
+ PyObject* func = NULL;
+ PyObject* lv_func = NULL;
+ pt_result = nltk_pos_tag(sentence);
+ args = nltk_make_args_from_pyobject(pt_result);
+ func = nltk_get_function_handle(globe_nltk, "ne_chunk");
+ tmp_result = nltk_call_function_with_args(func, args);
+ lv_func = nltk_get_function_handle(tmp_result, "leaves");
+ result = nltk_call_function_with_args(lv_func, NULL);
+
+ if (args)
+ Py_DECREF(args);
+
+ if (func)
+ Py_DECREF(func);
+
+ if (pt_result)
+ Py_DECREF(pt_result);
+
+ if (tmp_result)
+ Py_DECREF(tmp_result);
+
+ if (lv_func)
+ Py_DECREF(lv_func);
+
+ return result;
+}
+
+PyObject* nltk_language_detect(const char* sentence)
+{
+ PyObject* args = NULL;
+ PyObject* func = NULL;
+ PyObject* result = NULL;
+ args = nltk_make_args_from_string(sentence);
+ func = nltk_get_function_handle(globe_lang,"detect");
+ result = nltk_call_function_with_args(func, args);
+ Py_DECREF(args);
+ if (func)
+ Py_DECREF(func);
+
+ return result;
+}
+
+PyObject* nltk_get_module(const char* name)
+{
+ PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
+ return PyImport_ImportModuleNoBlock(name);
+}
+
+int nltk_get_size_from_list(PyObject* list)
+{
+ if (PyList_Check(list))
+ {
+ return PyList_Size(list);
+ }
+ else
+ {
+ return -1;
+ }
+}
+
+int nltk_get_size_from_tuple(PyObject* tuple)
+{
+ if (PyTuple_Check(tuple))
+ {
+ return PyTuple_Size(tuple);
+ }
+ else
+ {
+ return -1;
+ }
+}
+
+PyObject* nltk_get_element_from_list_by_index(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;
+}
+
+PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
+{
+ PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
+ 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_get_string_from_element(PyObject* elm)
+{
+ PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
+ char* ch = (char*) malloc(BUF_LEN_256);
+ if (ch == NULL)
+ {
+ PERR("malloc failed");
+ return ch;
+ }
+ memset(ch, 0, BUF_LEN_256);
+
+ const char *tmp_str = PyUnicode_AsUTF8(elm);
+ if (tmp_str == NULL) {
+ PERR("failed to get char from PyObject");
+ free(ch);
+ return NULL;
+ }
+
+ strncpy(ch, tmp_str, BUF_LEN_256-1);
+ return ch;
+}
+
+PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
+{
+ PRET_VM(!module, NULL, "Input parameter [module] is NULL!");
+ PRET_VM(!func_name, NULL, "Input parameter [func_name] is NULL!");
+ return PyObject_GetAttrString(module, func_name);
+}
+
+PyObject* nltk_make_args_from_string(const char* info)
+{
+ PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
+ 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, PyUnicode_FromString(info));
+ return pArgs;
+}
+
+PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
+{
+ PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
+ 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_make_args_from_strings(char* info, char* tag)
+{
+ PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
+ PyObject *pArgs;
+ //create args tuple struct to fill the arg one by one ,here , only create one string with 1
+ pArgs = PyTuple_New(2);
+ PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(info));
+ PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(tag));
+ return pArgs;
+}
+
+PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
+{
+ PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
+ return PyObject_CallObject(func, args);
+}
#include "service.h"
#include "nlp_log.h"
#include "message.h"
+#include "nltk.h"
-static PyObject* globe_nltk = NULL;
-static PyObject* globe_lemm = NULL;
-static PyObject* globe_lang = NULL;
+typedef enum {
+ NLTK_CMD_NONE = -1,
+ NLTK_CMD_TOKENIZE,
+ NLTK_CMD_POSTAG,
+ NLTK_CMD_NECHUNK,
+ NLTK_CMD_LANGDETECT,
+ NLTK_CMD_UNKNOWN
+} NLTK_CMDS;
int sec = 180.0;
static Ecore_Timer *service_close_timer = NULL;
__destroy_client(client);
}
-void nltk_load()
-{
- globe_nltk = nltk_get_module("nltk");
- if (globe_nltk)
- PINFO("nltk library loaded success: ");
- else
- PERR("Failed to get nltk module");
-
- globe_lemm = nltk_get_module("nltk.stem");
- if (globe_lemm)
- PINFO("nltk stem library loaded success: ");
- else
- PERR("Failed to get nltk.stem module");
-
- globe_lang = nltk_get_module("langdetect");
- if (globe_lang)
- PINFO("langdetect library loaded success: ");
- else
- PERR("Failed to get nltk.stem module");
-}
-
bool service_app_create(void *data)
{
nltk_initialize();
/*APP_EVENT_LOW_MEMORY*/
}
-void nltk_initialize()
-{
- PENTER();
- Py_Initialize();
-}
-
-void nltk_finalize()
-{
- PENTER();
- Py_Finalize();
-}
-
-PyObject* nltk_word_tokenize(const char* sentence)
-{
- PyObject* args = NULL;
- PyObject* func = NULL;
- PyObject* lists = NULL;
- args = nltk_make_args_from_string(sentence);
- func = nltk_get_function_handle(globe_nltk, "word_tokenize");
- lists = nltk_call_function_with_args(func, args);
- Py_DECREF(args);
- if (func)
- Py_DECREF(func);
- return lists;
-}
-
-PyObject* nltk_pos_tag(const char* sentence)
-{
- PyObject* args = NULL;
- PyObject* func = NULL;
- PyObject* wt_result = NULL;
- PyObject* result = NULL;
- wt_result = nltk_word_tokenize(sentence);
- func = nltk_get_function_handle(globe_nltk, "pos_tag");
- args = nltk_make_args_from_pyobject(wt_result);
- result = nltk_call_function_with_args(func, args);
-
- if (args)
- Py_DECREF(args);
-
- if (func)
- Py_DECREF(func);
-
- if (wt_result)
- Py_DECREF(wt_result);
-
- return result;
-}
-
-PyObject* nltk_ne_chunk(const char* sentence)
-{
- PyObject* args = NULL;
- PyObject* pt_result = NULL;
- PyObject* tmp_result = NULL;
- PyObject* result = NULL;
- PyObject* func = NULL;
- PyObject* lv_func = NULL;
- pt_result = nltk_pos_tag(sentence);
- args = nltk_make_args_from_pyobject(pt_result);
- func = nltk_get_function_handle(globe_nltk, "ne_chunk");
- tmp_result = nltk_call_function_with_args(func, args);
- lv_func = nltk_get_function_handle(tmp_result, "leaves");
- result = nltk_call_function_with_args(lv_func, NULL);
-
- if (args)
- Py_DECREF(args);
-
- if (func)
- Py_DECREF(func);
-
- if (pt_result)
- Py_DECREF(pt_result);
-
- if (tmp_result)
- Py_DECREF(tmp_result);
-
- if (lv_func)
- Py_DECREF(lv_func);
-
- return result;
-}
-
-PyObject* nltk_language_detect(const char* sentence)
-{
- PyObject* args = NULL;
- PyObject* func = NULL;
- PyObject* result = NULL;
- args = nltk_make_args_from_string(sentence);
- func = nltk_get_function_handle(globe_lang,"detect");
- result = nltk_call_function_with_args(func, args);
- Py_DECREF(args);
- if (func)
- Py_DECREF(func);
-
- return result;
-}
-
-PyObject* nltk_get_module(const char* name)
-{
- PRET_VM(!name, NULL, "Input parameter [name] is NULL!");
- return PyImport_ImportModuleNoBlock(name);
-}
-
-int nltk_get_size_from_list(PyObject* list)
-{
- if (PyList_Check(list))
- {
- return PyList_Size(list);
- }
- else
- {
- return -1;
- }
-}
-
-int nltk_get_size_from_tuple(PyObject* tuple)
-{
- if (PyTuple_Check(tuple))
- {
- return PyTuple_Size(tuple);
- }
- else
- {
- return -1;
- }
-}
-
-PyObject* nltk_get_element_from_list_by_index(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;
-}
-
-PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
-{
- PRET_VM(!tuple, NULL, "Input parameter [tuple] is NULL!");
- 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_get_string_from_element(PyObject* elm)
-{
- PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
- char* ch = (char*) malloc(BUF_LEN_256);
- if (ch == NULL)
- {
- PERR("malloc failed");
- return ch;
- }
- memset(ch, 0, BUF_LEN_256);
-
- const char *tmp_str = PyUnicode_AsUTF8(elm);
- if (tmp_str == NULL) {
- PERR("failed to get char from PyObject");
- free(ch);
- return NULL;
- }
-
- strncpy(ch, tmp_str, BUF_LEN_256-1);
- return ch;
-}
-
-PyObject* nltk_get_function_handle(PyObject* module, char * func_name)
-{
- PRET_VM(!module, NULL, "Input parameter [module] is NULL!");
- PRET_VM(!func_name, NULL, "Input parameter [func_name] is NULL!");
- return PyObject_GetAttrString(module, func_name);
-}
-
-PyObject* nltk_make_args_from_string(const char* info)
-{
- PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
- 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, PyUnicode_FromString(info));
- return pArgs;
-}
-
-PyObject* nltk_make_args_from_pyobject(PyObject* pyobj)
-{
- PRET_VM(!pyobj, NULL, "Input parameter [pyobj] is NULL!");
- 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_make_args_from_strings(char* info, char* tag)
-{
- PRET_VM(!info, NULL, "Input parameter [info] is NULL!");
- PyObject *pArgs;
- //create args tuple struct to fill the arg one by one ,here , only create one string with 1
- pArgs = PyTuple_New(2);
- PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(info));
- PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(tag));
- return pArgs;
-}
-
-PyObject* nltk_call_function_with_args(PyObject* func, PyObject* args)
-{
- PRET_VM(!func, NULL, "Input parameter [func] is NULL!");
- return PyObject_CallObject(func, args);
-}
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
-#include "../../service/inc/service.h"
+#include "../../service/inc/nltk.h"
static int g_init = false;