-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrclss.cpp
57 lines (48 loc) · 1.53 KB
/
rclss.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
#include "io_func.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace dlib;
using namespace std;
std::string modelfname;
if (argc == 1)
{
modelfname = "kmeans_model";
}
else if (argc == 2)
{
modelfname = std::string(argv[1]);
}
else
{
std::cout << "Usage: rclss <model_fname>" << std::endl;
return 0;
}
kcentroid<kernel_type> kc;
kkmeans<kernel_type> kmeans_clustering(kc);
dlib::vector_normalizer<feature_point> normalizer;
std::vector<std::vector<feature_point>> clusters;
cout << "Deserializing." << endl;
deserialize_model(modelfname, kmeans_clustering, normalizer, clusters);
cout << "Reading input." << endl;
for(std::string line; getline(cin, line); )
{
auto item = parse_line(line);
auto item_normalized = normalizer(item);
auto cluster_index = kmeans_clustering(item_normalized);
auto kernel = kmeans_clustering.get_kcentroid(cluster_index).get_kernel();
distance_function<kernel_type> distance_func(kernel, item_normalized);
auto cluster = clusters[cluster_index];
std::map<float, int> ordered_points;
for(size_t p = 0; p < cluster.size(); ++p)
{
ordered_points.emplace(distance_func(normalizer(cluster[p])), p);
}
cout << "Closest offers: " << std::endl;
for(const auto& pair: ordered_points)
{
print_formatted(cout, cluster[pair.second]);
}
}
return 0;
}