1#ifndef LIGHTGBM_IO_PARSER_HPP_
2#define LIGHTGBM_IO_PARSER_HPP_
3#include <LightGBM/dataset.h>
5#include <LightGBM/utils/common.h>
6#include <LightGBM/utils/log.h>
9#include <unordered_map>
17 explicit CSVParser(
int label_idx,
int total_columns)
18 :label_idx_(label_idx), total_columns_(total_columns) {
21 std::vector<std::pair<int, double>>* out_features,
double* out_label)
const override {
26 while (*str !=
'\0') {
27 str = Common::Atof(str, &val);
28 if (idx == label_idx_) {
32 else if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
33 out_features->emplace_back(idx + bias, val);
38 }
else if (*str !=
'\0') {
39 Log::Fatal(
"Input format error when parsing as CSV");
44 inline int TotalColumns()
const override {
45 return total_columns_;
50 int total_columns_ = -1;
55 explicit TSVParser(
int label_idx,
int total_columns)
56 :label_idx_(label_idx), total_columns_(total_columns) {
59 std::vector<std::pair<int, double>>* out_features,
double* out_label)
const override {
63 while (*str !=
'\0') {
64 str = Common::Atof(str, &val);
65 if (idx == label_idx_) {
68 }
else if (std::fabs(val) > kZeroThreshold || std::isnan(val)) {
69 out_features->emplace_back(idx + bias, val);
74 }
else if (*str !=
'\0') {
75 Log::Fatal(
"Input format error when parsing as TSV");
80 inline int TotalColumns()
const override {
81 return total_columns_;
86 int total_columns_ = -1;
92 :label_idx_(label_idx) {
94 Log::Fatal(
"Label should be the first column in a LibSVM file");
98 std::vector<std::pair<int, double>>* out_features,
double* out_label)
const override {
101 if (label_idx_ == 0) {
102 str = Common::Atof(str, &val);
104 str = Common::SkipSpaceAndTab(str);
106 while (*str !=
'\0') {
107 str = Common::Atoi(str, &idx);
108 str = Common::SkipSpaceAndTab(str);
111 str = Common::Atof(str, &val);
112 out_features->emplace_back(idx, val);
114 Log::Fatal(
"Input format error when parsing as LibSVM");
116 str = Common::SkipSpaceAndTab(str);
120 inline int TotalColumns()
const override {
void ParseOneLine(const char *str, std::vector< std::pair< int, double > > *out_features, double *out_label) const override
Parse one line with label.
Definition parser.hpp:20
void ParseOneLine(const char *str, std::vector< std::pair< int, double > > *out_features, double *out_label) const override
Parse one line with label.
Definition parser.hpp:97
Interface for Parser.
Definition dataset.h:249
void ParseOneLine(const char *str, std::vector< std::pair< int, double > > *out_features, double *out_label) const override
Parse one line with label.
Definition parser.hpp:58
desc and descl2 fields must be written in reStructuredText format
Definition application.h:10