Status LegacyReplyBuilder::addOutputDocs(DocumentRange docs) { invariant(_state == State::kOutputDocs); invariant(_allowAddingOutputDocs); auto dataSize = docs.data().length(); auto hasSpace = _hasSpaceFor(dataSize); if (!hasSpace.isOK()) { return hasSpace; } // The temporary obj is used to address the case when where is not enough space. // BSONArray overhead can not be estimated upfront. std::vector<BSONObj> docsTmp{}; std::size_t lenTmp = 0; std::size_t tmpIndex(_currentIndex); for (auto&& it : docs) { docsTmp.emplace_back(it.getOwned()); lenTmp += BSONObjBuilder::numStr(++tmpIndex).length() + 2; // space for storing array index } hasSpace = _hasSpaceFor(dataSize + lenTmp); if (!hasSpace.isOK()) { return hasSpace; } // vector::insert instead of swap allows to call addOutputDoc(s) multiple times _outputDocs.insert(_outputDocs.end(), std::make_move_iterator(docsTmp.begin()), std::make_move_iterator(docsTmp.end())); _currentIndex = tmpIndex; _currentLength += lenTmp; _currentLength += dataSize; return Status::OK(); }
/** * @brief analyze each word in a sentence * */ vector<MorphologicalAnalyze> Dictionary::getMorphologicalAnalyze(const wstring & _sentence) { // tokenize sentence to words vector<wstring> words = getDictionaryTools()->dictionarySplit(_sentence, WORD_DELIMITER); // number of words int n = (int) words.size(); // init morphological info list vector<vector<MorphologicalInfo> > minfos = vector<vector<MorphologicalInfo> >(); vector<int> minfoSize = vector<int>(); // morphological analysis for each word for (int i = 0; i < n; ++i) { // morphological analysis for words[i] shared_ptr<vector<MorphologicalInfo> > minfo = std::make_shared<vector<MorphologicalInfo> >(); trie->getMorphologicalInfoList(words.at(i), minfo); // if no MorphologicalInfo found if (minfo->size() == 0) { // create MorphologicalInfo - NULL MorphologicalInfo info; info.featureListId = 0; // then add to minfo minfo->push_back(info); } minfos.push_back(*minfo); minfoSize.push_back(minfo->size()); } //init maxNGram; int maxNGram = -1; vector<int> maxNGramIndex; // init zero-array length n vector<int> tmpIndex(n, 0); while (1) { // get combination of MorphologicalInfo vector<MorphologicalInfo> _tmpMinfo = vector<MorphologicalInfo>(); for(int i = 0; i < n; ++i) { _tmpMinfo.push_back(minfos.at(i).at(tmpIndex.at(i))); } // get NGram value for the combination int tmpNGramValue = this->getNGramTrie()->morphologicalNGramValue(_tmpMinfo); // compare tmpNGramValue to maxNGram if (tmpNGramValue > maxNGram) { maxNGram = tmpNGramValue; maxNGramIndex = tmpIndex; } // change combination int i = n - 1; int z = 1; while (i >= 0 && z == 1) { if (tmpIndex.at(i) + z < minfoSize.at(i)) { tmpIndex.at(i) += z; z = 0; } else { tmpIndex.at(i) = 0; z = 1; } i--; } if (i < 0 && z == 1) { break; } } // get result //wcout << "maxNGram = " << maxNGram << endl; vector<MorphologicalAnalyze> result = vector<MorphologicalAnalyze>(); for (int i = 0; i < n; ++i) { MorphologicalAnalyze mAnalyze; mAnalyze.minfos = minfos.at(i); mAnalyze.bestVariant = maxNGramIndex.at(i); result.push_back(mAnalyze); } return result; }