forked from nagadomi/kaggle-lshtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.hpp
72 lines (64 loc) · 1.08 KB
/
reader.hpp
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
#ifndef READER_H
#define READER_H
#include "util.hpp"
#include <fstream>
#include <sstream>
class DataReader
{
private:
std::ifstream m_fp;
char m_buffer[1024 * 1024];
public:
bool
open(const char *file)
{
m_fp.open(file, std::ifstream::in);
if (!m_fp) {
return false;
}
m_fp.rdbuf()->pubsetbuf(m_buffer, sizeof(m_buffer));
return true;
}
void
read(std::vector<fv_t> &data,
std::vector<label_t> &labels)
{
std::string line;
data.clear();
labels.clear();
getline(m_fp, line); // skip headeer
while (getline(m_fp, line)) {
std::istringstream is(line);
fv_t fv;
label_t label;
char sep;
float value;
int id;
is >> std::noskipws;
while (is >> id >> sep) {
if (sep == ',') {
label.insert(id);
is >> sep;
if (sep != ' ') {
is.putback(sep);
}
} else {
label.insert(id);
break;
}
}
while (is >> id >> sep >> value) {
fv.insert(std::make_pair(id, value));
is >> sep;
}
data.push_back(fv);
labels.push_back(label);
}
}
void
close(void)
{
m_fp.close();
}
};
#endif