void SearchEngine::known(const QVector<QString>& result,
                         DictCandidates& candidates)
{
    Dictionary::const_iterator end = dictionary.end();

    for (auto& str : result)
    {
        //dictionary = QHash<string, int> - word is a key, frequency of use as per .txt file - is a value
        Dictionary::const_iterator foundWord = this->dictionary.find(str);

        // candidates = Dictionary<int, string> - frequency of use is a key, word is a value. We need
        // to sort all results, that were found in main dictionary (QHash)
        if (foundWord != end)
            candidates[foundWord.value()] = foundWord.key();
    }
}
Ejemplo n.º 2
0
std::string stringify(State const& state, Value const& value, std::vector<int64_t> nest) {
	std::string result = "[1]";
	bool dots = false;
	switch(value.type)
	{
		case Type::Null:
			return "NULL";
		case Type::Raw:
			return stringifyVector(state, (Raw const&)value);
		case Type::Logical:
			return stringifyVector(state, (Logical const&)value);
		case Type::Integer:
			return stringifyVector(state, (Integer const&)value);
		case Type::Double:
			return stringifyVector(state, (Double const&)value);
		case Type::Character:
			return stringifyVector(state, (Character const&)value);
		
		case Type::List:
		{
			List const& v = (List const&)value;

			int64_t length = v.length;
			if(length == 0) return "list()";
			if(length > 100) { dots = true; length = 100; }
			result = "";

            std::string prefix = "";
            for(int64_t i = 0; i < nest.size(); ++i) {
                prefix = prefix + "[[" + intToStr(nest[i]) + "]]";
            }

            nest.push_back(0);
			for(int64_t i = 0; i < length; i++) {
                nest.back()++;
				result = result + prefix + "[[" + intToStr(i+1) + "]]\n";
				if(!List::isNA(v[i])) 
                    result = result + stringify(state, v[i], nest);
				result = result + "\n";
				if(i < length-1) result = result + "\n";
			}
			if(dots) result = result + " ... (" + intToStr(v.length) + " elements)";
			return result;
		}
		case Type::Function:
		{
			result = state.externStr(((Function const&)value).prototype()->string);
			return result;
		}
		case Type::Environment:
		{
			Environment* env = (REnvironment(value)).ptr();
			if(env == state.global)
				result = std::string("environment <global>");
			else
				result = std::string("environment <") + intToHexStr((int64_t)env) + ">";
			result = result + "\nVariables:\n";
			Dictionary* d = REnvironment(value).ptr();
			for(Dictionary::const_iterator i = d->begin(); i != d->end(); ++i) {
				result = result + "\t" + state.externStr(i.string())
						+ ":\t" + state.stringify(i.value()) + "\n";
			}
			return result;
		}
		case Type::Object:
		{
			Object const& o = (Object const&)value;
            std::vector<int64_t> emptyNest;
			result = stringify(state, o.base(), emptyNest);
			result = result + "\nAttributes:\n";
			Dictionary* d = o.dictionary();
			for(Dictionary::const_iterator i = d->begin(); i != d->end(); ++i) {
				result = result + "\t" + state.externStr(i.string())
						+ ":\t" + state.stringify(i.value()) + "\n";
			}
			return result;
		}
		case Type::Future:
			return std::string("future") + intToStr(value.i);
		default:
			return Type::toString(value.type);
	};
}