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

Method extractNgrams

src/python/handler/py_utils.cpp:447–490  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

445}
446
447py::UniqueObj CorpusObject::extractNgrams(size_t minCf, size_t minDf, size_t maxLen, size_t maxCand,
448 float minScore, bool normalized, size_t workers) const
449{
450 if (!isIndependent())
451 throw py::RuntimeError{ "Cannot modify the corpus bound to a topic model." };
452 size_t vSize = vocab->vocabs->size();
453 vector<size_t> cf(vSize),
454 df(vSize),
455 odf(vSize);
456 for (auto& d : docs)
457 {
458 for (auto w : d.words)
459 {
460 if (w == tomoto::non_vocab_id) continue;
461 odf[w] = 1;
462 cf[w]++;
463 }
464
465 for (size_t i = 0; i < df.size(); ++i) df[i] += odf[i];
466 fill(odf.begin(), odf.end(), 0);
467 }
468
469 auto tx = [](const tomoto::RawDoc& raw)
470 {
471 return RawDocWrapper{ raw };
472 };
473 auto docBegin = tomoto::makeTransformIter(docs.begin(), tx);
474 auto docEnd = tomoto::makeTransformIter(docs.end(), tx);
475 auto cands = tomoto::phraser::extractPMINgrams(docBegin, docEnd,
476 cf, df,
477 minCf, minDf, 2, maxLen, maxCand, minScore, normalized
478 );
479
480 auto ret = py::UniqueObj{ PyList_New(0) };
481 for (auto& c : cands)
482 {
483 auto item = py::makeNewObject<CandidateObject>();
484 item->corpus = py::UniqueCObj<CorpusObject>{ (CorpusObject*)this };
485 Py_INCREF(this);
486 item->cand = move(c);
487 PyList_Append(ret.get(), (PyObject*)item.get());
488 }
489 return ret;
490}
491
492// TODO: It loses some ngram patterns. Fix me!
493size_t CorpusObject::concatNgrams(PyObject* cands, const std::string& delimiter)

Callers

nothing calls this directly

Calls 6

makeTransformIterFunction · 0.85
extractPMINgramsFunction · 0.85
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected