parser.cpp 4.53 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
#include "parser.hpp"

3
#include <string>
Guolin Ke's avatar
Guolin Ke committed
4
#include <fstream>
Guolin Ke's avatar
Guolin Ke committed
5
#include <functional>
6
#include <iostream>
Guolin Ke's avatar
Guolin Ke committed
7
#include <memory>
Guolin Ke's avatar
Guolin Ke committed
8
9
10

namespace LightGBM {

Guolin Ke's avatar
Guolin Ke committed
11
void GetStatistic(const char* str, int* comma_cnt, int* tab_cnt, int* colon_cnt) {
Guolin Ke's avatar
Guolin Ke committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  *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);
    }
  }
}

Guolin Ke's avatar
Guolin Ke committed
26
27
28
29
int GetLabelIdxForLibsvm(std::string& str, int num_features, int label_idx) {
  if (num_features <= 0) {
    return label_idx;
  }
30
31
32
  str = Common::Trim(str);
  auto pos_space = str.find_first_of(" \f\n\r\t\v");
  auto pos_colon = str.find_first_of(":");
33
  if (pos_space == std::string::npos || pos_space < pos_colon) {
Guolin Ke's avatar
Guolin Ke committed
34
    return label_idx;
35
36
  } else {
    return -1;
37
38
39
  }
}

Guolin Ke's avatar
Guolin Ke committed
40
41
42
43
int GetLabelIdxForTSV(std::string& str, int num_features, int label_idx) {
  if (num_features <= 0) {
    return label_idx;
  }
44
45
  str = Common::Trim(str);
  auto tokens = Common::Split(str.c_str(), '\t');
Guolin Ke's avatar
Guolin Ke committed
46
  if (static_cast<int>(tokens.size()) == num_features) {
Guolin Ke's avatar
Guolin Ke committed
47
    return -1;
48
  } else {
Guolin Ke's avatar
Guolin Ke committed
49
    return label_idx;
50
51
52
  }
}

Guolin Ke's avatar
Guolin Ke committed
53
54
55
56
int GetLabelIdxForCSV(std::string& str, int num_features, int label_idx) {
  if (num_features <= 0) {
    return label_idx;
  }
57
58
  str = Common::Trim(str);
  auto tokens = Common::Split(str.c_str(), ',');
Guolin Ke's avatar
Guolin Ke committed
59
  if (static_cast<int>(tokens.size()) == num_features) {
Guolin Ke's avatar
Guolin Ke committed
60
    return -1;
61
  } else {
Guolin Ke's avatar
Guolin Ke committed
62
    return label_idx;
63
64
65
  }
}

Guolin Ke's avatar
Guolin Ke committed
66
67
68
69
70
71
72
enum DataType {
  INVALID,
  CSV,
  TSV,
  LIBSVM
};

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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;
  }
}

Guolin Ke's avatar
Guolin Ke committed
88
Parser* Parser::CreateParser(const char* filename, bool header, int num_features, int label_idx) {
89
90
  auto reader = VirtualFileReader::Make(filename);
  if (!reader->Init()) {
91
    Log::Fatal("Data file %s doesn't exist", filename);
Guolin Ke's avatar
Guolin Ke committed
92
93
  }
  std::string line1, line2;
94
95
96
97
98
99
100
101
  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));
Guolin Ke's avatar
Guolin Ke committed
102
  if (header) {
Guolin Ke's avatar
Guolin Ke committed
103
    if (!tmp_file.eof()) {
104
      getline(tmp_file, line1, reader.get(), buffer, buffer_size);
Guolin Ke's avatar
Guolin Ke committed
105
106
    }
  }
Guolin Ke's avatar
Guolin Ke committed
107
  if (!tmp_file.eof()) {
108
    getline(tmp_file, line1, reader.get(), buffer, buffer_size);
Guolin Ke's avatar
Guolin Ke committed
109
  } else {
110
    Log::Fatal("Data file %s should have at least one line", filename);
Guolin Ke's avatar
Guolin Ke committed
111
112
  }
  if (!tmp_file.eof()) {
113
    getline(tmp_file, line2, reader.get(), buffer, buffer_size);
Guolin Ke's avatar
Guolin Ke committed
114
  } else {
115
    Log::Warning("Data file %s only has one line", filename);
Guolin Ke's avatar
Guolin Ke committed
116
117
118
119
120
121
122
  }
  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);
123
124


Guolin Ke's avatar
Guolin Ke committed
125
126

  DataType type = DataType::INVALID;
Guolin Ke's avatar
Guolin Ke committed
127
128
129
  if (line2.size() == 0) {
    // if only have one line on file
    if (colon_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
130
      type = DataType::LIBSVM;
Guolin Ke's avatar
Guolin Ke committed
131
    } else if (tab_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
132
      type = DataType::TSV;
Guolin Ke's avatar
Guolin Ke committed
133
    } else if (comma_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
134
135
      type = DataType::CSV;
    }
Guolin Ke's avatar
Guolin Ke committed
136
137
  } else {
    if (colon_cnt > 0 || colon_cnt2 > 0) {
Guolin Ke's avatar
Guolin Ke committed
138
139
140
      type = DataType::LIBSVM;
    } else if (tab_cnt == tab_cnt2 && tab_cnt > 0) {
      type = DataType::TSV;
Guolin Ke's avatar
Guolin Ke committed
141
      CHECK(tab_cnt == tab_cnt2);
Guolin Ke's avatar
Guolin Ke committed
142
    } else if (comma_cnt == comma_cnt2 && comma_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
143
      type = DataType::CSV;
Guolin Ke's avatar
Guolin Ke committed
144
      CHECK(comma_cnt == comma_cnt2);
Guolin Ke's avatar
Guolin Ke committed
145
146
    }
  }
Guolin Ke's avatar
Guolin Ke committed
147
  if (type == DataType::INVALID) {
148
    Log::Fatal("Unknown format of training data");
Guolin Ke's avatar
Guolin Ke committed
149
  }
Guolin Ke's avatar
Guolin Ke committed
150
  std::unique_ptr<Parser> ret;
Guolin Ke's avatar
Guolin Ke committed
151
152
  if (type == DataType::LIBSVM) {
    label_idx = GetLabelIdxForLibsvm(line1, num_features, label_idx);
Guolin Ke's avatar
Guolin Ke committed
153
    ret.reset(new LibSVMParser(label_idx));
154
  } else if (type == DataType::TSV) {
Guolin Ke's avatar
Guolin Ke committed
155
    label_idx = GetLabelIdxForTSV(line1, num_features, label_idx);
Guolin Ke's avatar
Guolin Ke committed
156
    ret.reset(new TSVParser(label_idx, tab_cnt + 1));
157
  } else if (type == DataType::CSV) {
Guolin Ke's avatar
Guolin Ke committed
158
    label_idx = GetLabelIdxForCSV(line1, num_features, label_idx);
Guolin Ke's avatar
Guolin Ke committed
159
    ret.reset(new CSVParser(label_idx, comma_cnt + 1));
Guolin Ke's avatar
Guolin Ke committed
160
161
162
  }

  if (label_idx < 0) {
163
    Log::Info("Data file %s doesn't contain a label column", filename);
Guolin Ke's avatar
Guolin Ke committed
164
  }
Guolin Ke's avatar
Guolin Ke committed
165
  return ret.release();
Guolin Ke's avatar
Guolin Ke committed
166
167
168
}

}  // namespace LightGBM