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
|
#pragma once
#include "indexer/feature_data.hpp"
#include "indexer/classificator.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "base/string_utils.hpp"
#include "std/string.hpp"
class MetadataTagProcessor
{
FeatureParams & m_params;
public:
MetadataTagProcessor(FeatureParams ¶ms)
: m_params(params)
{
}
bool operator() (string const & k, string const & v)
{
if (v.empty())
return false;
using feature::Metadata;
Metadata & md = m_params.GetMetadata();
if (k == "cuisine")
{
string const & value = ValidateAndFormat_cuisine(v);
if (!value.empty())
md.Add(Metadata::FMD_CUISINE, value);
}
else if (k == "phone" || k == "contact:phone")
{
string const & value = ValidateAndFormat_phone(v);
if (!value.empty())
md.Add(Metadata::FMD_PHONE_NUMBER, value);
}
else if (k == "maxspeed")
{
string const & value = ValidateAndFormat_maxspeed(v);
if (!value.empty())
md.Add(Metadata::FMD_MAXSPEED, value);
}
else if (k == "stars")
{
string const & value = ValidateAndFormat_stars(v);
if (!value.empty())
md.Add(Metadata::FMD_STARS, value);
}
else if (k == "addr:postcode")
{
string const & value = ValidateAndFormat_postcode(v);
if (!value.empty())
md.Add(Metadata::FMD_POSTCODE, value);
}
else if (k == "url")
{
string const & value = ValidateAndFormat_url(v);
if (!value.empty())
md.Add(Metadata::FMD_URL, value);
}
else if (k == "website" || k == "contact:website")
{
string const & value = ValidateAndFormat_url(v);
if (!value.empty())
md.Add(Metadata::FMD_WEBSITE, value);
}
else if (k == "operator")
{
string const & value = ValidateAndFormat_operator(v);
if (!value.empty())
md.Add(Metadata::FMD_OPERATOR, value);
}
else if (k == "opening_hours")
{
string const & value = ValidateAndFormat_opening_hours(v);
if (!value.empty())
md.Add(Metadata::FMD_OPEN_HOURS, value);
}
else if (k == "ele")
{
string const & value = ValidateAndFormat_ele(v);
if (!value.empty())
md.Add(Metadata::FMD_ELE, value);
}
else if (k == "turn:lanes")
{
string const & value = ValidateAndFormat_turn_lanes(v);
if (!value.empty())
md.Add(Metadata::FMD_TURN_LANES, value);
}
else if (k == "turn:lanes:forward")
{
string const & value = ValidateAndFormat_turn_lanes_forward(v);
if (!value.empty())
md.Add(Metadata::FMD_TURN_LANES_FORWARD, value);
}
else if (k == "turn:lanes:backward")
{
string const & value = ValidateAndFormat_turn_lanes_backward(v);
if (!value.empty())
md.Add(Metadata::FMD_TURN_LANES_BACKWARD, value);
}
else if (k == "email" || k == "contact:email")
{
string const & value = ValidateAndFormat_email(v);
if (!value.empty())
md.Add(Metadata::FMD_EMAIL, value);
}
else if (k == "wikipedia")
{
string const & value = ValidateAndFormat_wikipedia(v);
if (!value.empty())
md.Add(Metadata::FMD_WIKIPEDIA, value);
}
return false;
}
protected:
/// Validation and formatting functions
string ValidateAndFormat_maxspeed(string const & v) const
{
if (!ftypes::IsSpeedCamChecker::Instance()(m_params.m_Types))
return string();
return v;
}
string ValidateAndFormat_stars(string const & v) const
{
if (v.empty())
return string();
// we accepting stars from 1 to 7
if (v[0] <= '0' || v[0] > '7')
return string();
// ignore numbers large then 9
if (v.size() > 1 && (v[1] >= '0' && v[1] <= '9'))
return string();
return string(1, v[0]);
}
string ValidateAndFormat_cuisine(string const & v) const
{
return v;
}
string ValidateAndFormat_operator(string const & v) const
{
static ftypes::IsATMChecker const IsATM;
static ftypes::IsFuelStationChecker const IsFuelStation;
if (!(IsATM(m_params.m_Types) || IsFuelStation(m_params.m_Types)))
return string();
return v;
}
string ValidateAndFormat_url(string const & v) const
{
return v;
}
string ValidateAndFormat_phone(string const & v) const
{
return v;
}
string ValidateAndFormat_opening_hours(string const & v) const
{
return v;
}
string ValidateAndFormat_ele(string const & v) const
{
static ftypes::IsPeakChecker const IsPeak;
if (!IsPeak(m_params.m_Types))
return string();
double val = 0;
if(!strings::to_double(v, val) || val == 0)
return string();
return v;
}
string ValidateAndFormat_turn_lanes(string const & v) const
{
return v;
}
string ValidateAndFormat_turn_lanes_forward(string const & v) const
{
return v;
}
string ValidateAndFormat_turn_lanes_backward(string const & v) const
{
return v;
}
string ValidateAndFormat_email(string const & v) const
{
return v;
}
string ValidateAndFormat_postcode(string const & v) const
{
return v;
}
// Special URL encoding for wikipedia:
// Replaces special characters with %HH codes
// And spaces with underscores.
string WikiUrlEncode(string const & value) const
{
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (auto const & c : value)
{
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
escaped << c;
else if (c == ' ')
escaped << '_';
else
escaped << '%' << std::uppercase << setw(2) << static_cast<int>(static_cast<unsigned char>(c));
}
return escaped.str();
}
string ValidateAndFormat_wikipedia(string const & v) const
{
// Find prefix before ':', shortest case: "lg:aa".
string::size_type i = v.find(':');
if (i == string::npos || i < 2 || i + 2 > v.length())
return string();
// URL encode lang:title (lang is at most 3 chars), so URL can be reconstructed faster.
if (i <= 3 || v.substr(0, i) == "be-x-old")
return v.substr(0, i + 1) + WikiUrlEncode(v.substr(i + 1));
static string::size_type const minUrlPartLength = string("//be.wikipedia.org/wiki/AB").length();
if (v[i+1] == '/' && i + minUrlPartLength < v.length())
{
// Convert URL to "lang:title".
i += 3; // skip "://"
string::size_type const j = v.find('.', i + 1);
static string const wikiUrlPart = ".wikipedia.org/wiki/";
if (j != string::npos && v.substr(j, wikiUrlPart.length()) == wikiUrlPart)
return v.substr(i, j - i) + ":" + v.substr(j + wikiUrlPart.length());
}
return string();
}
};
|