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

Method GDMRModelObject

src/python/handler/py_GDMR.cpp:20–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20GDMRModelObject::GDMRModelObject(size_t tw, size_t minCnt, size_t minDf, size_t rmTop,
21 size_t k, PyObject* degrees, PyObject* alpha, float eta, float sigma, float sigma0, float alphaEps,
22 float orderDecay, PyObject* range,
23 PyObject* seed, PyObject* corpus, PyObject* transform)
24{
25 tomoto::GDMRArgs margs;
26 margs.k = k;
27 if (alpha) margs.alpha = broadcastObj<tomoto::Float>(alpha, margs.k,
28 [&]() { return "`alpha` must be an instance of `float` or `List[float]` with length `k` (given " + py::repr(alpha) + ")"; }
29 );
30 margs.eta = eta;
31 margs.sigma = sigma;
32 margs.sigma0 = sigma0;
33 margs.alphaEps = alphaEps;
34 margs.orderDecay = orderDecay;
35 if (seed && seed != Py_None && !py::toCpp<size_t>(seed, margs.seed))
36 {
37 throw py::ValueError{ "`seed` must be an integer or None." };
38 }
39
40 if (degrees && degrees != Py_None && !py::toCpp<vector<uint64_t>>(degrees, margs.degrees))
41 {
42 throw py::ValueError{ "`degrees` must be an iterable of int." };
43 }
44
45 auto inst = tomoto::IGDMRModel::create((tomoto::TermWeight)tw, margs);
46 if (!inst) throw py::ValueError{ "unknown `tw` value" };
47 this->inst = std::move(inst);
48 this->isPrepared = false;
49 this->seedGiven = !!seed;
50 this->minWordCnt = minCnt;
51 this->minWordDf = minDf;
52 this->removeTopWord = rmTop;
53
54 if (range && range != Py_None)
55 {
56 vector<tomoto::Float> vMin, vMax;
57 py::UniqueObj rangeIter{ PyObject_GetIter(range) }, item;
58 if (!rangeIter) throw py::ValueError{ "`metadata_range` must be a list of pairs." };
59 while (item = py::UniqueObj{ PyIter_Next(rangeIter.get()) })
60 {
61 vector<tomoto::Float> r;
62 if (!py::toCpp(item.get(), r))
63 {
64 throw py::ValueError{ "`metadata_range` must be a list of pairs." };
65 }
66 if (r.size() != 2) throw py::ValueError{ "`metadata_range` must be a list of pairs." };
67 vMin.emplace_back(r[0]);
68 vMax.emplace_back(r[1]);
69 }
70 if (vMin.size() != margs.degrees.size()) throw py::ValueError{ "`len(metadata_range)` must be equal to `len(degrees)`" };
71
72 inst->setMdRange(vMin, vMax);
73 }
74
75 insertCorpus(corpus, transform);
76}
77

Callers

nothing calls this directly

Calls 6

reprFunction · 0.85
toCppFunction · 0.85
emplace_backMethod · 0.80
setMdRangeMethod · 0.80
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected