Example #1
0
void ContactSuggestions::setAddressBook(const std::vector<Contact>& contacts)
{
  clearSuggestions();

  for (unsigned i = 0; i < contacts.size(); ++i)
    addSuggestion(contacts[i].formatted(), contacts[i].formatted());
}
void SuggestionResults::addPrediction(const int *const codePoints, const int codePointCount,
        const int probability) {
    if (probability == NOT_A_PROBABILITY) {
        // Invalid word.
        return;
    }
    addSuggestion(codePoints, codePointCount, probability, Dictionary::KIND_PREDICTION,
            NOT_AN_INDEX, NOT_A_FIRST_WORD_CONFIDENCE);
}
std::shared_ptr<SuggestionList> CompletionTrie::getSuggestions(std::string term,
		const int k) const {
	std::transform(term.begin(), term.end(), term.begin(), ::tolower);
	auto suggestions = suggestionStore->getSuggestionList(k);

	int termPrefixPos = 0;

	std::vector<NodeWithRelativeScoreStore> fittingLeafNodes;
	PackedNode* node = findBestFitting(term, termPrefixPos, fittingLeafNodes);
	std::vector<PackedNode*> v;

	/*
	 * TODO if term is wihtin fittingLeafNodes this must be shown as first element!
	 */

	term = term.substr(0, termPrefixPos);

	if (node == root || node == nullptr) {
		for (NodeWithRelativeScoreStore n : fittingLeafNodes) {
			suggestions->addSuggestion(n);
		}
		return suggestions;
	}

	std::vector<NodeWithRelativeScoreStore> nodesByParentScore;
	nodesByParentScore.push_back( { 0xFFFFFFFF, node, term });

	bool isFirstNode = true;
	while (!nodesByParentScore.empty()) {
		std::sort(nodesByParentScore.begin(), nodesByParentScore.end(),
				NodeWithScoreStoreComparator());

		NodeWithRelativeScoreStore nodeWithParentScore =
				*nodesByParentScore.rbegin();
		nodesByParentScore.pop_back();

//		std::cout << nodeWithParentScore.getString() << std::endl;

		if (nodeWithParentScore.node->isLeafNode()) {
			suggestions->addSuggestion(nodeWithParentScore);
			if (suggestions->isFull()) {
				return suggestions;
			}
		}

		/*
		 * Push first child to priority queue
		 */
		if (nodeWithParentScore.node->firstChildOffsetSize_ != 0) {
			PackedNode* child = getFirstChild(nodeWithParentScore.node);
			nodesByParentScore.push_back( {
					nodeWithParentScore.getRelativeScore(), child,
					nodeWithParentScore.getString() });
		}

		/*
		 * Push next sibling to priority queue
		 */
		if (!isFirstNode) {
			PackedNode* sibling = getNextSibling(nodeWithParentScore.node);
			if (sibling != nullptr) {
				nodesByParentScore.push_back(
						{ nodeWithParentScore.relativeScoreOfParent, sibling,
								nodeWithParentScore.prefix });
			}
		} else {
			isFirstNode = false;
		}
	}

	/*
	 * TODO: where should be put the nodes defining substrings of the requested term?
	 */
	return suggestions;
}