Repo created
This commit is contained in:
parent
4af19165ec
commit
68073add76
12458 changed files with 12350765 additions and 2 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue