mirror of
https://github.com/OpenMW/openmw.git
synced 2025-12-20 17:23:07 +00:00
Reimplement weightedSearch
This commit is contained in:
parent
ebfe91a108
commit
39832a3e71
1 changed files with 19 additions and 31 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
#include <unicode/locid.h>
|
#include <unicode/locid.h>
|
||||||
|
|
||||||
|
|
@ -944,37 +943,26 @@ namespace MWGui
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
std::string escapeRegex(const std::string& str)
|
std::vector<std::string> splitString(const std::string& inputString)
|
||||||
{
|
{
|
||||||
static const std::regex specialChars(R"r([\^\.\[\$\(\)\|\*\+\?\{])r", std::regex_constants::extended);
|
std::istringstream stringStream(inputString);
|
||||||
return std::regex_replace(str, specialChars, R"(\$&)");
|
return { std::istream_iterator<std::string>(stringStream), std::istream_iterator<std::string>() };
|
||||||
}
|
}
|
||||||
|
double weightedSearch(std::string corpus, std::string patternString)
|
||||||
|
{
|
||||||
|
if (patternString.empty() || patternString.find_first_not_of(" ") == std::string::npos)
|
||||||
|
return 1.0;
|
||||||
|
|
||||||
std::regex wordSearch(const std::string& query)
|
Misc::StringUtils::lowerCaseInPlace(corpus);
|
||||||
{
|
Misc::StringUtils::lowerCaseInPlace(patternString);
|
||||||
static const std::regex wordsRegex(R"([^[:space:]]+)", std::regex_constants::extended);
|
|
||||||
auto wordsBegin = std::sregex_iterator(query.begin(), query.end(), wordsRegex);
|
|
||||||
auto wordsEnd = std::sregex_iterator();
|
|
||||||
std::string searchRegex("(");
|
|
||||||
for (auto it = wordsBegin; it != wordsEnd; ++it)
|
|
||||||
{
|
|
||||||
if (it != wordsBegin)
|
|
||||||
searchRegex += '|';
|
|
||||||
searchRegex += escapeRegex(query.substr(it->position(), it->length()));
|
|
||||||
}
|
|
||||||
searchRegex += ')';
|
|
||||||
// query had only whitespace characters
|
|
||||||
if (searchRegex == "()")
|
|
||||||
searchRegex = "^(.*)$";
|
|
||||||
return std::regex(searchRegex, std::regex_constants::extended | std::regex_constants::icase);
|
|
||||||
}
|
|
||||||
|
|
||||||
double weightedSearch(const std::regex& regex, const std::string& text)
|
const std::vector<std::string> patternArray = splitString(patternString);
|
||||||
{
|
|
||||||
std::smatch matches;
|
double numberOfMatches = 0.0;
|
||||||
std::regex_search(text, matches, regex);
|
for (const std::string& word : patternArray)
|
||||||
// need a signed value, so cast to double (not an integer type to guarantee no overflow)
|
numberOfMatches += corpus.find(word) != std::string::npos ? 1.0 : 0.0;
|
||||||
return static_cast<double>(matches.size());
|
|
||||||
|
return numberOfMatches;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -997,14 +985,14 @@ namespace MWGui
|
||||||
constexpr bool operator<(const WeightedPage& rhs) const { return tie() < rhs.tie(); }
|
constexpr bool operator<(const WeightedPage& rhs) const { return tie() < rhs.tie(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
std::regex searchRegex = wordSearch(mScriptFilter->getCaption());
|
std::string searchPattern = mScriptFilter->getCaption();
|
||||||
std::vector<WeightedPage> weightedPages;
|
std::vector<WeightedPage> weightedPages;
|
||||||
weightedPages.reserve(LuaUi::scriptSettingsPageCount());
|
weightedPages.reserve(LuaUi::scriptSettingsPageCount());
|
||||||
for (size_t i = 0; i < LuaUi::scriptSettingsPageCount(); ++i)
|
for (size_t i = 0; i < LuaUi::scriptSettingsPageCount(); ++i)
|
||||||
{
|
{
|
||||||
LuaUi::ScriptSettingsPage page = LuaUi::scriptSettingsPageAt(i);
|
LuaUi::ScriptSettingsPage page = LuaUi::scriptSettingsPageAt(i);
|
||||||
double nameWeight = weightedSearch(searchRegex, page.mName);
|
double nameWeight = weightedSearch(page.mName, searchPattern);
|
||||||
double hintWeight = weightedSearch(searchRegex, page.mSearchHints);
|
double hintWeight = weightedSearch(page.mSearchHints, searchPattern);
|
||||||
if ((nameWeight + hintWeight) > 0)
|
if ((nameWeight + hintWeight) > 0)
|
||||||
weightedPages.push_back({ i, page.mName, -nameWeight, -hintWeight });
|
weightedPages.push_back({ i, page.mName, -nameWeight, -hintWeight });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue