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
|
#include "search/search_query_params.hpp"
#include "indexer/feature_impl.hpp"
#include "indexer/scales.hpp"
#include "base/assert.hpp"
#include "std/algorithm.hpp"
namespace search
{
namespace
{
class DoAddStreetSynonyms
{
public:
DoAddStreetSynonyms(SearchQueryParams & params) : m_params(params) {}
void operator()(SearchQueryParams::TString const & s, size_t i)
{
if (s.size() > 2)
return;
string const ss = strings::ToUtf8(strings::MakeLowerCase(s));
// All synonyms should be lowercase!
if (ss == "n")
AddSym(i, "north");
if (ss == "w")
AddSym(i, "west");
if (ss == "s")
AddSym(i, "south");
if (ss == "e")
AddSym(i, "east");
if (ss == "nw")
AddSym(i, "northwest");
if (ss == "ne")
AddSym(i, "northeast");
if (ss == "sw")
AddSym(i, "southwest");
if (ss == "se")
AddSym(i, "southeast");
}
private:
SearchQueryParams::TSynonymsVector & GetSyms(size_t i) const
{
size_t const count = m_params.m_tokens.size();
if (i < count)
return m_params.m_tokens[i];
ASSERT_EQUAL(i, count, ());
return m_params.m_prefixTokens;
}
void AddSym(size_t i, string const & sym) { GetSyms(i).push_back(strings::MakeUniString(sym)); }
SearchQueryParams & m_params;
};
} // namespace
SearchQueryParams::SearchQueryParams() : m_scale(scales::GetUpperScale()) {}
void SearchQueryParams::Clear()
{
m_tokens.clear();
m_prefixTokens.clear();
m_langs.clear();
m_scale = scales::GetUpperScale();
}
void SearchQueryParams::EraseTokens(vector<size_t> & eraseInds)
{
eraseInds.erase(unique(eraseInds.begin(), eraseInds.end()), eraseInds.end());
ASSERT(is_sorted(eraseInds.begin(), eraseInds.end()), ());
// fill temporary vector
vector<TSynonymsVector> newTokens;
size_t skipI = 0;
size_t const count = m_tokens.size();
size_t const eraseCount = eraseInds.size();
for (size_t i = 0; i < count; ++i)
{
if (skipI < eraseCount && eraseInds[skipI] == i)
++skipI;
else
newTokens.push_back(move(m_tokens[i]));
}
// assign to m_tokens
newTokens.swap(m_tokens);
if (skipI < eraseCount)
{
// it means that we need to skip prefix tokens
ASSERT_EQUAL(skipI + 1, eraseCount, (eraseInds));
ASSERT_EQUAL(eraseInds[skipI], count, (eraseInds));
m_prefixTokens.clear();
}
}
void SearchQueryParams::ProcessAddressTokens()
{
// Erases all number tokens.
// Assumes that USA street name numbers are end with "st, nd, rd, th" suffixes.
vector<size_t> toErase;
ForEachToken([&toErase](SearchQueryParams::TString const & s, size_t i)
{
if (feature::IsNumber(s))
toErase.push_back(i);
});
EraseTokens(toErase);
// Adds synonyms for N, NE, NW, etc.
ForEachToken(DoAddStreetSynonyms(*this));
}
template <class ToDo>
void SearchQueryParams::ForEachToken(ToDo && toDo)
{
size_t const count = m_tokens.size();
for (size_t i = 0; i < count; ++i)
{
ASSERT(!m_tokens[i].empty(), ());
ASSERT(!m_tokens[i].front().empty(), ());
toDo(m_tokens[i].front(), i);
}
if (!m_prefixTokens.empty())
{
ASSERT(!m_prefixTokens.front().empty(), ());
toDo(m_prefixTokens.front(), count);
}
}
} // namespace search
string DebugPrint(search::SearchQueryParams const & params)
{
ostringstream os;
os << "SearchQueryParams [ m_tokens=" << DebugPrint(params.m_tokens)
<< ", m_prefixTokens=" << DebugPrint(params.m_prefixTokens) << "]";
return os.str();
}
|