-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
py_PLDA.cpp
207 lines (189 loc) · 7.95 KB
/
py_PLDA.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "../TopicModel/PLDA.h"
#include "module.h"
#include "utils.h"
using namespace std;
tomoto::RawDoc::MiscType PLDA_misc_args(TopicModelObject* self, const tomoto::RawDoc::MiscType& o)
{
tomoto::RawDoc::MiscType ret;
ret["labels"] = getValueFromMiscDefault<vector<string>>("labels", o, "`PLDAModel` requires a `labels` value in `Iterable[str]` type.");
return ret;
}
static int PLDA_init(TopicModelObject *self, PyObject *args, PyObject *kwargs)
{
size_t tw = 0, minCnt = 0, minDf = 0, rmTop = 0;
tomoto::PLDAArgs margs;
PyObject* objCorpus = nullptr, *objTransform = nullptr;
PyObject* objAlpha = nullptr, *objSeed = nullptr;
static const char* kwlist[] = { "tw", "min_cf", "min_df", "rm_top", "latent_topics", "topics_per_label", "alpha", "eta",
"seed", "corpus", "transform", nullptr };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|nnnnnnOfOOO", (char**)kwlist, &tw, &minCnt, &minDf, &rmTop,
&margs.numLatentTopics, &margs.numTopicsPerLabel, &objAlpha, &margs.eta, &objSeed, &objCorpus, &objTransform)) return -1;
return py::handleExc([&]()
{
if (objAlpha) margs.alpha = broadcastObj<tomoto::Float>(objAlpha, margs.k,
[=]() { return "`alpha` must be an instance of `float` or `List[float]` with length `k` (given " + py::repr(objAlpha) + ")"; }
);
if (objSeed) margs.seed = py::toCpp<size_t>(objSeed, "`seed` must be an integer or None.");
tomoto::ITopicModel* inst = tomoto::IPLDAModel::create((tomoto::TermWeight)tw, margs);
if (!inst) throw py::ValueError{ "unknown `tw` value" };
self->inst = inst;
self->isPrepared = false;
self->seedGiven = !!objSeed;
self->minWordCnt = minCnt;
self->minWordDf = minDf;
self->removeTopWord = rmTop;
self->initParams = py::buildPyDict(kwlist,
tw, minCnt, minDf, rmTop,
margs.numLatentTopics, margs.numTopicsPerLabel, margs.alpha, margs.eta, margs.seed
);
py::setPyDictItem(self->initParams, "version", getVersion());
insertCorpus(self, objCorpus, objTransform);
return 0;
});
}
static PyObject* PLDA_addDoc(TopicModelObject* self, PyObject* args, PyObject *kwargs)
{
PyObject *argWords, *argLabels = nullptr;
size_t ignoreEmptyWords = 1;
static const char* kwlist[] = { "words", "labels", "ignore_empty_words", nullptr };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Op", (char**)kwlist, &argWords, &argLabels, &ignoreEmptyWords)) return nullptr;
return py::handleExc([&]() -> PyObject*
{
if (!self->inst) throw py::RuntimeError{ "inst is null" };
if (self->isPrepared) throw py::RuntimeError{ "cannot add_doc() after train()" };
auto* inst = static_cast<tomoto::IPLDAModel*>(self->inst);
if (PyUnicode_Check(argWords))
{
if (PyErr_WarnEx(PyExc_RuntimeWarning, "`words` should be an iterable of str.", 1)) return nullptr;
}
tomoto::RawDoc raw = buildRawDoc(argWords);
if (argLabels)
{
if (PyUnicode_Check(argLabels))
{
if (PyErr_WarnEx(PyExc_RuntimeWarning, "`labels` should be an iterable of str.", 1)) return nullptr;
}
raw.misc["labels"] = py::toCpp<vector<string>>(argLabels, "`labels` must be an iterable of str.");
}
try
{
auto ret = inst->addDoc(raw);
return py::buildPyValue(ret);
}
catch (const tomoto::exc::EmptyWordArgument&)
{
if (ignoreEmptyWords)
{
Py_INCREF(Py_None);
return Py_None;
}
else
{
throw;
}
}
});
}
static DocumentObject* PLDA_makeDoc(TopicModelObject* self, PyObject* args, PyObject *kwargs)
{
PyObject *argWords, *argLabels = nullptr;
static const char* kwlist[] = { "words", "labels", nullptr };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &argWords, &argLabels)) return nullptr;
return py::handleExc([&]() -> DocumentObject*
{
if (!self->inst) throw py::RuntimeError{ "inst is null" };
if (!self->isPrepared) throw py::RuntimeError{ "`train()` should be called before `make_doc()`." };
auto* inst = static_cast<tomoto::IPLDAModel*>(self->inst);
if (PyUnicode_Check(argWords))
{
if (PyErr_WarnEx(PyExc_RuntimeWarning, "`words` should be an iterable of str.", 1)) return nullptr;
}
tomoto::RawDoc raw = buildRawDoc(argWords);
if (argLabels)
{
if (PyUnicode_Check(argLabels))
{
if (PyErr_WarnEx(PyExc_RuntimeWarning, "`labels` should be an iterable of str.", 1)) return nullptr;
}
raw.misc["labels"] = py::toCpp<vector<string>>(argLabels, "`labels` must be an iterable of str.");
}
auto doc = inst->makeDoc(raw);
py::UniqueObj corpus{ PyObject_CallFunctionObjArgs((PyObject*)&UtilsCorpus_type, (PyObject*)self, nullptr) };
auto* ret = (DocumentObject*)PyObject_CallFunctionObjArgs((PyObject*)&UtilsDocument_type, corpus.get(), nullptr);
ret->doc = doc.release();
ret->owner = true;
return ret;
});
}
static VocabObject* PLDA_getTopicLabelDict(TopicModelObject* self, void* closure)
{
return py::handleExc([&]()
{
if (!self->inst) throw py::RuntimeError{ "inst is null" };
auto* ret = (VocabObject*)PyObject_CallObject((PyObject*)&UtilsVocab_type, nullptr);
ret->dep = (PyObject*)self;
Py_INCREF(ret->dep);
ret->vocabs = (tomoto::Dictionary*)&static_cast<tomoto::IPLDAModel*>(self->inst)->getTopicLabelDict();
ret->size = -1;
return ret;
});
}
DEFINE_LOADER(PLDA, PLDA_type);
DEFINE_GETTER(tomoto::IPLDAModel, PLDA, getNumLatentTopics);
DEFINE_GETTER(tomoto::IPLDAModel, PLDA, getNumTopicsPerLabel);
PyObject* LDA_getTopicWords(TopicModelObject* self, PyObject* args, PyObject *kwargs);
static PyMethodDef PLDA_methods[] =
{
{ "add_doc", (PyCFunction)PLDA_addDoc, METH_VARARGS | METH_KEYWORDS, LLDA_add_doc__doc__ },
{ "make_doc", (PyCFunction)PLDA_makeDoc, METH_VARARGS | METH_KEYWORDS, LLDA_make_doc__doc__ },
{ "load", (PyCFunction)PLDA_load, METH_STATIC | METH_VARARGS | METH_KEYWORDS, LDA_load__doc__ },
{ "loads", (PyCFunction)PLDA_loads, METH_STATIC | METH_VARARGS | METH_KEYWORDS, LDA_loads__doc__ },
{ "get_topic_words", (PyCFunction)LDA_getTopicWords, METH_VARARGS | METH_KEYWORDS, PLDA_get_topic_words__doc__},
{ nullptr }
};
static PyGetSetDef PLDA_getseters[] = {
{ (char*)"topic_label_dict", (getter)PLDA_getTopicLabelDict, nullptr, PLDA_topic_label_dict__doc__, nullptr },
{ (char*)"latent_topics", (getter)PLDA_getNumLatentTopics, nullptr, PLDA_latent_topics__doc__, nullptr },
{ (char*)"topics_per_label", (getter)PLDA_getNumTopicsPerLabel, nullptr, PLDA_topics_per_label__doc__, nullptr },
{ nullptr },
};
TopicModelTypeObject PLDA_type = { {
PyVarObject_HEAD_INIT(nullptr, 0)
"tomotopy.PLDAModel", /* tp_name */
sizeof(TopicModelObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)TopicModelObject::dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
PLDA___init____doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PLDA_methods, /* tp_methods */
0, /* tp_members */
PLDA_getseters, /* tp_getset */
&LDA_type, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)PLDA_init, /* tp_init */
PyType_GenericAlloc,
PyType_GenericNew,
}, PLDA_misc_args };