-
Notifications
You must be signed in to change notification settings - Fork 0
/
executive.cpp
143 lines (122 loc) · 3.05 KB
/
executive.cpp
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
// Author: Kristi Daigh
// Project: MLEM2 Rule Induction
// Date: 11/20/2019
/** Source file for executive class.
@file executive.cpp
This class handles the execution
of the program. */
#include "algorithm.hpp"
#include "executive.hpp"
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
Executive::Executive(){
m_numAttributes = 0;
}
Executive::~Executive(){
delete m_data;
}
bool Executive::parseInFile(string filename) {
fstream file;
file.open(filename);
// Unable to open file; Signal with boolean
if(!file) {
return false;
}
parseFormat(file);
m_data = new Dataset(m_numAttributes + 1);
parseHeader(file);
string word;
unsigned col = 0;
while(file >> word){
// IF: col is at last column, reset col
if(col == m_numAttributes + 1){
col = 0;
}
size_t pos = word.find('!');
if(pos != string::npos){
if(pos != 0){
word = word.erase(pos);
} else {
word = "";
}
string comment;
getline(file, comment, '\n');
}
if(!word.empty()){
m_data->addValue(col, word);
col++;
}
}
file.close();
return true;
}
bool Executive::generateOutFile(string filename){
ofstream file;
file.open(filename);
// Unable to open file; Signal with boolean
if(!file) {
return false;
}
Algorithm mlem2(m_numAttributes);
mlem2.generateRuleset(file, m_data);
file.close();
return true;
}
void Executive::parseFormat(istream& file){
if(file.eof()){
cerr << "No format found." << endl;
return;
}
string line = removeComments(file);
size_t beg = line.find('<');
size_t end = line.find('>');
if(beg != string::npos && end != string::npos){
line = line.substr(beg + 1, end);
}
for(char& c : line) {
if(c == 'a'){
m_numAttributes++;
}
}
}
void Executive::parseHeader(istream& file){
if(file.eof()){
cerr << "No header found." << endl;
return;
}
string line = removeComments(file);
size_t beg = line.find('[');
size_t end = line.find(']');
if(beg != string::npos && end != string::npos){
line = line.substr(beg + 1, end);
}
string word;
istringstream buffer(line);
for(unsigned i = 0; i < m_numAttributes; i++){
buffer >> word;
m_data->addAttribute(word);
}
buffer >> word;
m_data->setDecision(word);
}
string Executive::removeComments(istream & file){
string line;
getline(file, line);
size_t pos = line.find('!');
// WHILE: Line contains comment
while(pos != string::npos){
// IF: Entire line is comment
if(line.find('!') == 0){
getline(file, line);
pos = line.find('!');
}
// ELSE: Comment is at the end of line
else {
line.erase('!');
pos = string::npos;
}
}
return line;
}