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

Method addDoc

src/python/handler/py_LLDA.cpp:46–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

buildRawDocFunction · 0.85
toCppFunction · 0.85
buildPyValueFunction · 0.70

Tested by

no test coverage detected