From 9a510f4e4d545a26f4a6d1ce8dc9fef104405aab Mon Sep 17 00:00:00 2001 From: tatiana-yan Date: Fri, 8 Feb 2019 14:00:38 +0300 Subject: [PATCH] [search] Query token synonyms minor refactoring. --- search/bookmarks/processor.cpp | 2 +- search/geocoder.cpp | 2 +- search/locality_scorer.cpp | 2 +- search/processor.cpp | 2 +- search/query_params.cpp | 2 +- search/query_params.hpp | 22 ++++++++++++++++++---- search/ranking_utils.cpp | 15 ++------------- search/retrieval.cpp | 4 ++-- search/streets_matcher.cpp | 2 +- search/token_slice.hpp | 2 +- 10 files changed, 29 insertions(+), 26 deletions(-) diff --git a/search/bookmarks/processor.cpp b/search/bookmarks/processor.cpp index db4cd57246..4bdbf83d02 100644 --- a/search/bookmarks/processor.cpp +++ b/search/bookmarks/processor.cpp @@ -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 diff --git a/search/geocoder.cpp b/search/geocoder.cpp index fcc4793a25..5eb5ab1d8e 100644 --- a/search/geocoder.cpp +++ b/search/geocoder.cpp @@ -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); } diff --git a/search/locality_scorer.cpp b/search/locality_scorer.cpp index 23ac5eb788..95f787b6fb 100644 --- a/search/locality_scorer.cpp +++ b/search/locality_scorer.cpp @@ -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 diff --git a/search/processor.cpp b/search/processor.cpp index 134c3a2f57..3c919afbe7 100644 --- a/search/processor.cpp +++ b/search/processor.cpp @@ -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(); } diff --git a/search/query_params.cpp b/search/query_params.cpp index 85ea05811d..a286c47548 100644 --- a/search/query_params.cpp +++ b/search/query_params.cpp @@ -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(); } diff --git a/search/query_params.hpp b/search/query_params.hpp index 3d5c2eeeea..75fabfc24c 100644 --- a/search/query_params.hpp +++ b/search/query_params.hpp @@ -26,8 +26,9 @@ public: using TypeIndices = std::vector; using Langs = base::SafeSmallSet; - struct Token + class Token { + public: Token() = default; Token(String const & original) : m_original(original) {} @@ -57,12 +58,27 @@ public: } } + template + std::enable_if_t, 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)); + } + + 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 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 diff --git a/search/ranking_utils.cpp b/search/ranking_utils.cpp index f6f202d45a..6e379ba0b1 100644 --- a/search/ranking_utils.cpp +++ b/search/ranking_utils.cpp @@ -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 const & tokens, diff --git a/search/retrieval.cpp b/search/retrieval.cpp index f1eabff838..12a0d94392 100644 --- a/search/retrieval.cpp +++ b/search/retrieval.cpp @@ -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; } diff --git a/search/streets_matcher.cpp b/search/streets_matcher.cpp index 1caa7167e6..ad2cd6e18f 100644 --- a/search/streets_matcher.cpp +++ b/search/streets_matcher.cpp @@ -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)) diff --git a/search/token_slice.hpp b/search/token_slice.hpp index 37a412ed15..cced7e4b07 100644 --- a/search/token_slice.hpp +++ b/search/token_slice.hpp @@ -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: