Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_offset_match.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9ca26296f31d4bcc5ade80720a73f940d108796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#pragma once

#include "search/common.hpp"
#include "search/query_params.hpp"
#include "search/search_index_values.hpp"
#include "search/search_trie.hpp"
#include "search/token_slice.hpp"

#include "indexer/trie.hpp"

#include "base/assert.hpp"
#include "base/dfa_helpers.hpp"
#include "base/levenshtein_dfa.hpp"
#include "base/stl_helpers.hpp"
#include "base/string_utils.hpp"
#include "base/uni_string_dfa.hpp"

#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <queue>
#include <unordered_set>
#include <utility>
#include <vector>

namespace search
{
namespace impl
{
namespace
{
template <typename ValueList>
bool FindLangIndex(trie::Iterator<ValueList> const & trieRoot, uint8_t lang, uint32_t & langIx)
{
  ASSERT_LESS(trieRoot.m_edges.size(), std::numeric_limits<uint32_t>::max(), ());

  uint32_t const numLangs = static_cast<uint32_t>(trieRoot.m_edges.size());
  for (uint32_t i = 0; i < numLangs; ++i)
  {
    auto const & edge = trieRoot.m_edges[i].m_label;
    ASSERT_GREATER_OR_EQUAL(edge.size(), 1, ());
    if (edge[0] == lang)
    {
      langIx = i;
      return true;
    }
  }
  return false;
}
}  // namespace

template <typename ValueList, typename DFA, typename ToDo>
bool MatchInTrie(trie::Iterator<ValueList> const & trieRoot, strings::UniChar const * rootPrefix,
                 size_t rootPrefixSize, DFA const & dfa, ToDo && toDo)
{
  using TrieDFAIt = std::shared_ptr<trie::Iterator<ValueList>>;
  using DFAIt = typename DFA::Iterator;
  using State = std::pair<TrieDFAIt, DFAIt>;

  std::queue<State> q;

  {
    auto it = dfa.Begin();
    DFAMove(it, rootPrefix, rootPrefix + rootPrefixSize);
    if (it.Rejects())
      return false;
    q.emplace(trieRoot.Clone(), it);
  }

  bool found = false;

  while (!q.empty())
  {
    auto const p = q.front();
    q.pop();

    auto const & trieIt = p.first;
    auto const & dfaIt = p.second;

    if (dfaIt.Accepts())
    {
      trieIt->m_values.ForEach(
          [&dfaIt, &toDo](auto const & v) { toDo(v, dfaIt.ErrorsMade() == 0); });
      found = true;
    }

    size_t const numEdges = trieIt->m_edges.size();
    for (size_t i = 0; i < numEdges; ++i)
    {
      auto const & edge = trieIt->m_edges[i];

      auto curIt = dfaIt;
      strings::DFAMove(curIt, edge.m_label.begin(), edge.m_label.end());
      if (!curIt.Rejects())
        q.emplace(trieIt->GoToEdge(i), curIt);
    }
  }

  return found;
}

template <typename Filter, typename Value>
class OffsetIntersector
{
  using Values = std::unordered_map<Value, bool>;

  Filter const & m_filter;
  std::unique_ptr<Values> m_prevValues;
  std::unique_ptr<Values> m_values;

public:
  explicit OffsetIntersector(Filter const & filter)
    : m_filter(filter), m_values(std::make_unique<Values>())
  {
  }

  void operator()(Value const & v, bool exactMatch)
  {
    if (m_prevValues && !m_prevValues->count(v))
      return;

    if (m_filter(v))
    {
      auto res = m_values->emplace(v, exactMatch);
      if (!res.second)
        res.first->second = res.first->second || exactMatch;
    }
  }

  void NextStep()
  {
    if (!m_prevValues)
      m_prevValues = std::make_unique<Values>();

    m_prevValues.swap(m_values);
    m_values->clear();
  }

  template <class ToDo>
  void ForEachResult(ToDo && toDo) const
  {
    if (!m_prevValues)
      return;
    for (auto const & value : *m_prevValues)
      toDo(value.first, value.second);
  }
};
}  // namespace impl

template <typename ValueList>
struct TrieRootPrefix
{
  using Value = typename ValueList::Value;
  using Iterator = trie::Iterator<ValueList>;

  Iterator const & m_root;
  strings::UniChar const * m_prefix;
  size_t m_prefixSize;

  TrieRootPrefix(Iterator const & root, typename Iterator::Edge::EdgeLabel const & edge)
    : m_root(root)
  {
    if (edge.size() == 1)
    {
      m_prefix = 0;
      m_prefixSize = 0;
    }
    else
    {
      m_prefix = &edge[1];
      m_prefixSize = edge.size() - 1;
    }
  }
};

template <typename Filter, typename Value>
class TrieValuesHolder
{
public:
  TrieValuesHolder(Filter const & filter) : m_filter(filter) {}

  void operator()(Value const & v, bool exactMatch)
  {
    if (m_filter(v))
      m_values.emplace_back(v, exactMatch);
  }

  template <class ToDo>
  void ForEachValue(ToDo && toDo) const
  {
    for (auto const & value : m_values)
      toDo(value.first, value.second);
  }

private:
  std::vector<std::pair<Value, bool>> m_values;
  Filter const & m_filter;
};

template <typename DFA>
struct SearchTrieRequest
{
  template <typename Langs>
  void SetLangs(Langs const & langs)
  {
    m_langs.clear();
    for (auto const lang : langs)
    {
      if (lang >= 0 && lang <= std::numeric_limits<int8_t>::max())
        m_langs.insert(static_cast<int8_t>(lang));
    }
  }

  bool HasLang(int8_t lang) const { return m_langs.find(lang) != m_langs.cend(); }

  void Clear()
  {
    m_names.clear();
    m_categories.clear();
    m_langs.clear();
  }

  std::vector<DFA> m_names;
  std::vector<strings::UniStringDFA> m_categories;

  // Set of languages, will be prepended to all DFAs in |m_names|
  // during retrieval from a search index.  Semantics of this field
  // depends on the search index, for example this can be a set of
  // langs from StringUtf8Multilang, or a set of locale indices.
  std::unordered_set<int8_t> m_langs;
};

// Calls |toDo| for each feature accepted by at least one DFA.
//
// *NOTE* |toDo| may be called several times for the same feature.
template <typename DFA, typename ValueList, typename ToDo>
void MatchInTrie(std::vector<DFA> const & dfas, TrieRootPrefix<ValueList> const & trieRoot,
                 ToDo && toDo)
{
  for (auto const & dfa : dfas)
    impl::MatchInTrie(trieRoot.m_root, trieRoot.m_prefix, trieRoot.m_prefixSize, dfa, toDo);
}

// Calls |toDo| for each feature in categories branch matching to |request|.
//
// *NOTE* |toDo| may be called several times for the same feature.
template <typename DFA, typename ValueList, typename ToDo>
bool MatchCategoriesInTrie(SearchTrieRequest<DFA> const & request,
                           trie::Iterator<ValueList> const & trieRoot, ToDo && toDo)
{
  uint32_t langIx = 0;
  if (!impl::FindLangIndex(trieRoot, search::kCategoriesLang, langIx))
    return false;

  auto const & edge = trieRoot.m_edges[langIx].m_label;
  ASSERT_GREATER_OR_EQUAL(edge.size(), 1, ());

  auto const catRoot = trieRoot.GoToEdge(langIx);
  MatchInTrie(request.m_categories, TrieRootPrefix<ValueList>(*catRoot, edge), toDo);

  return true;
}

// Calls |toDo| with trie root prefix and language code on each
// language allowed by |request|.
template <typename DFA, typename ValueList, typename ToDo>
void ForEachLangPrefix(SearchTrieRequest<DFA> const & request,
                       trie::Iterator<ValueList> const & trieRoot, ToDo && toDo)
{
  ASSERT_LESS(trieRoot.m_edges.size(), std::numeric_limits<uint32_t>::max(), ());

  uint32_t const numLangs = static_cast<uint32_t>(trieRoot.m_edges.size());
  for (uint32_t langIx = 0; langIx < numLangs; ++langIx)
  {
    auto const & edge = trieRoot.m_edges[langIx].m_label;
    ASSERT_GREATER_OR_EQUAL(edge.size(), 1, ());
    int8_t const lang = static_cast<int8_t>(edge[0]);
    if (edge[0] < search::kCategoriesLang && request.HasLang(lang))
    {
      auto const langRoot = trieRoot.GoToEdge(langIx);
      TrieRootPrefix<ValueList> langPrefix(*langRoot, edge);
      toDo(langPrefix, lang);
    }
  }
}

// Calls |toDo| for each feature whose description matches to
// |request|.  Each feature will be passed to |toDo| only once.
template <typename DFA, typename ValueList, typename Filter, typename ToDo>
void MatchFeaturesInTrie(SearchTrieRequest<DFA> const & request,
                         trie::Iterator<ValueList> const & trieRoot, Filter const & filter,
                         ToDo && toDo)
{
  using Value = typename ValueList::Value;

  TrieValuesHolder<Filter, Value> categoriesHolder(filter);
  bool const categoriesMatched = MatchCategoriesInTrie(request, trieRoot, categoriesHolder);

  impl::OffsetIntersector<Filter, Value> intersector(filter);

  ForEachLangPrefix(
      request, trieRoot,
      [&request, &intersector](TrieRootPrefix<ValueList> & langRoot, int8_t /* lang */) {
        MatchInTrie(request.m_names, langRoot, intersector);
      });

  if (categoriesMatched)
    categoriesHolder.ForEachValue(intersector);

  intersector.NextStep();
  intersector.ForEachResult(std::forward<ToDo>(toDo));
}

template <typename ValueList, typename Filter, typename ToDo>
void MatchPostcodesInTrie(TokenSlice const & slice, trie::Iterator<ValueList> const & trieRoot,
                          Filter const & filter, ToDo && toDo)
{
  using namespace strings;
  using Value = typename ValueList::Value;

  uint32_t langIx = 0;
  if (!impl::FindLangIndex(trieRoot, search::kPostcodesLang, langIx))
    return;

  auto const & edge = trieRoot.m_edges[langIx].m_label;
  auto const postcodesRoot = trieRoot.GoToEdge(langIx);

  impl::OffsetIntersector<Filter, Value> intersector(filter);
  for (size_t i = 0; i < slice.Size(); ++i)
  {
    // Full match required even for prefix token. Reasons:
    // 1. For postcode every symbol is important, partial matching can lead to wrong results.
    // 2. For prefix match query like "streetname 40" where |streetname| is located in 40xxx
    // postcode zone will give all street vicinity as the result which is wrong.
    std::vector<UniStringDFA> dfas;
    slice.Get(i).ForOriginalAndSynonyms([&dfas](UniString const & s) { dfas.emplace_back(s); });
    MatchInTrie(dfas, TrieRootPrefix<ValueList>(*postcodesRoot, edge), intersector);

    intersector.NextStep();
  }

  intersector.ForEachResult(std::forward<ToDo>(toDo));
}
}  // namespace search