AttributeTypes.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // AttributeTypes.h
  2. #ifndef __ATTRIBUTETYPES_H__
  3. #define __ATTRIBUTETYPES_H__
  4. #include <string>
  5. #include <ostream>
  6. #include <fstream>
  7. #include <map>
  8. #include <vector>
  9. using namespace std;
  10. class AttributeType
  11. {
  12. public:
  13. AttributeType(string attName) : m_attName(attName) {}
  14. virtual ~AttributeType() = 0;
  15. virtual double operator()(string attValue) = 0;
  16. ///< Functor to convert the string values of the attribute in the file
  17. ///< to double representation in the data.
  18. virtual void Print(ofstream& ofs, double const &attValue) = 0;
  19. ///< Writes data correctly formatted to the provided output file stream.
  20. virtual void PrintDescription(ofstream &ofs);
  21. ///< Writes the description of the attributes as it should be in the ARFF file.
  22. virtual vector<string> GetMapping();
  23. ///< Returns the mapping of the attribute values to the one
  24. ///< provided in the definition of the attribute.
  25. string GetName();
  26. private:
  27. string m_attName;
  28. };
  29. // Class representing Integer attribute in ARFF
  30. class AttributeTypeInt : public AttributeType
  31. {
  32. public:
  33. AttributeTypeInt(string attName);
  34. ~AttributeTypeInt();
  35. double operator()(string attValue);
  36. ///< The returned double can correctly represent an integer up the
  37. ///< range of its fraction term with 0 exponent.
  38. void Print(ofstream& ofs, double const &attValue);
  39. void PrintDescription(ofstream &ofs);
  40. };
  41. // Class representing Numeric attribute in ARFF
  42. class AttributeTypeNum : public AttributeType
  43. {
  44. public:
  45. AttributeTypeNum(string attName, int precision = 2);
  46. ~AttributeTypeNum();
  47. double operator()(string attValue);
  48. ///< Just return a double.
  49. void Print(ofstream& ofs, double const &attValue);
  50. void PrintDescription(ofstream &ofs);
  51. private:
  52. int m_precision;
  53. };
  54. // Class representing Nominal attribute in ARFF
  55. class AttributeTypeNom : public AttributeType
  56. {
  57. public:
  58. AttributeTypeNom(string attName, string attDescription);
  59. ///< The input string is the line describing the attribute. It processes
  60. ///< the content within the curly braces.
  61. ~AttributeTypeNom();
  62. double operator()(string attValue);
  63. ///< Returns the position of the string the list of nominal values of teh attribute.
  64. ///< It works quivalently to an enumeration.
  65. void Print(ofstream& ofs, double const &attValue);
  66. void PrintDescription(ofstream &ofs);
  67. vector<string> GetMapping();
  68. ///< Returns the mapping of nominal values to double.
  69. private:
  70. map<string,double> m_nominalMap;
  71. map<double,string> m_inversedMap;
  72. };
  73. #endif /*__ATTRIBUTETYPES_H__*/