-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathparser.cpp
172 lines (157 loc) · 4.7 KB
/
parser.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*!
* Copyright (c) 2016 Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/
#include "parser.hpp"
#include <string>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
namespace LightGBM {
void GetStatistic(const char* str, int* comma_cnt, int* tab_cnt, int* colon_cnt) {
*comma_cnt = 0;
*tab_cnt = 0;
*colon_cnt = 0;
for (int i = 0; str[i] != '\0'; ++i) {
if (str[i] == ',') {
++(*comma_cnt);
} else if (str[i] == '\t') {
++(*tab_cnt);
} else if (str[i] == ':') {
++(*colon_cnt);
}
}
}
int GetLabelIdxForLibsvm(std::string& str, int num_features, int label_idx) {
if (num_features <= 0) {
return label_idx;
}
str = Common::Trim(str);
auto pos_space = str.find_first_of(" \f\n\r\t\v");
auto pos_colon = str.find_first_of(":");
if (pos_space == std::string::npos || pos_space < pos_colon) {
return label_idx;
} else {
return -1;
}
}
int GetLabelIdxForTSV(std::string& str, int num_features, int label_idx) {
if (num_features <= 0) {
return label_idx;
}
str = Common::Trim(str);
auto tokens = Common::Split(str.c_str(), '\t');
if (static_cast<int>(tokens.size()) == num_features) {
return -1;
} else {
return label_idx;
}
}
int GetLabelIdxForCSV(std::string& str, int num_features, int label_idx) {
if (num_features <= 0) {
return label_idx;
}
str = Common::Trim(str);
auto tokens = Common::Split(str.c_str(), ',');
if (static_cast<int>(tokens.size()) == num_features) {
return -1;
} else {
return label_idx;
}
}
enum DataType {
INVALID,
CSV,
TSV,
LIBSVM
};
void getline(std::stringstream& ss, std::string& line, const VirtualFileReader* reader, std::vector<char>& buffer, size_t buffer_size) {
std::getline(ss, line);
while (ss.eof()) {
size_t read_len = reader->Read(buffer.data(), buffer_size);
if (read_len <= 0) {
break;
}
ss.clear();
ss.str(std::string(buffer.data(), read_len));
std::string tmp;
std::getline(ss, tmp);
line += tmp;
}
}
Parser* Parser::CreateParser(const char* filename, bool header, int num_features, int label_idx) {
auto reader = VirtualFileReader::Make(filename);
if (!reader->Init()) {
Log::Fatal("Data file %s doesn't exist", filename);
}
std::string line1, line2;
size_t buffer_size = 64 * 1024;
auto buffer = std::vector<char>(buffer_size);
size_t read_len = reader->Read(buffer.data(), buffer_size);
if (read_len <= 0) {
Log::Fatal("Data file %s couldn't be read", filename);
}
std::stringstream tmp_file(std::string(buffer.data(), read_len));
if (header) {
if (!tmp_file.eof()) {
getline(tmp_file, line1, reader.get(), buffer, buffer_size);
}
}
if (!tmp_file.eof()) {
getline(tmp_file, line1, reader.get(), buffer, buffer_size);
} else {
Log::Fatal("Data file %s should have at least one line", filename);
}
if (!tmp_file.eof()) {
getline(tmp_file, line2, reader.get(), buffer, buffer_size);
} else {
Log::Warning("Data file %s only has one line", filename);
}
int comma_cnt = 0, comma_cnt2 = 0;
int tab_cnt = 0, tab_cnt2 = 0;
int colon_cnt = 0, colon_cnt2 = 0;
// Get some statistic from 2 line
GetStatistic(line1.c_str(), &comma_cnt, &tab_cnt, &colon_cnt);
GetStatistic(line2.c_str(), &comma_cnt2, &tab_cnt2, &colon_cnt2);
DataType type = DataType::INVALID;
if (line2.size() == 0) {
// if only have one line on file
if (colon_cnt > 0) {
type = DataType::LIBSVM;
} else if (tab_cnt > 0) {
type = DataType::TSV;
} else if (comma_cnt > 0) {
type = DataType::CSV;
}
} else {
if (colon_cnt > 0 || colon_cnt2 > 0) {
type = DataType::LIBSVM;
} else if (tab_cnt == tab_cnt2 && tab_cnt > 0) {
type = DataType::TSV;
CHECK(tab_cnt == tab_cnt2);
} else if (comma_cnt == comma_cnt2 && comma_cnt > 0) {
type = DataType::CSV;
CHECK(comma_cnt == comma_cnt2);
}
}
if (type == DataType::INVALID) {
Log::Fatal("Unknown format of training data");
}
std::unique_ptr<Parser> ret;
if (type == DataType::LIBSVM) {
label_idx = GetLabelIdxForLibsvm(line1, num_features, label_idx);
ret.reset(new LibSVMParser(label_idx));
} else if (type == DataType::TSV) {
label_idx = GetLabelIdxForTSV(line1, num_features, label_idx);
ret.reset(new TSVParser(label_idx, tab_cnt + 1));
} else if (type == DataType::CSV) {
label_idx = GetLabelIdxForCSV(line1, num_features, label_idx);
ret.reset(new CSVParser(label_idx, comma_cnt + 1));
}
if (label_idx < 0) {
Log::Info("Data file %s doesn't contain a label column", filename);
}
return ret.release();
}
} // namespace LightGBM