-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.cpp
76 lines (62 loc) · 1.7 KB
/
update.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
#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include <jubatus/client.hpp>
#include <cassert>
#include <unistd.h>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
using std::make_pair;
using std::pair;
using std::string;
using std::vector;
using jubatus::client::common::datum;
using jubatus::classifier::estimate_result;
using jubatus::classifier::labeled_datum;
datum make_datum(const char *filedata) {
datum d;
d.add_string("filedata", filedata);
return d;
}
int main(int argc, char *argv[]) {
assert(2 == argc);
string host = "127.0.0.1";
int port = 9199;
string name = "test";
jubatus::classifier::client::classifier *client;
client = new jubatus::classifier::client::classifier(host, port, name, 1.0);
fs::path rootdir(argv[1]);
typedef fs::directory_iterator dirit_t;
dirit_t endit;
for (dirit_t dirit(rootdir); dirit != endit; dirit++) {
if (!fs::is_directory(dirit->status()))
continue;
fs::path dir = dirit->path();
for (dirit_t dirit(dir); dirit != endit; dirit++) {
if (!fs::is_regular_file(dirit->status()))
continue;
fs::path file = dirit->path();
std::ifstream ifs(file.c_str());
char rbuf[1024*1024];
ifs.read(rbuf, sizeof(rbuf) - 1);
rbuf[ifs.gcount()] = '\0';
std::vector<labeled_datum> train_data;
train_data.push_back(
labeled_datum(dir.c_str(), make_datum(rbuf)));
retry:
try {
client->train(train_data);
} catch (msgpack::rpc::timeout_error &te) {
std::cout << "timed out: " << file.c_str() << std::endl;
sleep(1);
delete client;
client = new jubatus::classifier::client::classifier(
host, port, name, 1.0);
goto retry;
}
}
}
return 0;
}