13#if defined(HAVE_READLINE) && defined(HAVE_READLINE_READLINE_H)
21#define LOOKUPTABLE "common.lookuptable"
22#define KEYWORD_VECTOR "common.keyword_vector"
23#define PRD_RUNNER "predictor_runner"
24#define IS_LOOKUP_INITIALISED "is_lookup_initialised"
25#define INIT_TABLE_ON_SYSTEM "init_table_on_system"
26#define GET_PREDICTION "get_prediction"
27#define READ_DICTIONARY "read_dictionary"
28#define CREATE_TABLE "create_table"
29#define PYTPATH(B) sprintf(B, "%s/ml_python", DATA_PATH)
30#define SING_BIN(B) sprintf(B, "%s/Singular", BIN_PATH)
31#define SING_EXT_SCRIPT(B) sprintf(B, "%s/ml_singular/extract.lib", \
36PyObject *_call_python_function(
char *module,
char *func);
37PyObject *_call_python_function_args(
char *module,
char *func, PyObject *pArgs);
38ml_internal *_get_internals();
40int _set_vectors_file_list();
59int ml_is_initialised()
62 PyObject *pValue =
NULL;
64 if (!Py_IsInitialized())
return 0;
66 pValue = _call_python_function(LOOKUPTABLE, IS_LOOKUP_INITIALISED);
68 if (pValue ==
NULL)
return 0;
69 if (!PyBool_Check(pValue)) {
75 t_value = PyObject_IsTrue(pValue);
85 if (!internal_obs.pDictionary)
return 0;
86 if (!internal_obs.pVectors)
return 0;
87 if (!internal_obs.pFile_list)
return 0;
105 PyObject *pValue =
NULL;
106 PyObject *pPath =
NULL;
107 PyObject *pMyPath =
NULL;
109 PyObject *pName =
NULL;
110 PyObject *pModule =
NULL;
111 PyObject *pArgs =
NULL;
114 if (!Py_IsInitialized()) {
118 pName = PyString_FromString(
"sys");
120 pModule = PyImport_Import(pName);
122 if (pModule ==
NULL){
124 fprintf(stderr,
"Failed to load \"%s\"\n",
"sys");
129 pPath = PyObject_GetAttrString(pModule,
"path");
132 fprintf(stderr,
"Failed to get python path list\n");
140 spath = PyString_AsString(
pString);
145 if (!strstr(spath, buffer)) {
146 pMyPath = PyString_FromString(buffer);
149 PyList_Append(pPath, pMyPath);
158 pArgs = PyTuple_New(2);
159 SING_EXT_SCRIPT(buffer);
160 pString = PyString_FromString(buffer);
161 PyTuple_SetItem(pArgs, 0,
pString);
164 pString = PyString_FromString(buffer);
165 PyTuple_SetItem(pArgs, 1,
pString);
167 pValue = _call_python_function_args(LOOKUPTABLE,
168 INIT_TABLE_ON_SYSTEM,
172 if (pValue ==
NULL)
return 0;
174 if (!_set_dictionary())
return 0;
175 if (!_set_vectors_file_list())
return 0;
188 if (!Py_IsInitialized())
191 Py_XDECREF(internal_obs.pDictionary);
192 Py_XDECREF(internal_obs.pVectors);
193 Py_XDECREF(internal_obs.pFile_list);
194 internal_obs.pDictionary =
NULL;
195 internal_obs.pVectors =
NULL;
196 internal_obs.pFile_list =
NULL;
217int ml_make_prediction(
char *filename,
218 char *prediction_buffers[],
220 char *(*custom_strdup)(
const char *))
222 PyObject *pFName =
NULL;
223 PyObject *pArgs =
NULL;
224 PyObject *pValue =
NULL;
229 pFName = PyString_FromString(filename);
232 fprintf(stderr,
"This is weird\n");
236 pArgs = PyTuple_New(4);
238 fprintf(stderr,
"This is also weird\n");
243 PyTuple_SetItem(pArgs, 0, pFName);
247 PyTuple_SetItem(pArgs, 1, internal_obs.pDictionary);
248 Py_INCREF(internal_obs.pDictionary);
249 PyTuple_SetItem(pArgs, 2, internal_obs.pVectors);
250 Py_INCREF(internal_obs.pVectors);
251 PyTuple_SetItem(pArgs, 3, internal_obs.pFile_list);
252 Py_INCREF(internal_obs.pFile_list);
254 pValue = _call_python_function_args(PRD_RUNNER,
262 if (!PyList_Check(pValue)) {
263 printf(
"Expected a list for prediction.\n");
267 if (PyList_Size(pValue) != 5) {
268 printf(
"List length is supposed to be five, but is %d.\n",
269 (
int)PyList_Size(pValue));
274 for (
i = 0;
i < 5;
i++) {
275 pString = PyObject_Str(PyList_GetItem(pValue,
i));
276 prediction_buffers[
i] = custom_strdup(PyString_AsString(
pString));
279 pred_len[
i] = strlen(prediction_buffers[
i]);
299PyObject *_call_python_function(
char *module,
char *func)
301 PyObject *pArgs = PyTuple_New(0);
302 PyObject *retvalue = _call_python_function_args(module, func, pArgs);
318PyObject *_call_python_function_args(
char *module,
char *func, PyObject *pArgs)
321 PyObject *pValue =
NULL;
323 if (!Py_IsInitialized()) {
325 printf(
"I don't like this\n");
329 pName = PyString_FromString(module);
331 pModule = PyImport_Import(pName);
334 if (pModule ==
NULL){
336 fprintf(stderr,
"Failed to load \"%s\"\n", module);
340 pFunc = PyObject_GetAttrString(pModule, func);
341 if (!pFunc || !PyCallable_Check(pFunc)) {
343 if(PyErr_Occurred()) {
347 "Cannot find function \"%s\"\n",
358 pValue = PyObject_CallObject(pFunc, pArgs);
363 if (pValue ==
NULL) {
364 printf(
"No return for function\n");
374ml_internal *_get_internals()
376 return &internal_obs;
389 PyObject *pValue =
NULL;
391 if (internal_obs.pDictionary) {
395 pValue = _call_python_function(KEYWORD_VECTOR, READ_DICTIONARY);
399 internal_obs.pDictionary = pValue;
413int _set_vectors_file_list()
415 PyObject *pValue =
NULL;
416 PyObject *pVal1 =
NULL, *pVal2 =
NULL;
418 if (internal_obs.pVectors && internal_obs.pFile_list) {
423 Py_XDECREF(internal_obs.pVectors);
424 Py_XDECREF(internal_obs.pFile_list);
425 internal_obs.pVectors =
NULL;
426 internal_obs.pFile_list =
NULL;
428 pValue = _call_python_function(LOOKUPTABLE, CREATE_TABLE);
432 pVal1 = PyTuple_GetItem(pValue, 0);
433 pVal2 = PyTuple_GetItem(pValue, 1);
443 if (!pVal1 || !pVal2) {
448 internal_obs.pVectors = pVal1;
449 internal_obs.pFile_list = pVal2;
Function definitions for using python to do machine learning in Singular.