add oneR to main #2
@ -1,5 +1,6 @@
|
|||||||
add_executable(arff
|
add_executable(arff
|
||||||
main.cpp
|
main.cpp
|
||||||
arff.cpp
|
arff.cpp
|
||||||
|
log.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
#include "arff.hpp"
|
#include "arff.hpp"
|
||||||
|
#include "log.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace ARFF {
|
namespace ARFF {
|
||||||
bool isVerbose = false;
|
|
||||||
|
|
||||||
void ParseArguments(int argc, char* argv[]) {
|
void ParseArguments(int argc, char* argv[]) {
|
||||||
std::string argument_string;
|
std::string argument_string;
|
||||||
for (int i = 0; i < argc; ++i) {
|
for (int i = 0; i < argc; ++i) {
|
||||||
argument_string.assign(argv[i]);
|
argument_string.assign(argv[i]);
|
||||||
if (argument_string == "-v" || argument_string == "--verbose") {
|
if (argument_string == "-v" || argument_string == "--verbose") {
|
||||||
isVerbose = true;
|
debug::verbose = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -22,25 +21,13 @@ namespace ARFF {
|
|||||||
std::cout << "Please enter name of the data file:\t";
|
std::cout << "Please enter name of the data file:\t";
|
||||||
std::cin >> filename;
|
std::cin >> filename;
|
||||||
if (filename.empty()) {
|
if (filename.empty()) {
|
||||||
LogError("ARFF/Setup", "No data filename provided, exiting...");
|
debug::Log(kError, "No data filename provided, exiting...");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogInfo(const std::string location, const std::string message) {
|
|
||||||
if (!isVerbose) { return; }
|
|
||||||
std::cout << '[' << location << " - INFO] ";
|
|
||||||
std::cout << message << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogError(const std::string location, const std::string message) {
|
|
||||||
if (!isVerbose) { return; }
|
|
||||||
std::cerr << '[' << location << " - ERROR] ";
|
|
||||||
std::cerr << message << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
AttributeType::AttributeType(std::string attribute) {
|
AttributeType::AttributeType(std::string attribute) {
|
||||||
this->attribute = attribute;
|
this->attribute = attribute;
|
||||||
}
|
}
|
||||||
@ -58,7 +45,7 @@ namespace ARFF {
|
|||||||
void Arff::Read(std::string filename) {
|
void Arff::Read(std::string filename) {
|
||||||
std::ifstream dataFile(filename);
|
std::ifstream dataFile(filename);
|
||||||
if (!dataFile.is_open()) {
|
if (!dataFile.is_open()) {
|
||||||
LogError("ARFF/Read", "Unable to open file with name `"
|
debug::Log(kError, "Unable to open file with name `"
|
||||||
+ filename + ", exiting...");
|
+ filename + ", exiting...");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -130,12 +117,12 @@ namespace ARFF {
|
|||||||
if (token == "@relation" || token == "@RELATION") {
|
if (token == "@relation" || token == "@RELATION") {
|
||||||
parser >> token;
|
parser >> token;
|
||||||
relation = token;
|
relation = token;
|
||||||
LogInfo("ARFF/Attribute", "Relation set: " + relation);
|
debug::Log(kLog, "Relation set: " + relation);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parser >> token;
|
parser >> token;
|
||||||
attributeList.emplace_back(token);
|
attributeList.emplace_back(token);
|
||||||
LogInfo("ARFF/Attribute", "Added attribute: " + token);
|
debug::Log(kLog, "Added attribute: " + token);
|
||||||
while (std::getline(parser, token, ',')) {
|
while (std::getline(parser, token, ',')) {
|
||||||
// Clean token from outside pieces
|
// Clean token from outside pieces
|
||||||
token.erase(std::remove(token.begin(), token.end(), ' '), token.end());
|
token.erase(std::remove(token.begin(), token.end(), ' '), token.end());
|
||||||
@ -146,7 +133,7 @@ namespace ARFF {
|
|||||||
token.erase(std::remove(token.begin(), token.end(), '\r'), token.end());
|
token.erase(std::remove(token.begin(), token.end(), '\r'), token.end());
|
||||||
token.erase(std::remove(token.begin(), token.end(), '\n'), token.end());
|
token.erase(std::remove(token.begin(), token.end(), '\n'), token.end());
|
||||||
attributeList.back().AddValue(token);
|
attributeList.back().AddValue(token);
|
||||||
LogInfo("ARFF/Attribute", "Added value: " + token);
|
debug::Log(kLog, "Added value: " + token);
|
||||||
}
|
}
|
||||||
// Additional missing value case
|
// Additional missing value case
|
||||||
attributeList.back().AddValue("?");
|
attributeList.back().AddValue("?");
|
||||||
@ -159,14 +146,14 @@ namespace ARFF {
|
|||||||
int id = 0;
|
int id = 0;
|
||||||
if (!database.empty()) { id = database.back().id + 1; }
|
if (!database.empty()) { id = database.back().id + 1; }
|
||||||
database.emplace_back(id, attributeList.size());
|
database.emplace_back(id, attributeList.size());
|
||||||
LogInfo("ARFF/Data", "Added id: " + std::to_string(database.back().id));
|
debug::Log(kLog, "Added id: " + std::to_string(database.back().id));
|
||||||
for (int i = 0; i < attributeList.size(); ++i) {
|
for (int i = 0; i < attributeList.size(); ++i) {
|
||||||
std::getline(parser, token, ',');
|
std::getline(parser, token, ',');
|
||||||
token.erase(std::remove(token.begin(), token.end(), ' '), token.end());
|
token.erase(std::remove(token.begin(), token.end(), ' '), token.end());
|
||||||
token.erase(std::remove(token.begin(), token.end(), '\r'), token.end());
|
token.erase(std::remove(token.begin(), token.end(), '\r'), token.end());
|
||||||
token.erase(std::remove(token.begin(), token.end(), '\n'), token.end());
|
token.erase(std::remove(token.begin(), token.end(), '\n'), token.end());
|
||||||
database.back().values.at(i) = token;
|
database.back().values.at(i) = token;
|
||||||
LogInfo("ARFF/Data", "Added instance value: " + token);
|
debug::Log(kLog, "Added instance value: " + token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,25 +161,25 @@ namespace ARFF {
|
|||||||
for (Instance instance : database) {
|
for (Instance instance : database) {
|
||||||
int successCheck = 0;
|
int successCheck = 0;
|
||||||
for (int i = 0; i < attributeList.size(); ++i) {
|
for (int i = 0; i < attributeList.size(); ++i) {
|
||||||
LogInfo("ARFF/Integrity", "Instance value tested: '"
|
debug::Log(kTrace, "Instance value tested: '"
|
||||||
+ instance.values.at(i) + "'");
|
+ instance.values.at(i) + "'");
|
||||||
for (std::string value : attributeList.at(i).values) {
|
for (std::string value : attributeList.at(i).values) {
|
||||||
LogInfo("ARFF/Integrity", "attributeList value: '"
|
debug::Log(kTrace, "attributeList value: '"
|
||||||
+ value + "'");
|
+ value + "'");
|
||||||
if (instance.values.at(i) == value) {
|
if (instance.values.at(i) == value) {
|
||||||
LogInfo("ARFF/Integrity", "Value found: " + value);
|
debug::Log(kTrace, "Value found: " + value);
|
||||||
++successCheck;
|
++successCheck;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (successCheck != attributeList.size()) {
|
if (successCheck != attributeList.size()) {
|
||||||
LogError("ARFF/Integrity", "Value size mismatch: "
|
debug::Log(kError, "Value size mismatch: "
|
||||||
+ std::to_string(successCheck) + " out of "
|
+ std::to_string(successCheck) + " out of "
|
||||||
+ std::to_string(attributeList.size()));
|
+ std::to_string(attributeList.size()));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LogInfo("ARFF/Integrity", "All values exist, continuing...");
|
debug::Log(kLog, "All values exist, continuing...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
namespace ARFF {
|
namespace ARFF {
|
||||||
void ParseArguments(int argc, char* argv[]);
|
void ParseArguments(int argc, char* argv[]);
|
||||||
std::string GetDataFilename(void);
|
std::string GetDataFilename(void);
|
||||||
void LogInfo(const std::string location, const std::string message);
|
|
||||||
void LogError(const std::string location, const std::string message);
|
|
||||||
|
|
||||||
struct AttributeType {
|
struct AttributeType {
|
||||||
public:
|
public:
|
||||||
|
19
src/arff/log.cpp
Normal file
19
src/arff/log.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include "log.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace debug {
|
||||||
|
bool verbose = false;
|
||||||
|
|
||||||
|
void Log(LogLevel level, std::string message) {
|
||||||
|
std::string logMessage = "";
|
||||||
|
if (!verbose && level > kNone) { return; }
|
||||||
|
switch (level) {
|
||||||
|
case kLog: logMessage += "[LOG] "; break;
|
||||||
|
case kWarn: logMessage += "[WARN] "; break;
|
||||||
|
case kError: logMessage += "[ERROR] "; break;
|
||||||
|
case kTrace: logMessage += "[TRACE] "; break;
|
||||||
|
}
|
||||||
|
logMessage += message;
|
||||||
|
std::cout << logMessage << std::endl;
|
||||||
|
}
|
||||||
|
}
|
19
src/arff/log.hpp
Normal file
19
src/arff/log.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef LOG_HPP
|
||||||
|
#define LOG_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
enum LogLevel {
|
||||||
|
kNone = -1,
|
||||||
|
kLog = 0,
|
||||||
|
kWarn,
|
||||||
|
kError,
|
||||||
|
kTrace
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace debug {
|
||||||
|
extern bool verbose;
|
||||||
|
void Log(LogLevel level, std::string message);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -2,5 +2,6 @@ include_directories(../arff)
|
|||||||
add_executable(onerule
|
add_executable(onerule
|
||||||
main.cpp
|
main.cpp
|
||||||
../arff/arff.cpp
|
../arff/arff.cpp
|
||||||
|
../arff/log.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -4,10 +4,12 @@
|
|||||||
* Description: Read and store ARFF data from a file
|
* Description: Read and store ARFF data from a file
|
||||||
*/
|
*/
|
||||||
#include "arff.hpp"
|
#include "arff.hpp"
|
||||||
|
#include "log.hpp"
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
ARFF::ParseArguments(argc, argv);
|
ARFF::ParseArguments(argc, argv);
|
||||||
ARFF::Arff data;
|
ARFF::Arff data;
|
||||||
data.Read(ARFF::GetDataFilename());
|
data.Read(ARFF::GetDataFilename());
|
||||||
data.PrintOverview();
|
data.PrintOverview();
|
||||||
|
debug::Log(kLog, "Test");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user