-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
77 lines (67 loc) · 2.17 KB
/
main.cpp
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
#include "JsonReader.h"
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <exception>
#include <filesystem>
#include <chrono>
int main(int argc, const char* const argv[])
{
try
{
if (argc != 2 || !std::filesystem::exists(argv[1]))
{
throw std::invalid_argument("input file path");
}
JsonReader reader;
std::unordered_map<std::string, uint32_t> property_count_map;
property_count_map.reserve(9000); // Wikidata has ~8800 properties
reader.onObjectBegin("claims", [&]()
{
reader.onPair("property", [&](const char* property_name)
{
++property_count_map[property_name];
});
});
std::function<void(const char*)> no_callback;
reader.onObjectEnd("claims", [&]()
{
reader.onPair("property", no_callback);
});
using clock = std::chrono::high_resolution_clock;
auto start_time = clock::now();
reader.readFile(argv[1]);
std::cout << "duration: " << std::chrono::duration_cast<std::chrono::seconds>(clock::now() - start_time).count() << " s\n\n";
struct property_count_data: public std::pair<const std::string*, uint32_t>
{
using parent = std::pair<const std::string*, uint32_t>;
using parent::parent;
bool operator < (const property_count_data& other) const noexcept
{
return this->second < other.second;
}
};
std::vector<property_count_data> data;
data.reserve(property_count_map.size());
for(const auto& item : property_count_map)
{
data.emplace_back(property_count_data{&item.first, item.second});
}
std::sort(data.rbegin(), data.rend());
for (const auto& item : data)
{
std::cout << *item.first << ';' << item.second << std::endl;
}
}
catch(const std::exception& ex)
{
std::cout << "error: " << ex.what() << std::endl;
return -1;
}
catch(...)
{
std::cout << "error: weird exception\n";
return -2;
}
return 0;
}