-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBTM.cpp
More file actions
107 lines (102 loc) · 2.66 KB
/
BTM.cpp
File metadata and controls
107 lines (102 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "BTM.h"
BTM_DLL PyObject *
initmodel(PyObject *self, PyObject* args)
{
int K;
int W;
double a;
double b;
int n_iter;
int save_step;
if(! PyArg_ParseTuple(args, "iiddii", &K, &W, &a, &b, &n_iter, &save_step))
return NULL;
Model* model = new Model(K, W, a, b, n_iter, save_step);
return PyInt_FromSize_t((unsigned long long)model);
}
BTM_DLL PyObject * train(PyObject *self, PyObject*args)
{
cout << "train";
char* file;
unsigned long long model;
if( !PyArg_ParseTuple(args, "Ks", &model, &file))
return NULL;
//reinterpreter cast
Model* rmodel = (Model*)model;
rmodel->train(file);
Py_INCREF(Py_None);
return Py_None;
}
BTM_DLL PyObject * savemodel(PyObject *self, PyObject*args)
{
cout << "savemodel";
char* file;
unsigned long long model;
if (!PyArg_ParseTuple(args, "Ks", &model, &file))
return NULL;
//reinterpreter cast
Model* rmodel = (Model*)model;
rmodel->savemodel(file);
Py_INCREF(Py_None);
return Py_None;
}
BTM_DLL PyObject * loadmodel(PyObject *self, PyObject*args)
{
cout << "savemodel";
char* file;
unsigned long long model;
if (!PyArg_ParseTuple(args, "Ks", &model, &file))
return NULL;
//reinterpreter cast
Model* rmodel = (Model*)model;
rmodel->loadmodel(file);
Py_INCREF(Py_None);
return Py_None;
}
BTM_DLL PyObject * predict(PyObject *self, PyObject*args)
{
cout << "predict";
vector<int> doc;
unsigned long long model;
PyObject*tmp;
if(!PyArg_ParseTuple(args, "KO", &model, &tmp))
return NULL;
int llength = PyObject_Size(tmp);
for (int i = 0; i < llength; i++)
{
PyObject* item = PyList_GetItem(tmp, i);
int id = _PyInt_AsInt(item);
doc.push_back(id);
}
//reinterpreter cast
Model* rmodel = (Model*)model;
auto topics=rmodel->predict(doc);
PyObject*pylist= PyList_New(0);
for(vector<double>::iterator iter=topics.begin();iter!=topics.end();iter++)
{
double tmp = *iter;
PyList_Append(pylist, PyFloat_FromDouble(tmp));
}
return pylist;
}
BTM_DLL PyObject * modeldel(PyObject *self, PyObject*args)
{
unsigned long long model;
if(! PyArg_ParseTuple(args, "K", &model))
return NULL;
Model *rmodel = (Model*)model;
delete rmodel;
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef BTMMethods[] = {
{ "initmodel", initmodel, METH_VARARGS,"initmodel." },
{ "delete",modeldel,METH_VARARGS,"deletemodel." },
{"predict",predict,METH_VARARGS,"predict"},
{"train",train,METH_VARARGS ,"train"},
{ "savemodel",savemodel,METH_VARARGS,"save" },
{ "loadmodel",loadmodel,METH_VARARGS ,"load" },
{ NULL, NULL }
};
BTM_DLL void initBTM(void){
Py_InitModule("BTM", BTMMethods);
}