[search] Query token synonyms minor refactoring.

This commit is contained in:
tatiana-yan 2019-02-08 14:00:38 +03:00 committed by mpimenov
parent 184d8463c9
commit 9a510f4e4d
10 changed files with 29 additions and 26 deletions

View file

@ -152,7 +152,7 @@ QueryVec Processor::GetQueryVec(IdfMap & idfs, QueryParams const & params) const
QueryVec::Builder builder;
for (size_t i = 0; i < params.GetNumTokens(); ++i)
{
auto const & token = params.GetToken(i).m_original;
auto const & token = params.GetToken(i).GetOriginal();
if (params.IsPrefixToken(i))
builder.SetPrefix(token);
else

View file

@ -201,7 +201,7 @@ void JoinQueryTokens(QueryParams const & params, TokenRange const & range, UniSt
ASSERT(range.IsValid(), (range));
for (size_t i : range)
{
res.append(params.GetToken(i).m_original);
res.append(params.GetToken(i).GetOriginal());
if (i + 1 != range.End())
res.append(sep);
}

View file

@ -134,7 +134,7 @@ void LocalityScorer::GetTopLocalities(MwmSet::MwmId const & countryId, BaseConte
++endToken)
{
auto const curToken = endToken - 1;
auto const & token = m_params.GetToken(curToken).m_original;
auto const & token = m_params.GetToken(curToken).GetOriginal();
if (m_params.IsPrefixToken(curToken))
builder.SetPrefix(token);
else

View file

@ -522,7 +522,7 @@ void Processor::InitParams(QueryParams & params) const
for (size_t i = 0; i < params.GetNumTokens(); ++i)
{
auto & token = params.GetToken(i);
if (IsStreetSynonym(token.m_original))
if (IsStreetSynonym(token.GetOriginal()))
params.GetTypeIndices(i).clear();
}

View file

@ -66,7 +66,7 @@ void QueryParams::Token::AddSynonym(String const & s)
string DebugPrint(QueryParams::Token const & token)
{
ostringstream os;
os << "Token [ m_original=" << DebugPrint(token.m_original)
os << "Token [ m_original=" << DebugPrint(token.GetOriginal())
<< ", m_synonyms=" << DebugPrint(token.m_synonyms) << " ]";
return os.str();
}

View file

@ -26,8 +26,9 @@ public:
using TypeIndices = std::vector<uint32_t>;
using Langs = base::SafeSmallSet<StringUtf8Multilang::kMaxSupportedLanguages>;
struct Token
class Token
{
public:
Token() = default;
Token(String const & original) : m_original(original) {}
@ -57,12 +58,27 @@ public:
}
}
template <typename Fn>
std::enable_if_t<std::is_same<std::result_of_t<Fn(String)>, bool>::value, bool> AnyOf(
Fn && fn) const
{
if (fn(m_original))
return true;
return std::any_of(m_synonyms.begin(), m_synonyms.end(), std::forward<Fn>(fn));
}
String const & GetOriginal() const { return m_original; }
void Clear()
{
m_original.clear();
m_synonyms.clear();
}
private:
friend std::string DebugPrint(QueryParams::Token const & token);
String m_original;
std::vector<String> m_synonyms;
};
@ -84,7 +100,7 @@ public:
Clear();
for (; tokenBegin != tokenEnd; ++tokenBegin)
m_tokens.emplace_back(*tokenBegin);
m_prefixToken.m_original = prefix;
m_prefixToken = Token(prefix);
m_hasPrefix = true;
m_typeIndices.resize(GetNumTokens());
}
@ -131,6 +147,4 @@ private:
Langs m_langs;
int m_scale = scales::GetUpperScale();
};
std::string DebugPrint(QueryParams::Token const & token);
} // namespace search

View file

@ -70,23 +70,12 @@ namespace impl
{
bool FullMatch(QueryParams::Token const & token, UniString const & text)
{
if (token.m_original == text)
return true;
auto const & synonyms = token.m_synonyms;
return find(synonyms.begin(), synonyms.end(), text) != synonyms.end();
return token.AnyOf([&text](UniString const & s) { return s == text; });
}
bool PrefixMatch(QueryParams::Token const & token, UniString const & text)
{
if (StartsWith(text, token.m_original))
return true;
for (auto const & synonym : token.m_synonyms)
{
if (StartsWith(text, synonym))
return true;
}
return false;
return token.AnyOf([&text](UniString const & s) { return StartsWith(text, s); });
}
ErrorsMade GetMinErrorsMade(vector<strings::UniString> const & tokens,

View file

@ -185,10 +185,10 @@ bool MatchFeatureByPostcode(FeatureType & ft, TokenSlice const & slice)
{
if (slice.IsPrefix(i))
{
if (!StartsWith(tokens[i], slice.Get(i).m_original))
if (!StartsWith(tokens[i], slice.Get(i).GetOriginal()))
return false;
}
else if (tokens[i] != slice.Get(i).m_original)
else if (tokens[i] != slice.Get(i).GetOriginal())
{
return false;
}

View file

@ -153,7 +153,7 @@ void StreetsMatcher::FindStreets(BaseContext const & ctx, FeaturesFilter const &
for (; curToken < ctx.m_numTokens && !ctx.IsTokenUsed(curToken) && !streets.IsEmpty();
++curToken)
{
auto const & token = params.GetToken(curToken).m_original;
auto const & token = params.GetToken(curToken).GetOriginal();
bool const isPrefix = params.IsPrefixToken(curToken);
if (house_numbers::LooksLikeHouseNumber(token, isPrefix))

View file

@ -65,7 +65,7 @@ public:
QuerySlice(TokenSlice const & slice) : m_slice(slice) {}
// QuerySlice overrides:
QueryParams::String const & Get(size_t i) const override { return m_slice.Get(i).m_original; }
QueryParams::String const & Get(size_t i) const override { return m_slice.Get(i).GetOriginal(); }
size_t Size() const override { return m_slice.Size(); }
private: