mirror of
https://codeberg.org/comaps/comaps
synced 2026-01-04 03:43:46 +00:00
committed by
Konstantin Pastbin
parent
c9cbb64f12
commit
76ffc99abd
46
libs/search/suggest.cpp
Normal file
46
libs/search/suggest.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "search/suggest.hpp"
|
||||
|
||||
#include "indexer/search_string_utils.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
namespace search
|
||||
{
|
||||
|
||||
std::string GetSuggestion(std::string const & name, QueryString const & query)
|
||||
{
|
||||
auto const nTokens = NormalizeAndTokenizeString(name);
|
||||
|
||||
bool prefixMatched = false;
|
||||
bool fullPrefixMatched = false;
|
||||
|
||||
for (auto const & token : nTokens)
|
||||
{
|
||||
if (StartsWith(token, query.m_prefix))
|
||||
{
|
||||
prefixMatched = true;
|
||||
fullPrefixMatched = token.size() == query.m_prefix.size();
|
||||
}
|
||||
}
|
||||
|
||||
// When |name| does not match prefix or when prefix equals to some
|
||||
// token of the |name| (for example, when user entered "Moscow"
|
||||
// without space at the end), we should not suggest anything.
|
||||
if (!prefixMatched || fullPrefixMatched)
|
||||
return {};
|
||||
|
||||
std::string suggest;
|
||||
for (auto const & token : query.m_tokens)
|
||||
{
|
||||
/// @todo Process street shorts like: st, av, ne, w, ..
|
||||
if (std::find(nTokens.begin(), nTokens.end(), token) == nTokens.end())
|
||||
{
|
||||
suggest += strings::ToUtf8(token);
|
||||
suggest += ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return suggest + name + ' ';
|
||||
}
|
||||
} // namespace search
|
||||
Reference in New Issue
Block a user