BayesNet/lib/Files/ArffFiles.cc

168 lines
4.3 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
ArffFiles::ArffFiles() = default;
2023-11-08 17:45:35 +00:00
std::vector<std::string> ArffFiles::getLines() const
2023-06-29 20:00:41 +00:00
{
return lines;
}
unsigned long int ArffFiles::getSize() const
{
return lines.size();
}
2023-11-08 17:45:35 +00:00
std::vector<std::pair<std::string, std::string>> ArffFiles::getAttributes() const
2023-06-29 20:00:41 +00:00
{
return attributes;
}
2023-11-08 17:45:35 +00:00
std::string ArffFiles::getClassName() const
2023-06-29 20:00:41 +00:00
{
return className;
}
2023-11-08 17:45:35 +00:00
std::string ArffFiles::getClassType() const
2023-06-29 20:00:41 +00:00
{
return classType;
}
2023-11-08 17:45:35 +00:00
std::vector<std::vector<float>>& ArffFiles::getX()
2023-06-29 20:00:41 +00:00
{
return X;
}
2023-11-08 17:45:35 +00:00
std::vector<int>& ArffFiles::getY()
2023-06-29 20:00:41 +00:00
{
return y;
}
2023-11-08 17:45:35 +00:00
void ArffFiles::loadCommon(std::string fileName)
2023-06-29 20:00:41 +00:00
{
2023-11-08 17:45:35 +00:00
std::ifstream file(fileName);
2023-06-29 20:00:41 +00:00
if (!file.is_open()) {
2023-11-08 17:45:35 +00:00
throw std::invalid_argument("Unable to open file");
2023-06-29 20:00:41 +00:00
}
2023-11-08 17:45:35 +00:00
std::string line;
std::string keyword;
std::string attribute;
std::string type;
std::string type_w;
2023-06-29 20:00:41 +00:00
while (getline(file, line)) {
if (line.empty() || line[0] == '%' || line == "\r" || line == " ") {
continue;
}
2023-11-08 17:45:35 +00:00
if (line.find("@attribute") != std::string::npos || line.find("@ATTRIBUTE") != std::string::npos) {
std::stringstream ss(line);
2023-06-29 20:00:41 +00:00
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())
2023-11-08 17:45:35 +00:00
throw std::invalid_argument("No attributes found");
2023-07-26 23:56:06 +00:00
}
2023-11-08 17:45:35 +00:00
void ArffFiles::load(const std::string& fileName, bool classLast)
2023-07-26 23:56:06 +00:00
{
int labelIndex;
loadCommon(fileName);
2023-06-29 20:00:41 +00:00
if (classLast) {
2023-11-08 17:45:35 +00:00
className = std::get<0>(attributes.back());
classType = std::get<1>(attributes.back());
2023-06-29 20:00:41 +00:00
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 {
2023-11-08 17:45:35 +00:00
className = std::get<0>(attributes.front());
classType = std::get<1>(attributes.front());
2023-06-29 20:00:41 +00:00
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);
}
2023-11-08 17:45:35 +00:00
void ArffFiles::load(const std::string& fileName, const std::string& name)
2023-07-26 23:56:06 +00:00
{
int labelIndex;
loadCommon(fileName);
bool found = false;
for (int i = 0; i < attributes.size(); ++i) {
if (attributes[i].first == name) {
2023-11-08 17:45:35 +00:00
className = std::get<0>(attributes[i]);
classType = std::get<1>(attributes[i]);
2023-07-26 23:56:06 +00:00
attributes.erase(attributes.begin() + i);
labelIndex = i;
found = true;
break;
}
}
if (!found) {
2023-11-08 17:45:35 +00:00
throw std::invalid_argument("Class name not found");
2023-07-26 23:56:06 +00:00
}
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
{
2023-11-08 17:45:35 +00:00
X = std::vector<std::vector<float>>(attributes.size(), std::vector<float>(lines.size()));
auto yy = std::vector<std::string>(lines.size(), "");
auto removeLines = std::vector<int>(); // Lines with missing values
2023-06-29 20:00:41 +00:00
for (size_t i = 0; i < lines.size(); i++) {
2023-11-08 17:45:35 +00:00
std::stringstream ss(lines[i]);
std::string value;
2023-06-29 20:00:41 +00:00
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);
}
2023-11-08 17:45:35 +00:00
std::string ArffFiles::trim(const std::string& source)
2023-06-29 20:00:41 +00:00
{
2023-11-08 17:45:35 +00:00
std::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;
}
2023-11-08 17:45:35 +00:00
std::vector<int> ArffFiles::factorize(const std::vector<std::string>& labels_t)
2023-06-29 20:00:41 +00:00
{
2023-11-08 17:45:35 +00:00
std::vector<int> yy;
2023-06-29 20:00:41 +00:00
yy.reserve(labels_t.size());
2023-11-08 17:45:35 +00:00
std::map<std::string, int> labelMap;
2023-06-29 20:00:41 +00:00
int i = 0;
2023-11-08 17:45:35 +00:00
for (const std::string& label : labels_t) {
2023-06-29 20:00:41 +00:00
if (labelMap.find(label) == labelMap.end()) {
labelMap[label] = i++;
}
yy.push_back(labelMap[label]);
}
return yy;
}