ArffBase.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // ArffBase.cpp
  2. #include "ArffBase.h"
  3. #include <fstream>
  4. #include <iterator>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <iostream>
  9. using namespace std;
  10. ArffBase::ArffBase() : m_dataReached(false)
  11. {
  12. }
  13. ArffBase::ArffBase(const char* filename) : m_dataReached(false)
  14. {
  15. Load(filename);
  16. }
  17. ArffBase::~ArffBase()
  18. {
  19. }
  20. bool ArffBase::Load(const char* filename)
  21. {
  22. ifstream ifs;
  23. ifs.open(filename, ifstream::in);
  24. if (ifs.fail())
  25. {
  26. cerr << "Could not open provided ARFF file: " << filename << endl;
  27. return false;
  28. }
  29. string line;
  30. while (ifs)
  31. {
  32. if(!getline(ifs, line))
  33. break;
  34. ProcessArffLine(line);
  35. }
  36. ifs.close();
  37. return true;
  38. }
  39. void ArffBase::Save(const char* filename)
  40. {
  41. ofstream ofs;
  42. ofs.open(filename, ofstream::out);
  43. ofs << "@RELATION " << m_relation << endl << endl;
  44. for (auto p: m_mMetadata)
  45. ofs << "\%@METADATA " << p.first << " " << p.second << endl;
  46. ofs << endl;
  47. for (unsigned int i=0; i<m_vpAttributes.size(); i++)
  48. (*m_vpAttributes[i]).PrintDescription(ofs);
  49. ofs << endl;
  50. for (auto p:m_vComments)
  51. ofs << p << endl;
  52. ofs << endl;
  53. ofs << "@DATA" << endl;
  54. for (auto lineEntry:m_data)
  55. {
  56. unsigned int i;
  57. for (i=0; i<lineEntry.size()-1; i++)
  58. {
  59. (*m_vpAttributes.at(i)).Print(ofs, lineEntry[i]);
  60. ofs << ",";
  61. }
  62. (*m_vpAttributes.at(i)).Print(ofs, lineEntry[i]);
  63. ofs << endl;
  64. }
  65. ofs.close();
  66. }
  67. void ArffBase::AddRow(vector<double> row)
  68. {
  69. assert(row.size() == m_vpAttributes.size());
  70. m_data.push_back(row);
  71. }
  72. void ArffBase::AddColumn(string attName, string type)
  73. {
  74. // remove spaces from type
  75. type.erase(remove(type.begin(), type.end(), ' '), type.end());
  76. m_vpAttributes.push_back(AttributeFactory(attName, type));
  77. for (auto &row:m_data)
  78. row.push_back(0.0);
  79. }
  80. vector<double>& ArffBase::operator[](const int index)
  81. {
  82. // range checked indexing
  83. return m_data.at(index);
  84. }
  85. void ArffBase::SetRelation(string relation)
  86. {
  87. m_relation = relation;
  88. }
  89. string ArffBase::GetRelation() const
  90. {
  91. return m_relation;
  92. }
  93. bool ArffBase::GetMetadata(const string& key, string& value) const
  94. {
  95. map<string,string>::const_iterator it = m_mMetadata.find(key);
  96. if (it != m_mMetadata.end())
  97. {
  98. value = it->second;
  99. return true;
  100. }
  101. value = "";
  102. return false;
  103. }
  104. bool ArffBase::SetMetadata(const string &key, const string &value)
  105. {
  106. m_mMetadata[key] = value;
  107. return true;
  108. }
  109. bool ArffBase::GetAttMapping(const int &attIndex, vector<string>& values) const
  110. {
  111. values = m_vpAttributes.at(attIndex)->GetMapping();
  112. if (values.size() == 0)
  113. return false;
  114. return true;
  115. }
  116. bool ArffBase::GetAttMapping(const string &attName, vector<string>& values) const
  117. {
  118. int index = 0;
  119. if (!GetAttIndex(attName, index))
  120. return false;
  121. return GetAttMapping(index, values);
  122. }
  123. bool ArffBase::GetAttIndex(const string &attName, int &index) const
  124. {
  125. for (index=0; index<(int)m_vpAttributes.size(); index++)
  126. {
  127. string curAttName = m_vpAttributes[index]->GetName();
  128. if (CompareStrings(attName, curAttName))
  129. break;
  130. }
  131. if (index == (int)m_vpAttributes.size())
  132. return false;
  133. return true;
  134. }
  135. void ArffBase::Size(int &rows, int &columns) const
  136. {
  137. rows = m_data.size();
  138. columns = m_vpAttributes.size();
  139. }
  140. arffData::const_iterator ArffBase::cbegin() const
  141. {
  142. return m_data.cbegin();
  143. }
  144. arffData::const_iterator ArffBase::cend() const
  145. {
  146. return m_data.cend();
  147. }
  148. arffData::iterator ArffBase::begin()
  149. {
  150. return m_data.begin();
  151. }
  152. arffData::iterator ArffBase::end()
  153. {
  154. return m_data.end();
  155. }
  156. // PRIVATE:
  157. void ArffBase::ProcessArffLine(string& line)
  158. {
  159. if (line.size() == 0)
  160. return;
  161. // break string on white space
  162. stringstream ss(line);
  163. istream_iterator<string> begin(ss);
  164. istream_iterator<string> end;
  165. vector<string> lineWords(begin, end);
  166. // find what type of line it is
  167. if (m_dataReached)
  168. ProcessData(line);
  169. else if (lineWords.size() > 0 && CompareStrings(lineWords[0], "@ATTRIBUTE"))
  170. ProcessAttribute(lineWords);
  171. else if (lineWords.size() > 0 && CompareStrings(lineWords[0], "\%@METADATA"))
  172. ProcessMetadata(lineWords);
  173. else if (lineWords.size() > 1 && CompareStrings(lineWords[0], "@RELATION"))
  174. m_relation = lineWords[1];
  175. else if (line[0] == '%')
  176. ProcessComment(line);
  177. else if (lineWords.size() > 0 && CompareStrings(lineWords[0], "@DATA"))
  178. m_dataReached = true;
  179. }
  180. void ArffBase::ProcessMetadata(vector<string>& metaLine)
  181. {
  182. m_mMetadata[metaLine[1]] = metaLine[2];
  183. }
  184. void ArffBase::ProcessAttribute(vector<string>& attLine)
  185. {
  186. // Attribute with fewer than 3 string parts is not possible
  187. if (attLine.size() < 3)
  188. return;
  189. string attName = attLine[1];
  190. string type;
  191. if (attLine[2].find('{') != string::npos)
  192. {
  193. for (auto s:attLine)
  194. type += s;
  195. }
  196. else
  197. type = attLine[2];
  198. m_vpAttributes.push_back(AttributeFactory(attName, type));
  199. }
  200. unique_ptr<AttributeType> ArffBase::AttributeFactory(string attName, string type)
  201. {
  202. unique_ptr<AttributeType> res;
  203. if (CompareStrings(type, "INTEGER"))
  204. res.reset(new AttributeTypeInt(attName));
  205. else if (CompareStrings(type, "NUMERIC"))
  206. res.reset(new AttributeTypeNum(attName));
  207. else
  208. res.reset(new AttributeTypeNom(attName, type));
  209. return move(res);
  210. }
  211. void ArffBase::ProcessComment(string& comLine)
  212. {
  213. m_vComments.push_back(comLine);
  214. }
  215. void ArffBase::ProcessData(string& dataLine)
  216. {
  217. // remove comments
  218. size_t pos = dataLine.find("\%");
  219. if (pos != string::npos)
  220. dataLine = dataLine.substr(0,pos);
  221. stringstream ss(dataLine);
  222. int id = 0;
  223. vector<double> dataEntry;
  224. while (ss)
  225. {
  226. string value;
  227. if (!getline(ss, value, ','))
  228. break;
  229. // Convert value based on the attribute
  230. double singleEntry = (*m_vpAttributes.at(id))(value);
  231. dataEntry.push_back(singleEntry);
  232. id++;
  233. }
  234. if (id == 0)
  235. return;
  236. assert(dataEntry.size() == m_vpAttributes.size());
  237. m_data.push_back(dataEntry);
  238. }
  239. bool ArffBase::CompareStrings(string first, string second) const
  240. {
  241. transform(first.begin(), first.end(), first.begin(), ::toupper);
  242. transform(second.begin(), second.end(), second.begin(), ::toupper);
  243. return first == second;
  244. }