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

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

#include "generator/feature_builder.hpp"
#include "generator/sponsored_scoring.hpp"

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

#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include <iomanip>

#include "boost/algorithm/string/replace.hpp"

using namespace feature;

namespace generator
{
// BookingHotel ------------------------------------------------------------------------------------
BookingHotel::BookingHotel(std::string const & src)
{
  std::vector<std::string> rec;
  strings::ParseCSVRow(src, '\t', rec);
  CHECK_EQUAL(rec.size(), FieldsCount(), ("Error parsing hotels.tsv line:",
                                          boost::replace_all_copy(src, "\t", "\\t")));

  CLOG(LDEBUG, strings::to_uint(rec[FieldIndex(Fields::Id)], m_id.Get()), ());
  // TODO(mgsergio): Use ms::LatLon.
  CLOG(LDEBUG, strings::to_double(rec[FieldIndex(Fields::Latitude)], m_latLon.m_lat), ());
  CLOG(LDEBUG, strings::to_double(rec[FieldIndex(Fields::Longtitude)], m_latLon.m_lon), ());

  m_name = rec[FieldIndex(Fields::Name)];
  m_address = rec[FieldIndex(Fields::Address)];

  CLOG(LDEBUG, strings::to_uint(rec[FieldIndex(Fields::Stars)], m_stars), ());
  CLOG(LDEBUG, strings::to_uint(rec[FieldIndex(Fields::PriceCategory)], m_priceCategory), ());
  CLOG(LDEBUG, strings::to_double(rec[FieldIndex(Fields::RatingBooking)], m_ratingBooking), ());
  CLOG(LDEBUG, strings::to_double(rec[FieldIndex(Fields::RatingUsers)], m_ratingUser), ());

  m_descUrl = rec[FieldIndex(Fields::DescUrl)];

  CLOG(LDEBUG, strings::to_uint(rec[FieldIndex(Fields::Type)], m_type), ());

  m_translations = rec[FieldIndex(Fields::Translations)];
}


// BookingDataset ----------------------------------------------------------------------------------
template <>
bool BookingDataset::NecessaryMatchingConditionHolds(FeatureBuilder const & fb) const
{
  if (fb.GetName(StringUtf8Multilang::kDefaultCode).empty())
    return false;

  return ftypes::IsHotelChecker::Instance()(fb.GetTypes());
}

template <>
void BookingDataset::PreprocessMatchedOsmObject(ObjectId, FeatureBuilder & fb,
                                                std::function<void(FeatureBuilder &)> const fn) const
{
  // Turn a hotel into a simple building.
  if (fb.GetGeomType() == GeomType::Area)
  {
    // Remove all information about the hotel.
    auto & meta = fb.GetMetadata();
    meta.Drop(Metadata::EType::FMD_STARS);
    meta.Drop(Metadata::EType::FMD_WEBSITE);
    meta.Drop(Metadata::EType::FMD_PHONE_NUMBER);

    auto & params = fb.GetParams();
    params.ClearName();

    auto const tourism = classif().GetTypeByPath({"tourism"});
    base::EraseIf(params.m_types, [tourism](uint32_t type)
    {
      ftype::TruncValue(type, 1);
      return type == tourism;
    });
  }

  fn(fb);
}

template <>
void BookingDataset::BuildObject(Object const & hotel,
                                 std::function<void(FeatureBuilder &)> const & fn) const
{
  FeatureBuilder fb;

  fb.SetCenter(mercator::FromLatLon(hotel.m_latLon.m_lat, hotel.m_latLon.m_lon));

  auto & metadata = fb.GetMetadata();
  metadata.Set(Metadata::FMD_SPONSORED_ID, strings::to_string(hotel.m_id.Get()));
  metadata.Set(Metadata::FMD_WEBSITE, hotel.m_descUrl);
  metadata.Set(Metadata::FMD_RATING, strings::to_string(hotel.m_ratingUser));
  metadata.Set(Metadata::FMD_STARS, strings::to_string(hotel.m_stars));
  metadata.Set(Metadata::FMD_PRICE_RATE, strings::to_string(hotel.m_priceCategory));

  auto & params = fb.GetParams();
  if (!hotel.m_street.empty())
    params.AddStreet(hotel.m_street);

  if (!hotel.m_houseNumber.empty())
    params.AddHouseNumber(hotel.m_houseNumber);

  if (!hotel.m_translations.empty())
  {
    // TODO(mgsergio): Move parsing to the hotel costruction stage.
    std::vector<std::string> parts;
    strings::ParseCSVRow(hotel.m_translations, '|', parts);
    CHECK_EQUAL(parts.size() % 3, 0, ("Invalid translation string:", hotel.m_translations));
    for (size_t i = 0; i < parts.size(); i += 3)
    {
      auto const langCode = StringUtf8Multilang::GetLangIndex(parts[i]);
      params.AddName(StringUtf8Multilang::GetLangByCode(langCode), parts[i + 1]);
      // TODO(mgsergio): e.AddTag("addr:full:" + parts[i], parts[i + 2]);
    }
  }
  params.AddName(StringUtf8Multilang::GetLangByCode(StringUtf8Multilang::kEnglishCode),
                 hotel.m_name);

  auto const & clf = classif();
  params.AddType(clf.GetTypeByPath({"sponsored", "booking"}));
  // Matching booking.com hotel types to OpenStreetMap values.
  // Booking types are listed in the closed API docs.
  switch (hotel.m_type)
  {
    case 19:
    case 205: params.AddType(clf.GetTypeByPath({"tourism", "motel"})); break;

    case 21:
    case 206:
    case 212: params.AddType(clf.GetTypeByPath({"tourism", "resort"})); break;

    case 3:
    case 23:
    case 24:
    case 25:
    case 202:
    case 207:
    case 208:
    case 209:
    case 210:
    case 216:
    case 220:
    case 223: params.AddType(clf.GetTypeByPath({"tourism", "guest_house"})); break;

    case 14:
    case 204:
    case 213:
    case 218:
    case 219:
    case 226:
    case 222: params.AddType(clf.GetTypeByPath({"tourism", "hotel"})); break;

    case 211:
    case 224:
    case 228: params.AddType(clf.GetTypeByPath({"tourism", "chalet"})); break;

    case 13:
    case 225:
    case 203: params.AddType(clf.GetTypeByPath({"tourism", "hostel"})); break;

    case 215:
    case 221:
    case 227:
    case 2:
    case 201: params.AddType(clf.GetTypeByPath({"tourism", "apartment"})); break;

    case 214: params.AddType(clf.GetTypeByPath({"tourism", "camp_site"})); break;

    default: params.AddType(clf.GetTypeByPath({"tourism", "hotel"})); break;
  }

  fn(fb);
}

template <>
BookingDataset::ObjectId BookingDataset::FindMatchingObjectIdImpl(FeatureBuilder const & fb) const
{
  auto const name = fb.GetName(StringUtf8Multilang::kDefaultCode);

  if (name.empty())
    return Object::InvalidObjectId();

  // Find |kMaxSelectedElements| nearest values to a point.
  auto const bookingIndexes =
      m_storage.GetNearestObjects(mercator::ToLatLon(fb.GetKeyPoint()));

  for (auto const j : bookingIndexes)
  {
    if (sponsored_scoring::Match(m_storage.GetObjectById(j), fb).IsMatched())
      return j;
  }

  return Object::InvalidObjectId();
}
}  // namespace generator