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

Method insertCorpus

src/python/handler/py_LDA.cpp:31–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29}
30
31std::vector<size_t> LDAModelObject::insertCorpus(PyObject* corpusObj, PyObject* transform)
32{
33 vector<size_t> ret;
34 if (!corpusObj || corpusObj == Py_None) return ret;
35 if (!PyObject_TypeCheck(corpusObj, py::Type<CorpusObject>)) throw py::ValueError{ "`corpus` must be an instance of `tomotopy.utils.Corpus`" };
36 auto corpus = (CorpusObject*)corpusObj;
37 bool insert_into_empty = inst->updateVocab(corpus->getVocabDict().getRaw());
38 if (corpus->isIndependent())
39 {
40 for (auto& rdoc : corpus->docs)
41 {
42 tomoto::RawDoc doc;
43 doc.rawStr = rdoc.rawStr;
44 doc.weight = rdoc.weight;
45 doc.docUid = rdoc.docUid;
46
47 for (size_t i = 0; i < rdoc.words.size(); ++i)
48 {
49 if (rdoc.words[i] == tomoto::non_vocab_id) continue;
50
51 if (insert_into_empty) doc.words.emplace_back(rdoc.words[i]);
52 else doc.words.emplace_back(corpus->getVocabDict().mapToNewDict(rdoc.words[i], inst->getVocabDict()));
53
54 if (!doc.rawStr.empty())
55 {
56 doc.origWordPos.emplace_back(rdoc.origWordPos[i]);
57 doc.origWordLen.emplace_back(rdoc.origWordLen[i]);
58 }
59 }
60
61 if (doc.words.empty())
62 {
63 fprintf(stderr, "Adding empty document was ignored.\n");
64 continue;
65 }
66
67 if (!doc.rawStr.empty()) char2Byte(doc.rawStr, doc.origWordPos, doc.origWordLen);
68 doc.misc = convertMisc(transformMisc(rdoc.misc, transform));
69 ret.emplace_back(inst->addDoc(doc));
70 }
71 }
72 else
73 {
74 for (Py_ssize_t i = 0; i < corpus->len(); ++i)
75 {
76 auto& rdoc = (tomoto::DocumentBase&)*corpus->getDoc(i);
77
78 tomoto::RawDoc doc;
79 doc.rawStr = rdoc.rawStr;
80 doc.weight = rdoc.weight;
81 doc.docUid = rdoc.docUid;
82
83 for (size_t i = 0; i < rdoc.words.size(); ++i)
84 {
85 if (rdoc.words[i] == tomoto::non_vocab_id) continue;
86
87 doc.words.emplace_back(corpus->getVocabDict().mapToNewDict(rdoc.words[i], inst->getVocabDict()));
88

Callers

nothing calls this directly

Calls 13

char2ByteFunction · 0.85
transformMiscFunction · 0.85
isIndependentMethod · 0.80
emplace_backMethod · 0.80
mapToNewDictMethod · 0.80
updateVocabMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45
addDocMethod · 0.45
lenMethod · 0.45
getDocMethod · 0.45
makeMiscMethod · 0.45

Tested by

no test coverage detected