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

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

#include "search/utils.hpp"

#include "ugc/types.hpp"

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

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

#include <array>
#include <iomanip>
#include <limits>
#include <sstream>

using namespace std;

namespace search
{
namespace
{
// See search/search_quality/scoring_model.py for details.  In short,
// these coeffs correspond to coeffs in a linear model.
double constexpr kHasName = 0.5;
double constexpr kCategoriesPopularity = 0.05;
double constexpr kCategoriesDistanceToPivot = -0.6874177;
double constexpr kCategoriesRank = 1.0000000;
double constexpr kCategoriesRating = 0.0500000;
double constexpr kCategoriesFalseCats = -1.0000000;
double constexpr kCategoriesRefusedByFilter = -1.0000000;

double constexpr kDistanceToPivot = -0.2123693;
double constexpr kRank = 0.1065355;
double constexpr kPopularity = 1.0000000;
double constexpr kRating = 0.0716319;
double constexpr kFalseCats = -0.4172461;
double constexpr kErrorsMade = -0.0391331;
double constexpr kMatchedFraction = 0.1876736;
double constexpr kAllTokensUsed = 0.0478513;
double constexpr kExactCountryOrCapital = 0.1247733;
double constexpr kRefusedByFilter = -1.0000000;
double constexpr kNameScore[NameScore::NAME_SCORE_COUNT] = {
  0.0085962 /* Zero */,
  -0.0099698 /* Substring */,
  -0.0158311 /* Prefix */,
  0.0172047 /* Full Match */
};
double constexpr kType[Model::TYPE_COUNT] = {
  -0.0467816 /* SUBPOI */,
  -0.0467816 /* COMPLEX_POI */,
  -0.0467816 /* Building */,
  -0.0444630 /* Street */,
  -0.0348396 /* Unclassified */,
  -0.0725383 /* Village */,
  0.0073583 /* City */,
  0.0233254 /* State */,
  0.1679389 /* Country */
};
double constexpr kResultType[base::Underlying(ResultType::Count)] = {
  0.0338794 /* TransportMajor */,
  0.0216298 /* TransportLocal */,
  0.0064977 /* Eat */,
  -0.0275763 /* Hotel */,
  0.0358858 /* Attraction */,
  -0.0195234 /* Service */,
  -0.0128952 /* General */
};

// Coeffs sanity checks.
static_assert(kHasName >= 0, "");
static_assert(kCategoriesPopularity >= 0, "");
static_assert(kDistanceToPivot <= 0, "");
static_assert(kRank >= 0, "");
static_assert(kPopularity >= 0, "");
static_assert(kErrorsMade <= 0, "");
static_assert(kExactCountryOrCapital >= 0, "");
static_assert(kCategoriesRefusedByFilter < 0, "");
static_assert(kRefusedByFilter < 0, "");

double TransformDistance(double distance)
{
  return min(distance, RankingInfo::kMaxDistMeters) / RankingInfo::kMaxDistMeters;
}

double TransformRating(pair<uint8_t, float> const & rating)
{
  double r = 0.0;
  // From statistics.
  double constexpr kAverageRating = 7.6;
  if (rating.first != 0)
  {
    r = (static_cast<double>(rating.second) - kAverageRating) /
        (ugc::UGC::kMaxRating - ugc::UGC::kRatingDetalizationThreshold);
    r *= static_cast<double>(rating.first) / 3.0 /* maximal confidence */;
  }
  return r;
}

void PrintParse(ostringstream & oss, array<TokenRange, Model::TYPE_COUNT> const & ranges,
                size_t numTokens)
{
  vector<Model::Type> types(numTokens, Model::Type::TYPE_COUNT);
  for (size_t i = 0; i < ranges.size(); ++i)
  {
    for (size_t pos : ranges[i])
    {
      CHECK_LESS(pos, numTokens, ());
      CHECK_EQUAL(types[pos], Model::Type::TYPE_COUNT, ());
      types[pos] = static_cast<Model::Type>(i);
    }
  }

  oss << "Parse [";
  for (size_t i = 0; i < numTokens; ++i)
  {
    if (i > 0)
      oss << " ";
    oss << DebugPrint(types[i]);
  }
  oss << "]";
}

class IsServiceTypeChecker
{
private:
  IsServiceTypeChecker()
  {
    array<string, 3> const oneLevelTypes = {"barrier", "power", "traffic_calming"};

    for (auto const & t : oneLevelTypes)
      m_oneLevelTypes.push_back(classif().GetTypeByPath({t}));
  }

public:
  static IsServiceTypeChecker const & Instance()
  {
    static const IsServiceTypeChecker instance;
    return instance;
  }

  bool operator()(feature::TypesHolder const & th) const
  {
    return base::AnyOf(th, [&](auto t) {
      ftype::TruncValue(t, 1);
      return find(m_oneLevelTypes.begin(), m_oneLevelTypes.end(), t) != m_oneLevelTypes.end();
    });
  }

private:
  vector<uint32_t> m_oneLevelTypes;
};
}  // namespace

// static
double const RankingInfo::kMaxDistMeters = 2e6;

// static
void RankingInfo::PrintCSVHeader(ostream & os)
{
  os << "DistanceToPivot"
     << ",Rank"
     << ",Popularity"
     << ",Rating"
     << ",NameScore"
     << ",ErrorsMade"
     << ",MatchedFraction"
     << ",SearchType"
     << ",ResultType"
     << ",PureCats"
     << ",FalseCats"
     << ",AllTokensUsed"
     << ",ExactCountryOrCapital"
     << ",IsCategorialRequest"
     << ",HasName";
}

string DebugPrint(RankingInfo const & info)
{
  ostringstream os;
  os << boolalpha;
  PrintParse(os, info.m_tokenRanges, info.m_numTokens);
  os << ", RankingInfo [";
  os << "m_distanceToPivot:" << info.m_distanceToPivot;
  os << ", m_rank:" << static_cast<int>(info.m_rank);
  os << ", m_popularity:" << static_cast<int>(info.m_popularity);
  os << ", m_rating:[" << static_cast<int>(info.m_rating.first) << ", " << info.m_rating.second
     << "]";
  os << ", m_nameScore:" << DebugPrint(info.m_nameScore);
  os << ", m_errorsMade:" << DebugPrint(info.m_errorsMade);
  os << ", m_isAltOrOldName: " << info.m_isAltOrOldName;
  os << ", m_numTokens:" << info.m_numTokens;
  os << ", m_matchedFraction:" << info.m_matchedFraction;
  os << ", m_type:" << DebugPrint(info.m_type);
  os << ", m_resultType:" << DebugPrint(info.m_resultType);
  os << ", m_pureCats:" << info.m_pureCats;
  os << ", m_falseCats:" << info.m_falseCats;
  os << ", m_allTokensUsed:" << info.m_allTokensUsed;
  os << ", m_exactCountryOrCapital:" << info.m_exactCountryOrCapital;
  os << ", m_categorialRequest:" << info.m_categorialRequest;
  os << ", m_hasName:" << info.m_hasName;
  os << ", m_refusedByFilter:" << info.m_refusedByFilter;
  os << "]";
  return os.str();
}

void RankingInfo::ToCSV(ostream & os) const
{
  os << fixed;
  os << m_distanceToPivot << ",";
  os << static_cast<int>(m_rank) << ",";
  os << static_cast<int>(m_popularity) << ",";
  os << TransformRating(m_rating) << ",";
  os << DebugPrint(m_nameScore) << ",";
  os << GetErrorsMadePerToken() << ",";
  os << m_matchedFraction << ",";
  os << DebugPrint(m_type) << ",";
  os << DebugPrint(m_resultType) << ",";
  os << m_pureCats << ",";
  os << m_falseCats << ",";
  os << (m_allTokensUsed ? 1 : 0) << ",";
  os << (m_exactCountryOrCapital ? 1 : 0) << ",";
  os << (m_categorialRequest ? 1 : 0) << ",";
  os << (m_hasName ? 1 : 0);
}

double RankingInfo::GetLinearModelRank() const
{
  // NOTE: this code must be consistent with scoring_model.py.  Keep
  // this in mind when you're going to change scoring_model.py or this
  // code. We're working on automatic rank calculation code generator
  // integrated in the build system.
  double const distanceToPivot = TransformDistance(m_distanceToPivot);
  double const rank = static_cast<double>(m_rank) / numeric_limits<uint8_t>::max();
  double const popularity = static_cast<double>(m_popularity) / numeric_limits<uint8_t>::max();
  double const rating = TransformRating(m_rating);

  auto nameScore = m_nameScore;
  if (m_pureCats || m_falseCats)
  {
    // If the feature was matched only by categorial tokens, it's
    // better for ranking to set name score to zero.  For example,
    // when we're looking for a "cafe", cafes "Cafe Pushkin" and
    // "Lermontov" both match to the request, but must be ranked in
    // accordance to their distances to the user position or viewport,
    // in spite of "Cafe Pushkin" has a non-zero name rank.
    nameScore = NAME_SCORE_ZERO;
  }

  double result = 0.0;
  if (!m_categorialRequest)
  {
    result += kDistanceToPivot * distanceToPivot;
    result += kRank * rank;
    result += kPopularity * popularity;
    result += kRating * rating;
    result += m_falseCats * kFalseCats;
    result += kType[m_type];
    if (Model::IsPoi(m_type))
    {
      CHECK_NOT_EQUAL(m_resultType, ResultType::Count, ());
      result += kResultType[base::Underlying(m_resultType)];
    }
    result += (m_allTokensUsed ? 1 : 0) * kAllTokensUsed;
    result += (m_exactCountryOrCapital ? 1 : 0) * kExactCountryOrCapital;
    auto const nameRank = kNameScore[nameScore] + kErrorsMade * GetErrorsMadePerToken() +
                          kMatchedFraction * m_matchedFraction;
    result += (m_isAltOrOldName ? 0.7 : 1.0) * nameRank;
    result += (m_refusedByFilter ? 1 : 0) * kRefusedByFilter;
  }
  else
  {
    result += kCategoriesDistanceToPivot * distanceToPivot;
    result += kCategoriesRank * rank;
    result += kCategoriesPopularity * popularity;
    result += kCategoriesRating * rating;
    result += kCategoriesFalseCats * kFalseCats;
    result += m_hasName * kHasName;
    result += (m_refusedByFilter ? 1 : 0) * kCategoriesRefusedByFilter;
  }
  return result;
}

// We build LevensteinDFA based on feature tokens to match query.
// Feature tokens can be longer than query tokens that's why every query token can be
// matched to feature token with maximal supported errors number.
// As maximal errors number depends only on tokens number (not tokens length),
// errorsMade per token is supposed to be a good metric.
double RankingInfo::GetErrorsMadePerToken() const
{
  size_t static const kMaxErrorsPerToken =
      GetMaxErrorsForTokenLength(numeric_limits<size_t>::max());
  if (!m_errorsMade.IsValid())
    return static_cast<double>(kMaxErrorsPerToken);

  CHECK_GREATER(m_numTokens, 0, ());
  return static_cast<double>(m_errorsMade.m_errorsMade) / static_cast<double>(m_numTokens);
}

ResultType GetResultType(feature::TypesHolder const & th)
{
  if (ftypes::IsEatChecker::Instance()(th))
    return ResultType::Eat;
  if (ftypes::IsHotelChecker::Instance()(th))
    return ResultType::Hotel;
  if (ftypes::IsRailwayStationChecker::Instance()(th) ||
      ftypes::IsSubwayStationChecker::Instance()(th) || ftypes::IsAirportChecker::Instance()(th))
  {
    return ResultType::TransportMajor;
  }
  if (ftypes::IsPublicTransportStopChecker::Instance()(th))
    return ResultType::TransportLocal;

  // We have several lists for attractions: short list in search categories for @tourism and long
  // list in ftypes::AttractionsChecker. We have highway-pedestrian, place-square, historic-tomb,
  // landuse-cemetery, amenity-townhall etc in long list and logic of long list is "if this object
  // has high popularity and/or wiki description probably it is attraction". It's better to use
  // short list here.
  auto static const attractionTypes =
      search::GetCategoryTypes("sights", "en", GetDefaultCategories());
  if (base::AnyOf(attractionTypes, [&th](auto t) { return th.Has(t); }))
    return ResultType::Attraction;

  if (IsServiceTypeChecker::Instance()(th))
    return ResultType::Service;

  return ResultType::General;
}

string DebugPrint(ResultType type)
{
  switch (type)
  {
  case ResultType::TransportMajor: return "TransportMajor";
  case ResultType::TransportLocal: return "TransportLocal";
  case ResultType::Eat: return "Eat";
  case ResultType::Hotel: return "Hotel";
  case ResultType::Attraction: return "Attraction";
  case ResultType::Service: return "Service";
  case ResultType::General: return "General";
  case ResultType::Count: return "Count";
  }
  UNREACHABLE();
}
}  // namespace search