-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextDataReader.cpp
81 lines (61 loc) · 1.83 KB
/
textDataReader.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
//
// Created by Tomev on 29.05.2017.
//
#include <c++/iostream>
#include <sstream>
#include "textDataReader.h"
#include "../attributesData/numericalAttributeData.h"
#include "../attributesData/categoricalAttributeData.h"
textDataReader::textDataReader(std::ifstream *sourceFile) : sourceFile(sourceFile) {}
void textDataReader::getNextRawDatum(void *target)
{
// If file is opened
if(sourceFile->is_open())
{
if(hasMoreData())
{
std::string *line = static_cast<std::string *>(target);
std::getline(*sourceFile, *line);
}
}
else
{
// If not then return error
std::cout << "Couldn't read file.\n";
}
}
void textDataReader::gatherAttributesData(unordered_map<string, attributeData*> *attributesData)
{
// This only works for .arff files. Not tested for others.
string line;
attributesOrder = new vector<string>();
if(attributesData != NULL)
{
// While line doesn't start with @attribute
while (line.find("@attribute")) getNextRawDatum(&line);
// While line starts with @attribute
while (!line.find("@attribute")) {
istringstream ss(line);
string type;
string name;
// Find name and type of attribute
getline(ss, name, ' ');
getline(ss, name, ' ');
getline(ss, type, ' ');
// Append attribute to proper place
if (type.find("numeric") == 0) (*attributesData)[name] = new numericalAttributeData(name);
else (*attributesData)[name] = new categoricalAttributeData(name);
attributesOrder->push_back(name);
getNextRawDatum(&line);
}
}
// If pointer was null, then only prepare for data reading.
while(line.find("@data")) getNextRawDatum(&line);
}
bool textDataReader::hasMoreData()
{
return !sourceFile->eof();
}
vector<string> *textDataReader::getAttributesOrder() {
return this->attributesOrder;
}