MCPcopy Create free account
hub / github.com/bab2min/tomotopy / addDoc

Method addDoc

src/python/handler/py_PLDA.cpp:45–83  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43}
44
45std::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
85py::UniqueCObj<DocumentObject> PLDAModelObject::makeDoc(PyObject* words, PyObject* labels)
86{

Callers

nothing calls this directly

Calls 3

buildRawDocFunction · 0.85
toCppFunction · 0.85
buildPyValueFunction · 0.70

Tested by

no test coverage detected