| 130 | } |
| 131 | |
| 132 | DataType GetDataType(const std::vector<std::string>& lines, int* num_col) { |
| 133 | DataType type = DataType::INVALID; |
| 134 | if (lines.empty()) { |
| 135 | return type; |
| 136 | } |
| 137 | int comma_cnt = 0; |
| 138 | int tab_cnt = 0; |
| 139 | int colon_cnt = 0; |
| 140 | GetStatistic(lines[0].c_str(), &comma_cnt, &tab_cnt, &colon_cnt); |
| 141 | if (lines.size() == 1) { |
| 142 | if (colon_cnt > 0) { |
| 143 | type = DataType::LIBSVM; |
| 144 | } else if (tab_cnt > 0) { |
| 145 | type = DataType::TSV; |
| 146 | } else if (comma_cnt > 0) { |
| 147 | type = DataType::CSV; |
| 148 | } |
| 149 | } else if (lines.size() > 1) { |
| 150 | int comma_cnt2 = 0; |
| 151 | int tab_cnt2 = 0; |
| 152 | int colon_cnt2 = 0; |
| 153 | GetStatistic(lines[1].c_str(), &comma_cnt2, &tab_cnt2, &colon_cnt2); |
| 154 | if (colon_cnt > 0 || colon_cnt2 > 0) { |
| 155 | type = DataType::LIBSVM; |
| 156 | } else if (tab_cnt == tab_cnt2 && tab_cnt > 0) { |
| 157 | type = DataType::TSV; |
| 158 | } else if (comma_cnt == comma_cnt2 && comma_cnt > 0) { |
| 159 | type = DataType::CSV; |
| 160 | } |
| 161 | if (type == DataType::TSV || type == DataType::CSV) { |
| 162 | // valid the type |
| 163 | for (size_t i = 2; i < lines.size(); ++i) { |
| 164 | GetStatistic(lines[i].c_str(), &comma_cnt2, &tab_cnt2, &colon_cnt2); |
| 165 | if (type == DataType::TSV && tab_cnt2 != tab_cnt) { |
| 166 | type = DataType::INVALID; |
| 167 | break; |
| 168 | } else if (type == DataType::CSV && comma_cnt != comma_cnt2) { |
| 169 | type = DataType::INVALID; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | if (type == DataType::LIBSVM) { |
| 176 | int max_col_idx = 0; |
| 177 | for (size_t i = 0; i < lines.size(); ++i) { |
| 178 | auto str = Common::Trim(lines[i]); |
| 179 | auto colon_pos = str.find_last_of(":"); |
| 180 | auto space_pos = str.find_last_of(" \f\t\v"); |
| 181 | auto sub_str = str.substr(space_pos + 1, space_pos - colon_pos - 1); |
| 182 | int cur_idx = 0; |
| 183 | Common::Atoi(sub_str.c_str(), &cur_idx); |
| 184 | max_col_idx = std::max(cur_idx, max_col_idx); |
| 185 | } |
| 186 | *num_col = max_col_idx + 1; |
| 187 | } else if (type == DataType::CSV) { |
| 188 | *num_col = comma_cnt + 1; |
| 189 | } else if (type == DataType::TSV) { |
no test coverage detected