| 43 | } |
| 44 | |
| 45 | std::optional<size_t> PLDAModelObject::addDoc(PyObject* words, PyObject* labels, bool ignoreEmptyWords) |
| 46 | { |
| 47 | if (isPrepared) throw py::RuntimeError{ "cannot add_doc() after train()" }; |
| 48 | auto* inst = getInst<tomoto::IPLDAModel>(); |
| 49 | if (PyUnicode_Check(words)) |
| 50 | { |
| 51 | if (PyErr_WarnEx(PyExc_RuntimeWarning, "`words` should be an iterable of str.", 1)) throw py::ExcPropagation{}; |
| 52 | } |
| 53 | tomoto::RawDoc raw = buildRawDoc(words); |
| 54 | if (labels) |
| 55 | { |
| 56 | if (PyUnicode_Check(labels)) |
| 57 | { |
| 58 | if (PyErr_WarnEx(PyExc_RuntimeWarning, "`labels` should be an iterable of str.", 1)) throw py::ExcPropagation{}; |
| 59 | } |
| 60 | vector<string> labelVec; |
| 61 | if (!py::toCpp(labels, labelVec)) |
| 62 | { |
| 63 | throw py::ValueError{ "`labels` must be an iterable of str." }; |
| 64 | } |
| 65 | raw.misc["labels"] = labelVec; |
| 66 | } |
| 67 | try |
| 68 | { |
| 69 | auto ret = inst->addDoc(raw); |
| 70 | return py::buildPyValue(ret); |
| 71 | } |
| 72 | catch (const tomoto::exc::EmptyWordArgument&) |
| 73 | { |
| 74 | if (ignoreEmptyWords) |
| 75 | { |
| 76 | return std::nullopt; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | throw; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | py::UniqueCObj<DocumentObject> PLDAModelObject::makeDoc(PyObject* words, PyObject* labels) |
| 86 | { |
nothing calls this directly
no test coverage detected