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
|
#include "geocoder/hierarchy.hpp"
#include "indexer/search_string_utils.hpp"
#include "base/assert.hpp"
#include "base/exception.hpp"
#include "base/logging.hpp"
#include "base/macros.hpp"
#include <fstream>
using namespace std;
namespace geocoder
{
// Hierarchy::Entry --------------------------------------------------------------------------------
bool Hierarchy::Entry::DeserializeFromJSON(string const & jsonStr, ParsingStats & stats)
{
try
{
base::Json root(jsonStr.c_str());
DeserializeFromJSONImpl(root.get(), jsonStr, stats);
return true;
}
catch (base::Json::Exception const & e)
{
LOG(LDEBUG, ("Can't parse entry:", e.Msg(), jsonStr));
}
return false;
}
// todo(@m) Factor out to geojson.hpp? Add geojson to myjansson?
void Hierarchy::Entry::DeserializeFromJSONImpl(json_t * const root, string const & jsonStr,
ParsingStats & stats)
{
if (!json_is_object(root))
{
++stats.m_badJsons;
MYTHROW(base::Json::Exception, ("Not a json object."));
}
json_t * const properties = base::GetJSONObligatoryField(root, "properties");
json_t * const address = base::GetJSONObligatoryField(properties, "address");
bool hasDuplicateAddress = false;
for (size_t i = 0; i < static_cast<size_t>(Type::Count); ++i)
{
Type const type = static_cast<Type>(i);
string const & levelKey = ToString(type);
string levelValue;
FromJSONObjectOptionalField(address, levelKey, levelValue);
if (levelValue.empty())
continue;
if (!m_address[i].empty())
{
LOG(LDEBUG, ("Duplicate address field", type, "when parsing", jsonStr));
hasDuplicateAddress = true;
}
search::NormalizeAndTokenizeString(levelValue, m_address[i]);
if (!m_address[i].empty())
m_type = static_cast<Type>(i);
}
m_nameTokens.clear();
FromJSONObjectOptionalField(properties, "name", m_name);
search::NormalizeAndTokenizeString(m_name, m_nameTokens);
if (m_name.empty())
++stats.m_emptyNames;
if (hasDuplicateAddress)
++stats.m_duplicateAddresses;
if (m_type == Type::Count)
{
LOG(LDEBUG, ("No address in an hierarchy entry:", jsonStr));
++stats.m_emptyAddresses;
}
else if (m_nameTokens != m_address[static_cast<size_t>(m_type)])
{
++stats.m_mismatchedNames;
LOG(LDEBUG, ("Hierarchy entry name is not the most detailed field in its address:", jsonStr));
}
}
// Hierarchy ---------------------------------------------------------------------------------------
Hierarchy::Hierarchy(string const & pathToJsonHierarchy)
{
ifstream ifs(pathToJsonHierarchy);
string line;
ParsingStats stats;
while (getline(ifs, line))
{
if (line.empty())
continue;
auto i = line.find(' ');
int64_t encodedId;
if (i == string::npos || !strings::to_any(line.substr(0, i), encodedId))
{
LOG(LWARNING, ("Cannot read osm id. Line:", line));
++stats.m_badOsmIds;
continue;
}
line = line.substr(i + 1);
Entry entry;
// todo(@m) We should really write uints as uints.
entry.m_osmId = base::GeoObjectId(static_cast<uint64_t>(encodedId));
if (!entry.DeserializeFromJSON(line, stats))
continue;
// The entry is indexed only by its address.
// todo(@m) Index it by name too.
if (entry.m_type != Type::Count)
{
++stats.m_numLoaded;
size_t const t = static_cast<size_t>(entry.m_type);
m_entries[entry.m_address[t]].emplace_back(entry);
}
}
LOG(LINFO, ("Finished reading the hierarchy. Stats:"));
LOG(LINFO, ("Entries indexed:", stats.m_numLoaded));
LOG(LINFO, ("Corrupted json lines:", stats.m_badJsons));
LOG(LINFO, ("Unreadable base::GeoObjectIds:", stats.m_badOsmIds));
LOG(LINFO, ("Entries with duplicate address parts:", stats.m_duplicateAddresses));
LOG(LINFO, ("Entries without address:", stats.m_emptyAddresses));
LOG(LINFO, ("Entries without names:", stats.m_emptyNames));
LOG(LINFO,
("Entries whose names do not match their most specific addresses:", stats.m_mismatchedNames));
LOG(LINFO, ("(End of stats.)"));
}
vector<Hierarchy::Entry> const * const Hierarchy::GetEntries(
vector<strings::UniString> const & tokens) const
{
auto it = m_entries.find(tokens);
if (it == m_entries.end())
return {};
return &it->second;
}
} // namespace geocoder
|