mirror of
https://github.com/OpenMW/openmw.git
synced 2025-12-20 07:53:20 +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 <iomanip>
|
||||
#include <regex>
|
||||
|
||||
#include <unicode/locid.h>
|
||||
|
||||
|
|
@ -944,37 +943,26 @@ namespace MWGui
|
|||
|
||||
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);
|
||||
return std::regex_replace(str, specialChars, R"(\$&)");
|
||||
std::istringstream stringStream(inputString);
|
||||
return { std::istream_iterator<std::string>(stringStream), std::istream_iterator<std::string>() };
|
||||
}
|
||||
|
||||
std::regex wordSearch(const std::string& query)
|
||||
double weightedSearch(std::string corpus, std::string 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);
|
||||
}
|
||||
if (patternString.empty() || patternString.find_first_not_of(" ") == std::string::npos)
|
||||
return 1.0;
|
||||
|
||||
double weightedSearch(const std::regex& regex, const std::string& text)
|
||||
{
|
||||
std::smatch matches;
|
||||
std::regex_search(text, matches, regex);
|
||||
// need a signed value, so cast to double (not an integer type to guarantee no overflow)
|
||||
return static_cast<double>(matches.size());
|
||||
Misc::StringUtils::lowerCaseInPlace(corpus);
|
||||
Misc::StringUtils::lowerCaseInPlace(patternString);
|
||||
|
||||
const std::vector<std::string> patternArray = splitString(patternString);
|
||||
|
||||
double numberOfMatches = 0.0;
|
||||
for (const std::string& word : patternArray)
|
||||
numberOfMatches += corpus.find(word) != std::string::npos ? 1.0 : 0.0;
|
||||
|
||||
return numberOfMatches;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -997,14 +985,14 @@ namespace MWGui
|
|||
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;
|
||||
weightedPages.reserve(LuaUi::scriptSettingsPageCount());
|
||||
for (size_t i = 0; i < LuaUi::scriptSettingsPageCount(); ++i)
|
||||
{
|
||||
LuaUi::ScriptSettingsPage page = LuaUi::scriptSettingsPageAt(i);
|
||||
double nameWeight = weightedSearch(searchRegex, page.mName);
|
||||
double hintWeight = weightedSearch(searchRegex, page.mSearchHints);
|
||||
double nameWeight = weightedSearch(page.mName, searchPattern);
|
||||
double hintWeight = weightedSearch(page.mSearchHints, searchPattern);
|
||||
if ((nameWeight + hintWeight) > 0)
|
||||
weightedPages.push_back({ i, page.mName, -nameWeight, -hintWeight });
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue