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

locality_scorer_test.cpp « search_tests « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5021e24af731afe2fd3c85508ed6181c27cc6b62 (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
#include "testing/testing.hpp"

#include "search/v2/locality_scorer.hpp"

#include "indexer/search_delimiters.hpp"
#include "indexer/search_string_utils.hpp"

#include "base/assert.hpp"
#include "base/stl_add.hpp"
#include "base/stl_helpers.hpp"
#include "base/string_utils.hpp"

#include "std/algorithm.hpp"
#include "std/set.hpp"
#include "std/unordered_map.hpp"
#include "std/vector.hpp"

using namespace search::v2;
using namespace search;
using namespace strings;

namespace
{
void InitParams(string const & query, bool lastTokenIsPrefix, QueryParams & params)
{
  params.m_tokens.clear();
  params.m_prefixTokens.clear();

  vector<UniString> tokens;

  Delimiters delims;
  SplitUniString(NormalizeAndSimplifyString(query), MakeBackInsertFunctor(tokens), delims);
  for (auto const & token : tokens)
    params.m_tokens.push_back({token});
  if (lastTokenIsPrefix)
  {
    ASSERT(!params.m_tokens.empty(), ());
    params.m_prefixTokens = params.m_tokens.back();
    params.m_tokens.pop_back();
  }
}

void AddLocality(string const & name, uint32_t featureId, QueryParams & params,
                 vector<Geocoder::Locality> & localities)
{
  set<UniString> tokens;

  Delimiters delims;
  SplitUniString(NormalizeAndSimplifyString(name), MakeInsertFunctor(tokens), delims);

  size_t numTokens = params.m_tokens.size();
  if (!params.m_prefixTokens.empty())
    ++numTokens;

  for (size_t startToken = 0; startToken != numTokens; ++startToken)
  {
    for (size_t endToken = startToken + 1; endToken <= numTokens; ++endToken)
    {
      bool matches = true;
      for (size_t i = startToken; i != endToken && matches; ++i)
      {
        UniString const & queryToken = params.GetTokens(i).front();
        bool const isPrefix = (i == params.m_tokens.size());
        if (isPrefix)
        {
          matches = any_of(tokens.begin(), tokens.end(), [&queryToken](UniString const & token)
                           {
                             return StartsWith(token, queryToken);
                           });
        }
        else
        {
          matches = (tokens.count(queryToken) != 0);
        }
      }
      if (matches)
        localities.emplace_back(featureId, startToken, endToken);
    }
  }
}

class LocalityScorerTest : public LocalityScorer::Delegate
{
public:
  LocalityScorerTest() : m_scorer(m_params, static_cast<LocalityScorer::Delegate &>(*this)) {}

  void InitParams(string const & query, bool lastTokenIsPrefix)
  {
    ::InitParams(query, lastTokenIsPrefix, m_params);
  }

  void AddLocality(string const & name, uint32_t featureId)
  {
    ::AddLocality(name, featureId, m_params, m_localities);
    m_names[featureId].push_back(name);
  }

  void GetTopLocalities(size_t limit)
  {
    m_scorer.GetTopLocalities(limit, m_localities);
    sort(m_localities.begin(), m_localities.end(), my::LessBy(&Geocoder::Locality::m_featureId));
  }

  // LocalityScorer::Delegate overrides:
  void GetNames(uint32_t featureId, vector<string> & names) const override
  {
    auto it = m_names.find(featureId);
    if (it != m_names.end())
      names.insert(names.end(), it->second.begin(), it->second.end());
  }

  uint8_t GetRank(uint32_t featureId) const override { return 0; }

protected:
  QueryParams m_params;
  vector<Geocoder::Locality> m_localities;
  unordered_map<uint32_t, vector<string>> m_names;
  LocalityScorer m_scorer;
};
}  // namespace

UNIT_CLASS_TEST(LocalityScorerTest, Smoke)
{
  enum
  {
    ID_NEW_ORLEANS,
    ID_YORK,
    ID_NEW_YORK,
  };

  InitParams("New York Time Square", false /* lastTokenIsPrefix */);

  AddLocality("New Orleans", ID_NEW_ORLEANS);
  AddLocality("York", ID_YORK);
  AddLocality("New York", ID_NEW_YORK);

  GetTopLocalities(100 /* limit */);
  TEST_EQUAL(3, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_NEW_ORLEANS, ());
  TEST_EQUAL(m_localities[1].m_featureId, ID_YORK, ());
  TEST_EQUAL(m_localities[2].m_featureId, ID_NEW_YORK, ());

  // New York is the best matching locality
  GetTopLocalities(1 /* limit */);
  TEST_EQUAL(1, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_NEW_YORK, ());
}

UNIT_CLASS_TEST(LocalityScorerTest, NumbersMatch)
{
  enum
  {
    ID_MARCH,
    ID_APRIL,
    ID_MAY,
    ID_TVER
  };

  InitParams("тверь советская 1", false /* lastTokenIsPrefix */);

  AddLocality("поселок 1 марта", ID_MARCH);
  AddLocality("поселок 1 апреля", ID_APRIL);
  AddLocality("поселок 1 мая", ID_MAY);
  AddLocality("тверь", ID_TVER);

  GetTopLocalities(100 /* limit */);
  TEST_EQUAL(4, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_MARCH, ());
  TEST_EQUAL(m_localities[1].m_featureId, ID_APRIL, ());
  TEST_EQUAL(m_localities[2].m_featureId, ID_MAY, ());
  TEST_EQUAL(m_localities[3].m_featureId, ID_TVER, ());

  // Tver is the best matching locality, as other localities were
  // matched by number.
  GetTopLocalities(1 /* limit */);
  TEST_EQUAL(1, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_TVER, ());
}

UNIT_CLASS_TEST(LocalityScorerTest, NumbersComplexMatch)
{
  enum
  {
    ID_MAY,
    ID_SAINT_PETERSBURG
  };

  InitParams("saint petersburg may 1", false /* lastTokenIsPrefix */);

  AddLocality("may 1", ID_MAY);
  AddLocality("saint petersburg", ID_SAINT_PETERSBURG);

  // "May 1" contains a numeric token, but as it was matched by at
  // least two tokens, there is no penalty for numeric token. And, as
  // it has smaller featureId, it should be left.
  GetTopLocalities(1 /* limit */);
  TEST_EQUAL(1, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_MAY, ());
}

UNIT_CLASS_TEST(LocalityScorerTest, PrefixMatch)
{
  enum
  {
    ID_SAN_ANTONIO,
    ID_NEW_YORK,
    ID_YORK,
    ID_MOSCOW
  };

  // QueryParams params;
  InitParams("New York San Anto", true /* lastTokenIsPrefix */);

  // vector<Geocoder::Locality> localities;
  AddLocality("San Antonio", ID_SAN_ANTONIO);
  AddLocality("New York", ID_NEW_YORK);
  AddLocality("York", ID_YORK);
  AddLocality("Moscow", ID_MOSCOW);

  // All localities except Moscow match to the search query.
  GetTopLocalities(100 /* limit */);
  TEST_EQUAL(3, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_SAN_ANTONIO, ());
  TEST_EQUAL(m_localities[1].m_featureId, ID_NEW_YORK, ());
  TEST_EQUAL(m_localities[2].m_featureId, ID_YORK, ());

  // New York and San Antonio are better than York, because they match
  // by two tokens (second token is prefix for San Antonio), whereas
  // York matches by only one token.
  GetTopLocalities(2 /* limit */);
  TEST_EQUAL(2, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_SAN_ANTONIO, ());
  TEST_EQUAL(m_localities[1].m_featureId, ID_NEW_YORK, ());

  // New York is a better than San Antonio because it matches by two
  // full tokens whereas San Antonio matches by one full token and by
  // one prefix token.
  GetTopLocalities(1 /* limit */);
  TEST_EQUAL(1, m_localities.size(), ());
  TEST_EQUAL(m_localities[0].m_featureId, ID_NEW_YORK, ());
}