BayesNet/lib/Files/ArffFiles.cc

170 lines
4.1 KiB
C++
Raw Permalink Normal View History

2023-06-29 20:00:41 +00:00
#include "ArffFiles.h"
#include <fstream>
#include <sstream>
#include <map>
2023-07-27 13:49:58 +00:00
#include <iostream>
2023-06-29 20:00:41 +00:00
using namespace std;
ArffFiles::ArffFiles() = default;
vector<string> ArffFiles::getLines() const
{
return lines;
}
unsigned long int ArffFiles::getSize() const
{
return lines.size();
}
vector<pair<string, string>> ArffFiles::getAttributes() const
{
return attributes;
}
string ArffFiles::getClassName() const
{
return className;
}
string ArffFiles::getClassType() const
{
return classType;
}
vector<vector<float>>& ArffFiles::getX()
{
return X;
}
vector<int>& ArffFiles::getY()
{
return y;
}
2023-07-26 23:56:06 +00:00
void ArffFiles::loadCommon(string fileName)
2023-06-29 20:00:41 +00:00
{
ifstream file(fileName);
if (!file.is_open()) {
throw invalid_argument("Unable to open file");
}
string line;
string keyword;
string attribute;
string type;
string type_w;
while (getline(file, line)) {
if (line.empty() || line[0] == '%' || line == "\r" || line == " ") {
continue;
}
if (line.find("@attribute") != string::npos || line.find("@ATTRIBUTE") != string::npos) {
stringstream ss(line);
ss >> keyword >> attribute;
type = "";
while (ss >> type_w)
type += type_w + " ";
attributes.emplace_back(trim(attribute), trim(type));
2023-06-29 20:00:41 +00:00
continue;
}
if (line[0] == '@') {
continue;
}
lines.push_back(line);
}
file.close();
if (attributes.empty())
throw invalid_argument("No attributes found");
2023-07-26 23:56:06 +00:00
}
void ArffFiles::load(const string& fileName, bool classLast)
{
int labelIndex;
loadCommon(fileName);
2023-06-29 20:00:41 +00:00
if (classLast) {
className = get<0>(attributes.back());
classType = get<1>(attributes.back());
attributes.pop_back();
2023-07-26 23:56:06 +00:00
labelIndex = static_cast<int>(attributes.size());
2023-06-29 20:00:41 +00:00
} else {
className = get<0>(attributes.front());
classType = get<1>(attributes.front());
attributes.erase(attributes.begin());
2023-07-26 23:56:06 +00:00
labelIndex = 0;
2023-06-29 20:00:41 +00:00
}
2023-07-26 23:56:06 +00:00
generateDataset(labelIndex);
}
void ArffFiles::load(const string& fileName, const string& name)
{
int labelIndex;
loadCommon(fileName);
bool found = false;
for (int i = 0; i < attributes.size(); ++i) {
if (attributes[i].first == name) {
className = get<0>(attributes[i]);
classType = get<1>(attributes[i]);
attributes.erase(attributes.begin() + i);
labelIndex = i;
found = true;
break;
}
}
if (!found) {
throw invalid_argument("Class name not found");
}
generateDataset(labelIndex);
2023-06-29 20:00:41 +00:00
}
2023-07-26 23:56:06 +00:00
void ArffFiles::generateDataset(int labelIndex)
2023-06-29 20:00:41 +00:00
{
X = vector<vector<float>>(attributes.size(), vector<float>(lines.size()));
auto yy = vector<string>(lines.size(), "");
2023-07-27 13:49:58 +00:00
auto removeLines = vector<int>(); // Lines with missing values
2023-06-29 20:00:41 +00:00
for (size_t i = 0; i < lines.size(); i++) {
stringstream ss(lines[i]);
string value;
int pos = 0;
int xIndex = 0;
while (getline(ss, value, ',')) {
if (pos++ == labelIndex) {
yy[i] = value;
} else {
2023-07-27 13:49:58 +00:00
if (value == "?") {
X[xIndex++][i] = -1;
removeLines.push_back(i);
} else
X[xIndex++][i] = stof(value);
2023-06-29 20:00:41 +00:00
}
}
}
2023-07-27 13:49:58 +00:00
for (auto i : removeLines) {
yy.erase(yy.begin() + i);
for (auto& x : X) {
x.erase(x.begin() + i);
}
}
2023-06-29 20:00:41 +00:00
y = factorize(yy);
}
string ArffFiles::trim(const string& source)
{
string s(source);
s.erase(0, s.find_first_not_of(" '\n\r\t"));
s.erase(s.find_last_not_of(" '\n\r\t") + 1);
2023-06-29 20:00:41 +00:00
return s;
}
vector<int> ArffFiles::factorize(const vector<string>& labels_t)
{
vector<int> yy;
yy.reserve(labels_t.size());
map<string, int> labelMap;
int i = 0;
for (const string& label : labels_t) {
if (labelMap.find(label) == labelMap.end()) {
labelMap[label] = i++;
}
yy.push_back(labelMap[label]);
}
return yy;
}