-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharff_parser.cpp
More file actions
executable file
·187 lines (178 loc) · 5.79 KB
/
Copy patharff_parser.cpp
File metadata and controls
executable file
·187 lines (178 loc) · 5.79 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "arff_parser.h"
#include "arff_token.h"
#include <iostream>
ArffParser::ArffParser(const std::string& _file) : m_lexer(NULL),
m_parsed(false),
m_data(NULL) {
m_lexer = new ArffLexer(_file);
}
ArffParser::~ArffParser() {
if(m_lexer != NULL) {
delete m_lexer;
m_lexer = NULL;
}
if(m_data != NULL) {
delete m_data;
m_data = NULL;
}
}
//ArffData* ArffParser::parse()
void ArffParser::parse(List<Point>* datalist)
{
/*if((ArffData*)NULL != m_data) {
return m_data;
}*/
//List<Point> pData;
m_data = new ArffData();
_read_relation();
_read_attrs();
_read_instances(datalist);
m_parsed = true;
//return m_data;
//return pData;
}
void ArffParser::_read_relation() {
// @relation
ArffToken tok1 = m_lexer->next_token();
if(tok1.token_enum() != RELATION) {
THROW("%s: First token must be of 'RELATION'! It is '%s'",
"ArffParser::_read_relation",
arff_token2str(tok1.token_enum()).c_str());
}
// name
ArffToken tok2 = m_lexer->next_token();
if(tok2.token_enum() != VALUE_TOKEN) {
THROW("%s: RELATION token must be followed by %s! It is '%s'",
"ArffParser::_read_relation", "VALUE_TOKEN",
arff_token2str(tok2.token_enum()).c_str());
}
m_data->set_relation_name(tok2.token_str());
}
void ArffParser::_read_attrs() {
while(true) {
// @attribute (or @data)
ArffToken tok = m_lexer->next_token();
ArffTokenEnum type = tok.token_enum();
if((type == DATA_TOKEN) || (type == END_OF_FILE)) {
break;
}
if(type != ATTRIBUTE) {
THROW("%s: First token must be of 'ATTRIBUTE'! It is '%s'",
"ArffParser::_read_attrs", arff_token2str(type).c_str());
}
_read_attr();
}
}
void ArffParser::_read_attr() {
// name
ArffToken name = m_lexer->next_token();
if(name.token_enum() != VALUE_TOKEN) {
THROW("%s: 'ATTRIBUTE' must be followed by a '%s'! It is '%s'",
"ArffParser::_read_attr", "VALUE_TOKEN",
arff_token2str(name.token_enum()).c_str());
}
// type
ArffToken type = m_lexer->next_token();
ArffTokenEnum ate = type.token_enum();
ArffValueEnum ave = UNKNOWN_VAL;
switch(ate) {
case NUMERIC_TOKEN:
ave = NUMERIC;
break;
case STRING_TOKEN:
ave = STRING;
break;
case DATE_TOKEN:
ave = DATE;
break;
case BRKT_OPEN:
ave = NOMINAL;
break;
default:
THROW("%s: Bad attribute type for name=%s attr-type=%s!",
"ArffParser::_read_attr", name.token_str().c_str(),
arff_token2str(ate).c_str());
}
m_data->add_attr(new ArffAttr(name.token_str(), ave));
///@todo: get the date-format
if(ave != NOMINAL) {
return;
}
// nominal type
while(true) {
ArffToken tok = m_lexer->next_token();
if(tok.token_enum() == VALUE_TOKEN) {
m_data->add_nominal_val(name.token_str(), tok.token_str());
}
else if(tok.token_enum() == BRKT_CLOSE) {
break;
}
else {
THROW("%s: For nominal values expecting '%s' got '%s' token!",
"ArffParser::_read_attr", "VALUE_TOKEN",
arff_token2str(tok.token_enum()).c_str());
}
}
}
//void ArffParser::_read_instances() {
void ArffParser::_read_instances(List<Point>* datalist) {
bool end_of_file = false;
//List<Point> pData;
int val;
float f;
double d;
int ss = 0;
long tt1, tt2;
int32 num = m_data->num_attributes();
//tt1 = getCurrentRSS();
// while(!end_of_file && (++ss < 100)) {
while(!end_of_file){
/*tt2 = getCurrentRSS();
if(tt2-tt1 > 0) std::cout << "Difference\t" << tt2 - tt1 << " bytes" << std::endl;
tt1 = tt2;*/
Point p;
//ArffInstance* inst = new ArffInstance();
ArffInstance* inst;
for(int32 i=0;i<num;++i) {
ArffToken tok = m_lexer->next_token();
ArffTokenEnum type = tok.token_enum();
ArffValueEnum aType = m_data->get_attr(i)->type();
if(type == END_OF_FILE) {
end_of_file = true;
break;
}
if((type != VALUE_TOKEN) && (type != MISSING_TOKEN)) {
THROW("%s expects '%s' or '%s', it is '%s'!",
"ArffParser::_read_instances", "VALUE_TOKEN",
"MISSING_TOKEN", arff_token2str(type).c_str());
}
if(type == MISSING_TOKEN) {
//inst->add(new ArffValue(aType));
std::cout << "Missing value found in input file"<< std::endl;
}
else if(aType == INTEGER) {
//inst->add(new ArffValue(tok.token_str(), true));
p.InsertInteger(tok.token_int32());
}
else if(aType == NUMERIC) {
//inst->add(new ArffValue(tok.token_str(), true));
p.InsertFloat(tok.token_float());
}
else if((aType == STRING) || (aType == NOMINAL)) {
//inst->add(new ArffValue(tok.token_str(), false));
p.InsertString(tok.token_str().c_str());
}
else { // has to be DATE type!
//inst->add(new ArffValue(tok.token_str(), false, true));
std::cout << "Unknown/Date type record found in input file" << std::endl;
}
}
if(!end_of_file) {
//m_data->add_instance(inst);
datalist->AddEle(p);
}
}
//std::cout << "Current RSS: " << getCurrentRSS() << " bytes" << std::endl;
//std::cout << "Peak RSS: " << getPeakRSS() << " bytes" << std::endl;
//return pData;
}