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

Method addDoc

src/python/handler/py_DMR.cpp:44–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

buildRawDocFunction · 0.85
reprFunction · 0.85
buildPyValueFunction · 0.70

Tested by

no test coverage detected