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: a80cd70cb2db417b724926681ffad42e9980c626 (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
#include "search/ranking_info.hpp"

#include "ugc/types.hpp"

#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 kDistanceToPivot = -1.0000000;
double constexpr kRank = 1.0000000;
// todo: (@t.yan) Adjust.
double constexpr kPopularity = 0.0500000;
// todo: (@t.yan) Adjust.
double constexpr kRating = 0.0500000;
double constexpr kFalseCats = -0.3691859;
double constexpr kErrorsMade = -0.0579812;
double constexpr kAllTokensUsed = 0.0000000;
double constexpr kHasName = 0.5;

double constexpr kNameScore[NameScore::NAME_SCORE_COUNT] = {
  -0.7245815 /* Zero */,
  0.1853727 /* Substring */,
  0.2046046 /* Prefix */,
  0.3346041 /* Full Match */
};
double constexpr kType[Model::TYPE_COUNT] = {
  -0.4458349 /* POI */,
  -0.4458349 /* Building */,
  -0.3001181 /* Street */,
  -0.3299295 /* Unclassified */,
  -0.3530548 /* Village */,
  0.4506418 /* City */,
  0.2889073 /* State */,
  0.6893882 /* Country */
};

// Coeffs sanity checks.
static_assert(kDistanceToPivot <= 0, "");
static_assert(kRank >= 0, "");
static_assert(kPopularity >= 0, "");
static_assert(kErrorsMade <= 0, "");
static_assert(kHasName >= 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;
}
}  // namespace

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

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

string DebugPrint(RankingInfo const & info)
{
  ostringstream os;
  os << boolalpha;
  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_type:" << DebugPrint(info.m_type);
  os << ", m_pureCats:" << info.m_pureCats;
  os << ", m_falseCats:" << info.m_falseCats;
  os << ", m_allTokensUsed:" << info.m_allTokensUsed;
  os << ", m_categorialRequest:" << info.m_categorialRequest;
  os << ", m_hasName:" << info.m_hasName;
  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 << GetErrorsMade() << ",";
  os << DebugPrint(m_type) << ",";
  os << m_pureCats << ",";
  os << m_falseCats << ",";
  os << (m_allTokensUsed ? 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;
  result += kDistanceToPivot * distanceToPivot;
  result += kRank * rank;
  result += kPopularity * popularity;
  result += kRating * rating;
  result += m_falseCats * kFalseCats;
  if (!m_categorialRequest)
  {
    result += kType[m_type];
    result += kNameScore[nameScore];
    result += kErrorsMade * GetErrorsMade();
    result += (m_allTokensUsed ? 1 : 0) * kAllTokensUsed;
  }
  else
  {
    result += m_hasName * kHasName;
  }
  return result;
}

size_t RankingInfo::GetErrorsMade() const
{
  return m_errorsMade.IsValid() ? m_errorsMade.m_errorsMade : 0;
}
}  // namespace search