| 42 | } |
| 43 | |
| 44 | std::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 | |
| 87 | py::UniqueCObj<DocumentObject> DMRModelObject::makeDoc(PyObject* words, const std::string& metadata, PyObject* multiMetadata) |
| 88 | { |
nothing calls this directly
no test coverage detected