-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArffReader.cpp
More file actions
124 lines (104 loc) · 2.99 KB
/
ArffReader.cpp
File metadata and controls
124 lines (104 loc) · 2.99 KB
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
#include "ArffReader.h"
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template <typename Iterator>
bool parse_relation(Iterator first, Iterator last, std::string& relation)
{
bool r = qi::phrase_parse(first, last, ( (qi::lit("@relation")|qi::lit("@RELATION")) >> +ascii::char_ ), ascii::space, relation);
if (first != last)
return false;
return r;
}
template <typename Iterator>
bool parse_attribute(Iterator first, Iterator last, std::string& attribute, std::vector<std::string>& v)
{
bool r = qi::phrase_parse(first, last,
(qi::lit("@attribute")|qi::lit("@ATTRIBUTE")) >>
+~ascii::char_('{') >>
'{' >> ( +~ascii::char_(",}") % ',' ) >> '}',
ascii::space, attribute, v);
if (first != last)
return false;
return r;
}
template <typename Iterator>
bool parse_data(Iterator first, Iterator last, std::vector<std::string>& v)
{
bool r = qi::phrase_parse(first, last, ( +~ascii::char_(',') % ',' ), ascii::space, v);
if (first != last)
return false;
return r;
}
}
int arffReader(std::string file)
{
ifstream arff;
arff.open(file);
std::string relation;
std::string str;
while(getline(arff, str))
{
if(str.empty())
break;
if(client::parse_relation(str.begin(), str.end(), relation))
{
//std::cout << relation << std::endl;
relationName = relation;
}
//else
//std::cout << "Parsing failed\n";
}
std::vector<std::map<std::string, unsigned int> > stringToInt;
while(getline(arff, str))
{
if(str.empty())
break;
std::vector<std::string> v;
std::string attribute;
if(client::parse_attribute(str.begin(), str.end(), attribute, v))
{
//std::cout << attribute << " ";
//for (std::vector<std::string>::size_type i = 0; i < v.size(); ++i)
//std::cout << v[i] << " ";
//std::cout << std::endl;
std::map<std::string, unsigned int> stoi;
for (std::vector<std::string>::size_type i = 0; i < v.size(); ++i)
stoi.insert(std::make_pair(v[i], i));
stringToInt.push_back(stoi);
valuesPerFeature.push_back(v.size());
attributes.push_back(std::make_pair(attribute, v));
}
//else
//std::cout << "Parsing failed\n";
}
while(getline(arff, str))
if(str.compare("@data") == 0 || str.compare("@DATA") == 0)
break;
while(getline(arff, str))
{
if(str.empty())
break;
std::vector<std::string> v;
if (client::parse_data(str.begin(), str.end(), v))
{
//for (std::vector<std::string>::size_type i = 0; i < v.size(); ++i)
//std::cout << v[i] << " ";
//std::cout << std::endl;
std::vector<unsigned int> vint;
for (std::vector<std::string>::size_type i = 0; i < v.size(); ++i)
{
//if(v[i].compare("?") == 0)
// vint.push_back(-1);
//else
vint.push_back(stringToInt[i].find(v[i])->second);
}
e.push_back(vint);
}
//else
//std::cout << "Parsing failed\n";
}
arff.close();
return 0;
}