1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-12-20 11:53:07 +00:00

Reimplement weightedSearch

This commit is contained in:
ddbb07 2025-11-15 23:59:31 +01:00
parent ebfe91a108
commit 39832a3e71

View file

@ -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>() };
}
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)
{
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);
}
Misc::StringUtils::lowerCaseInPlace(corpus);
Misc::StringUtils::lowerCaseInPlace(patternString);
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());
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 });
}