| 445 | } |
| 446 | |
| 447 | py::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! |
| 493 | size_t CorpusObject::concatNgrams(PyObject* cands, const std::string& delimiter) |
nothing calls this directly
no test coverage detected