add native library source code about nltk
[platform/core/uifw/nlp.git] / nltk_native_library / src / nltk_native_library.c
1 /**
2  * This file contains the exported symbol.
3  */
4 #include "nltk_native_library.h"
5
6 // This is an example of an exported method.
7 void nltk_initialize()
8 {
9     Py_Initialize();
10 }
11
12 void nltk_finalize()
13 {
14     Py_Finalize();
15 }
16
17 PyObject* nltk_getModule(char* m_name)
18 {
19     return PyImport_ImportModuleNoBlock(m_name);
20 }
21
22 int nltk_getSizeFromList(PyObject* list)
23 {
24     if PyList_Check(list)
25     {
26         return PyList_Size(list);
27     }
28     else
29     {
30         return -1;
31     }
32 }
33
34 int nltk_getSizeFromTuple(PyObject* tuple)
35 {
36     if PyTuple_Check(tuple)
37     {
38         return PyTuple_Size(tuple);
39     }
40     else
41     {
42         return -1;
43     }
44 }
45
46 PyObject* nltk_getElementFromListByIndex(PyObject* list, int index)
47 {
48     PyObject* element;
49     if PyList_Check(list)
50     {
51         if (index > (PyList_Size(list)-1) || (index < 0 ))
52         {
53             element = NULL;
54         }
55         else
56         {
57             PyObject *item = PyList_GetItem(list, index);
58             element = item;
59         }
60     }
61     else
62     {
63         element = NULL;
64     }
65     return element;
66 }
67
68 char* nltk_getStringFromListElement(PyObject* elm)
69 {
70     return PyString_AsString(elm);
71 }
72
73 PyObject* nltk_getElementFromTupleByIndex(PyObject* tuple, int index)
74 {
75     PyObject* element;
76     if PyTuple_Check(tuple)
77     {
78         if (index > (PyTuple_Size(tuple)-1) || (index < 0 ))
79         {
80             element = NULL;
81         }
82         else
83         {
84             PyObject *item = PyTuple_GetItem(tuple, index);
85             element = item;
86         }
87     }
88     else
89     {
90         element = NULL;
91     }
92     return element;
93 }
94
95 char* nltk_getStringFromElement(PyObject* elm)
96 {
97     char* ch = (char*) malloc(255);
98     strcpy(ch, PyString_AsString(elm));
99     return ch;
100 }
101
102 PyObject* nltk_getFunctionHandle(PyObject* m_module, char * f_name)
103 {
104     return PyObject_GetAttrString(m_module, f_name);
105 }
106
107 PyObject* nltk_makeArgsFromString(char* info)
108 {
109     PyObject *pArgs;
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));
113     return pArgs;
114 }
115
116 PyObject* nltk_makeArgsFromPyObject(PyObject* pyobj)
117 {
118     PyObject *pArgs;
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);
123     return pArgs;
124 }
125
126 PyObject* nltk_callFunctionWithArgs(PyObject* m_func, PyObject* args)
127 {
128     return PyObject_CallObject(m_func, args);
129 }
130
131 // This is an example of an method to get attribute of module.
132 char* nltk_getattrib(int z)
133 {
134     PyObject *pModule, *pFunc;
135     PyObject *pArgs;
136     char* ch = (char*) malloc(255);
137     if (ch != NULL)
138     {
139         pModule = PyImport_ImportModule("site");
140         if (pModule != NULL)
141         {
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");
148         if (pFunc != NULL) {
149             strcpy(ch, PyString_AsString(pFunc));
150             } else {
151             strcpy(ch, "attribute get error\n");
152             }
153         } else {
154             strcpy(ch, "nltk module import error\n");
155         }
156         return ch;
157     }
158     else
159     {
160         return NULL;
161     }
162 }