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

Method addDoc

src/python/handler/py_GDMR.cpp:78–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76}
77
78std::optional<size_t> GDMRModelObject::addDoc(PyObject* words, PyObject* numericMetadata, const std::string& metadata, PyObject* multiMetadata, bool ignoreEmptyWords)
79{
80 if (isPrepared) throw py::RuntimeError{ "cannot add_doc() after train()" };
81 auto* inst = getInst<tomoto::IGDMRModel>();
82 if (PyUnicode_Check(words))
83 {
84 if (PyErr_WarnEx(PyExc_RuntimeWarning, "`words` should be an iterable of str.", 1)) throw py::ExcPropagation{};
85 }
86
87 tomoto::RawDoc raw = buildRawDoc(words);
88 raw.misc["metadata"] = metadata;
89 if (multiMetadata)
90 {
91 vector<string> multiMetadataValue;
92 if (!py::toCpp<vector<string>>(multiMetadata, multiMetadataValue))
93 {
94 throw py::ValueError{
95 "`multi_metadata` must be an instance of `List[str]` (but given " + py::repr(multiMetadata) + ")"
96 };
97 }
98 raw.misc["multi_metadata"] = move(multiMetadataValue);
99 }
100 vector<tomoto::Float> nmd;
101 if (!py::toCpp(numericMetadata, nmd))
102 {
103 throw py::ValueError{
104 "`numeric_metadata` must be an iterable of float (but given " + py::repr(numericMetadata) + ")"
105 };
106 }
107 for (auto x : nmd)
108 {
109 if (!isfinite(x)) throw py::ValueError{ "`numeric_metadata` has non-finite value (" + py::reprFromCpp(nmd) + ")." };
110 }
111 raw.misc["numeric_metadata"] = move(nmd);
112 try
113 {
114 auto ret = inst->addDoc(raw);
115 return py::buildPyValue(ret);
116 }
117 catch (const tomoto::exc::EmptyWordArgument&)
118 {
119 if (ignoreEmptyWords)
120 {
121 return std::nullopt;
122 }
123 else
124 {
125 throw;
126 }
127 }
128}
129
130py::UniqueCObj<DocumentObject> GDMRModelObject::makeDoc(PyObject* words, PyObject* numericMetadata, const std::string& metadata, PyObject* multiMetadata)
131{

Callers

nothing calls this directly

Calls 5

buildRawDocFunction · 0.85
reprFunction · 0.85
toCppFunction · 0.85
reprFromCppFunction · 0.85
buildPyValueFunction · 0.70

Tested by

no test coverage detected