-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshufflels.cpp
More file actions
58 lines (53 loc) · 1.5 KB
/
shufflels.cpp
File metadata and controls
58 lines (53 loc) · 1.5 KB
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
#include <iostream>
#include <fstream>
#include <filesystem>
#include <list>
#include <cstring>
typedef struct {
std::string filename;
unsigned random;
} entry_t;
int main(int argc, char **argv) {
if (argc > 3) {
const auto &path = argv[1];
const auto &extension = argv[2];
auto percent = atoi(argv[3]);
if (percent > 100 || percent < 0) {
percent = 80;
}
try {
std::list<entry_t> list;
srand(time(nullptr));
for (const auto &entry: std::filesystem::directory_iterator(path)) {
if (entry.is_regular_file()) {
auto name = entry.path();
auto name_str = name.string();
auto name_ext = name.extension().string();
auto s = name_ext.c_str();
if (s[0] == '.' && !strcmp(&s[1], extension)) {
list.push_back({name.string(), (unsigned) rand() * (unsigned) rand()});
}
}
}
list.sort([](const entry_t &a, const entry_t &b) { return a.random < b.random; });
const auto train_count = list.size() * percent / 100;
std::ofstream f("train.lst", std::ios_base::app);
auto it = list.cbegin();
for (size_t i = 0; i < train_count; i++, it++) {
f << it->filename << std::endl;
}
f.close();
f = std::ofstream("test.lst", std::ios_base::app);
while (it != list.end()) {
f << it++->filename << std::endl;
}
f.close();
}
catch (const std::exception &e) {
std::cerr << e.what();
}
}
else {
std::cout << "Usage:\r\n\tshufflels directory extension percent_train";
}
}