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
|
#include "indexer/search_string_utils.hpp"
#include "indexer/string_set.hpp"
#include "base/assert.hpp"
#include "base/macros.hpp"
#include "base/mem_trie.hpp"
#include "3party/utfcpp/source/utf8/unchecked.h"
#include <algorithm>
using namespace std;
using namespace strings;
namespace search
{
namespace
{
// Replaces '#' followed by an end-of-string or a digit with space.
void RemoveNumeroSigns(UniString & s)
{
size_t const n = s.size();
size_t i = 0;
while (i < n)
{
if (s[i] != '#')
{
++i;
continue;
}
size_t j = i + 1;
while (j < n && IsASCIISpace(s[j]))
++j;
if (j == n || IsASCIIDigit(s[j]))
s[i] = ' ';
i = j;
}
}
} // namespace
UniString NormalizeAndSimplifyString(string const & s)
{
UniString uniString = MakeUniString(s);
for (size_t i = 0; i < uniString.size(); ++i)
{
UniChar & c = uniString[i];
switch (c)
{
// Replace "d with stroke" to simple d letter. Used in Vietnamese.
// (unicode-compliant implementation leaves it unchanged)
case 0x0110:
case 0x0111:
c = 'd';
break;
// Replace small turkish dotless 'ı' with dotted 'i'. Our own
// invented hack to avoid well-known Turkish I-letter bug.
case 0x0131:
c = 'i';
break;
// Replace capital turkish dotted 'İ' with dotted lowercased 'i'.
// Here we need to handle this case manually too, because default
// unicode-compliant implementation of MakeLowerCase converts 'İ'
// to 'i' + 0x0307.
case 0x0130:
c = 'i';
break;
// Some Danish-specific hacks.
case 0x00d8: // Ø
case 0x00f8: // ø
c = 'o';
break;
case 0x0152: // Œ
case 0x0153: // œ
c = 'o';
uniString.insert(uniString.begin() + (i++) + 1, 'e');
break;
case 0x00c6: // Æ
case 0x00e6: // æ
c = 'a';
uniString.insert(uniString.begin() + (i++) + 1, 'e');
break;
case 0x2116: // №
c = '#';
break;
}
}
MakeLowerCaseInplace(uniString);
NormalizeInplace(uniString);
// Remove accents that can appear after NFKD normalization.
uniString.erase_if([](UniChar const & c) {
// ̀ COMBINING GRAVE ACCENT
// ́ COMBINING ACUTE ACCENT
return (c == 0x0300 || c == 0x0301);
});
RemoveNumeroSigns(uniString);
return uniString;
/// @todo Restore this logic to distinguish и-й in future.
/*
// Just after lower casing is a correct place to avoid normalization for specific chars.
static auto const isSpecificChar = [](UniChar c) -> bool
{
return c == 0x0439; // й
};
UniString result;
result.reserve(uniString.size());
for (auto i = uniString.begin(), end = uniString.end(); i != end;)
{
auto j = find_if(i, end, isSpecificChar);
// We don't check if (j != i) because UniString and Normalize handle it correctly.
UniString normString(i, j);
NormalizeInplace(normString);
result.insert(result.end(), normString.begin(), normString.end());
if (j == end)
break;
result.push_back(*j);
i = j + 1;
}
return result;
*/
}
UniString FeatureTypeToString(uint32_t type)
{
string const s = "!type:" + to_string(type);
return UniString(s.begin(), s.end());
}
namespace
{
char const * kStreetTokensSeparator = "\t -,.";
/// @todo Move prefixes, suffixes into separate file (autogenerated).
/// It's better to distinguish synonyms comparison according to language/region.
class StreetsSynonymsHolder
{
public:
struct BooleanSum
{
using value_type = bool;
BooleanSum() { Clear(); }
void Add(bool value)
{
m_value = m_value || value;
m_empty = false;
}
template <typename ToDo>
void ForEach(ToDo && toDo) const
{
toDo(m_value);
}
void Clear()
{
m_value = false;
m_empty = true;
}
bool Empty() const { return m_empty; }
void Swap(BooleanSum & rhs)
{
swap(m_value, rhs.m_value);
swap(m_empty, rhs.m_empty);
}
bool m_value;
bool m_empty;
};
StreetsSynonymsHolder()
{
char const * affics[] =
{
// Russian
"аллея", "бульвар", "набережная", "переулок", "площадь", "проезд", "проспект", "шоссе", "тупик", "улица", "тракт", "ал", "бул", "наб", "пер", "пл", "пр", "просп", "ш", "туп", "ул", "тр",
// English
"street", "avenue", "square", "road", "boulevard", "drive", "highway", "lane", "way", "circle", "st", "av", "ave", "sq", "rd", "blvd", "dr", "hwy", "ln",
// Lithuanian
"g", "pr", "pl", "kel",
// Български език - Bulgarian
"булевард", "бул", "площад", "пл", "улица", "ул", "квартал", "кв",
// Canada - Canada
"allee", "alley", "autoroute", "aut", "bypass", "byway", "carrefour", "carref", "chemin", "cercle", "circle", "côte", "crossing", "cross", "expressway", "freeway", "fwy", "line", "link", "loop", "parkway", "pky", "pkwy", "path", "pathway", "ptway", "route", "rue", "rte", "trail", "walk",
// Cesky - Czech
"ulice", "ul", "náměstí", "nám",
// Deutsch - German
"allee", "al", "brücke", "br", "chaussee", "gasse", "gr", "pfad", "straße", "str", "weg", "platz",
// Español - Spanish
"avenida", "avd", "avda", "bulevar", "bulev", "calle", "calleja", "cllja", "callejón", "callej", "cjon", "cllon", "callejuela", "cjla", "callizo", "cllzo", "calzada", "czada", "costera", "coste", "plza", "pza", "plazoleta", "pzta", "plazuela", "plzla", "tránsito", "trans", "transversal", "trval", "trasera", "tras", "travesía", "trva",
// Français - French
"rue", "avenue", "carré", "cercle", "route", "boulevard", "drive", "autoroute", "lane", "chemin",
// Nederlands - Dutch
"laan", "ln.", "straat", "steenweg", "stwg", "st",
// Norsk - Norwegian
"vei", "veien", "vn", "gaten", "gata", "gt", "plass", "plassen", "sving", "svingen", "sv",
// Polski - Polish
"aleja", "aleje", "aleji", "alejach", "aleją", "plac", "placu", "placem", "ulica", "ulicy",
// Português - Portuguese
"street", "avenida", "quadrado", "estrada", "boulevard", "carro", "auto-estrada", "lane", "caminho",
// Română - Romanian
"bul", "bdul", "blv", "bulevard", "bulevardu", "calea", "cal", "piața", "pţa", "pța", "strada", "stra", "stradela", "sdla", "stradă", "unitate", "autostradă", "lane",
// Slovenščina - Slovenian
"cesta",
// Suomi - Finnish
"kaari", "kri", "katu", "kuja", "kj", "kylä", "polku", "tie", "t", "tori", "väylä", "vlä",
// Svenska - Swedish
"väg", "vägen", "gatan", "gränd", "gränden", "stig", "stigen", "plats", "platsen",
// Türkçe - Turkish
"sokak", "sk", "sok", "sokağı", "cadde", "cd", "caddesi", "bulvar", "bulvarı",
// Tiếng Việt – Vietnamese
"quốc lộ", "ql", "tỉnh lộ", "tl", "Đại lộ", "Đl", "Đường", "Đ", "Đường sắt", "Đs", "Đường phố", "Đp", "vuông", "con Đường", "Đại lộ", "Đường cao tốc",
// Українська - Ukrainian
"дорога", "провулок", "площа", "шосе", "вулиця", "дор", "пров", "вул"
};
for (auto const * s : affics)
{
UniString const us = NormalizeAndSimplifyString(s);
m_strings.Add(us, true /* end of string */);
}
}
bool MatchPrefix(UniString const & s) const { return m_strings.HasPrefix(s); }
bool FullMatch(UniString const & s) const { return m_strings.HasKey(s); }
private:
base::MemTrie<UniString, BooleanSum, base::VectorMoves> m_strings;
};
StreetsSynonymsHolder g_streets;
} // namespace
string DropLastToken(string const & str)
{
search::Delimiters delims;
using Iter = utf8::unchecked::iterator<string::const_iterator>;
// Find start iterator of prefix in input query.
Iter iter(str.end());
while (iter.base() != str.begin())
{
Iter prev = iter;
--prev;
if (delims(*prev))
break;
iter = prev;
}
return string(str.begin(), iter.base());
}
UniString GetStreetNameAsKey(string const & name, bool ignoreStreetSynonyms)
{
if (name.empty())
return UniString();
UniString res;
SimpleTokenizer iter(name, kStreetTokensSeparator);
while (iter)
{
UniString const s = NormalizeAndSimplifyString(*iter);
++iter;
if (ignoreStreetSynonyms && IsStreetSynonym(s))
continue;
res.append(s);
}
return (res.empty() ? NormalizeAndSimplifyString(name) : res);
}
bool IsStreetSynonym(UniString const & s)
{
return g_streets.FullMatch(s);
}
bool IsStreetSynonymPrefix(UniString const & s)
{
return g_streets.MatchPrefix(s);
}
bool ContainsNormalized(string const & str, string const & substr)
{
UniString const ustr = NormalizeAndSimplifyString(str);
UniString const usubstr = NormalizeAndSimplifyString(substr);
return std::search(ustr.begin(), ustr.end(), usubstr.begin(), usubstr.end()) != ustr.end();
}
// StreetTokensFilter ------------------------------------------------------------------------------
void StreetTokensFilter::Put(strings::UniString const & token, bool isPrefix, size_t tag)
{
if ((isPrefix && IsStreetSynonymPrefix(token)) || (!isPrefix && IsStreetSynonym(token)))
{
++m_numSynonyms;
if (m_numSynonyms == 1)
{
m_delayedToken = token;
m_delayedTag = tag;
return;
}
if (m_numSynonyms == 2)
EmitToken(m_delayedToken, m_delayedTag);
}
EmitToken(token, tag);
}
} // namespace search
|