parser.cpp 3.52 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
#include "parser.hpp"

#include <iostream>
#include <fstream>

namespace LightGBM {

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

23
24
25
26
27
28
29
30
31
32
33
34
35
36
bool CheckHasLabelForLibsvm(std::string& str) {
  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_colon == std::string::npos || pos_colon > pos_space) {
    return true;
  } else {
    return false;
  }
}

bool CheckHasLabelForTSV(std::string& str, int num_features) {
  str = Common::Trim(str);
  auto tokens = Common::Split(str.c_str(), '\t');
Guolin Ke's avatar
Guolin Ke committed
37
  if (static_cast<int>(tokens.size()) == num_features) {
38
39
40
41
42
43
44
45
46
    return false;
  } else {
    return true;
  }
}

bool CheckHasLabelForCSV(std::string& str, int num_features) {
  str = Common::Trim(str);
  auto tokens = Common::Split(str.c_str(), ',');
Guolin Ke's avatar
Guolin Ke committed
47
  if (static_cast<int>(tokens.size()) == num_features) {
48
49
50
51
52
53
    return false;
  } else {
    return true;
  }
}

Guolin Ke's avatar
Guolin Ke committed
54
Parser* Parser::CreateParser(const char* filename, bool has_header, int num_features, int label_idx) {
Guolin Ke's avatar
Guolin Ke committed
55
56
57
  std::ifstream tmp_file;
  tmp_file.open(filename);
  if (!tmp_file.is_open()) {
Qiwei Ye's avatar
Qiwei Ye committed
58
    Log::Fatal("Data file: %s doesn't exist", filename);
Guolin Ke's avatar
Guolin Ke committed
59
60
  }
  std::string line1, line2;
Guolin Ke's avatar
Guolin Ke committed
61
62
63
64
65
  if (has_header) {
    if (!tmp_file.eof()) {
      std::getline(tmp_file, line1);
    }
  }
Guolin Ke's avatar
Guolin Ke committed
66
67
68
  if (!tmp_file.eof()) {
    std::getline(tmp_file, line1);
  } else {
Qiwei Ye's avatar
Qiwei Ye committed
69
    Log::Fatal("Data file: %s at least should have one line", filename);
Guolin Ke's avatar
Guolin Ke committed
70
71
72
73
  }
  if (!tmp_file.eof()) {
    std::getline(tmp_file, line2);
  } else {
Qiwei Ye's avatar
Qiwei Ye committed
74
    Log::Error("Data file: %s only have one line", filename);
Guolin Ke's avatar
Guolin Ke committed
75
76
77
78
79
80
81
82
  }
  tmp_file.close();
  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);
83
  Parser* ret = nullptr;
Guolin Ke's avatar
Guolin Ke committed
84
  bool has_label = true;
Guolin Ke's avatar
Guolin Ke committed
85
86
87
  if (line2.size() == 0) {
    // if only have one line on file
    if (colon_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
88
89
      if (num_features > 0) {
        has_label = CheckHasLabelForLibsvm(line1);
90
      }
Guolin Ke's avatar
Guolin Ke committed
91
      ret = new LibSVMParser(has_label ? label_idx : -1);
Guolin Ke's avatar
Guolin Ke committed
92
    } else if (tab_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
93
94
      if (num_features > 0 ) {
        has_label = CheckHasLabelForTSV(line1, num_features);
95
      }
Guolin Ke's avatar
Guolin Ke committed
96
      ret = new TSVParser(has_label ? label_idx : -1);
Guolin Ke's avatar
Guolin Ke committed
97
    } else if (comma_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
98
99
      if (num_features > 0) {
        has_label = CheckHasLabelForCSV(line1, num_features);
100
      }
Guolin Ke's avatar
Guolin Ke committed
101
      ret = new CSVParser(has_label ? label_idx : -1);
102
    } 
Guolin Ke's avatar
Guolin Ke committed
103
104
  } else {
    if (colon_cnt > 0 || colon_cnt2 > 0) {
Guolin Ke's avatar
Guolin Ke committed
105
106
      if (num_features > 0) {
        has_label = CheckHasLabelForLibsvm(line1);
107
      }
Guolin Ke's avatar
Guolin Ke committed
108
      ret = new LibSVMParser(has_label ? label_idx : -1);
Guolin Ke's avatar
Guolin Ke committed
109
110
    }
    else if (tab_cnt == tab_cnt2 && tab_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
111
112
      if (num_features > 0) {
        has_label = CheckHasLabelForTSV(line1, num_features);
113
      }
Guolin Ke's avatar
Guolin Ke committed
114
      ret = new TSVParser(has_label ? label_idx : -1);
Guolin Ke's avatar
Guolin Ke committed
115
    } else if (comma_cnt == comma_cnt2 && comma_cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
116
117
      if (num_features > 0) {
        has_label = CheckHasLabelForCSV(line1, num_features);
118
      }
Guolin Ke's avatar
Guolin Ke committed
119
      ret = new CSVParser(has_label ? label_idx : -1);
Guolin Ke's avatar
Guolin Ke committed
120
121
    }
  }
Guolin Ke's avatar
Guolin Ke committed
122
123
124
  if (!has_label) {
    Log::Info("Data file: %s doesn't contain label column", filename);
  }
125
  return ret;
Guolin Ke's avatar
Guolin Ke committed
126
127
128
}

}  // namespace LightGBM