KSquare Utilities
Attribute.h
Go to the documentation of this file.
1 #ifndef _ATTRIBUTE_
2 #define _ATTRIBUTE_
3 
4 /**
5  *@class KKMLL::Attribute
6  *@author Kurt Kramer
7  *@brief describes a single Feature, Type and possible values.
8  *@details Used to support 'FileDesc', 'FeatureVector', and is derived classes classes. FileDesc
9  * will maintain a list of 'Attribute' objects to describe each separate field in a given
10  * FeatureFile. A given Feature can be one of several types, Numeric, Nominal, Ordinal,
11  * and Symbolic.
12  *
13  *@code
14  * Numeric - Any floating point value.
15  * Nominal - Feature must be one of a specified possible values. The Attribute class will
16  * maintain a list of possible values. This will be saved as a integer value from
17  * in the FeatureVector object. When training a classifier it would be best to
18  * bit encode these features. See the FeatureEncoder class.
19  * Ordinal - Similar to Nominal except there is a definite ordering of value.
20  * Symbolic - Similar to Nominal except you do not know all the possible values this attribute
21  * can take on.
22  *@endcode
23  *@see KKMLL::FeatureEncoder, KKMLL::FeatureVector, KKMLL::FeatureFileIO
24 */
25 
26 #include <map>
27 #include <vector>
28 #include "KKStr.h"
29 #include "KKQueue.h"
30 #include "XmlStream.h"
31 using namespace KKB;
32 
33 
34 namespace KKMLL
35 {
36  enum class AttributeType: int
37  {
39  Ignore,
40  Numeric,
41  Nominal,
42  Ordinal,
43  Symbolic /**< Same as Nominal, except the names file does not
44  * list all possible values. They have to be determined from
45  * the data file.
46  */
47  };
48 
50 
51 
53  {
54  public:
56  AttributeTypeVector (kkuint32 initialSize, AttributeType initialValue);
57 
58 
59  void ReadXML (XmlStream& s,
60  XmlTagConstPtr tag,
61  VolConstBool& cancelFlag,
62  RunLog& log
63  );
64 
65  void WriteXML (const KKStr& varName,
66  std::ostream& o
67  ) const;
68 
69  }; /* AttributeTypeVector */
70 
71 
73 
74  class Attribute
75  {
76  public:
77  Attribute ();
78 
79  Attribute (const KKStr& _name,
80  AttributeType _type,
81  kkint32 _fieldNum
82  );
83 
84  Attribute (const Attribute& a);
85 
86  ~Attribute ();
87 
88  /**
89  *@brief Adds a allowable Nominal value to the Nominal or Symbolic field that this attribute represents.
90  *@details To only be used by instances of 'Attribute' that represent Nominal or Symbolic type attributes. If the
91  * Attribute type is not s 'Nominal' or 'Symbolic' then this method will throw am exception
92  *@param[in] nominalValue A possible value that this instance of 'Attribute' could represent.
93  *@param[out] alreadyExists Indicates if this instance of 'Attribute' already contains a nominal value called 'nominalValue'.
94  */
95  void AddANominalValue (const KKStr& nominalValue,
96  bool& alreadyExists
97  );
98 
99  /**
100  *@brief Returns back the cardinality of the attribute; the number of possible values it can take.
101  *@details Only attributes with type Nominal or Symbolic have a fixed number
102  * of possible values all others will return 999999999.
103  */
104  kkint32 Cardinality () const;
105 
106  kkint32 FieldNum () const {return fieldNum;}
107 
108  kkint32 GetNominalCode (const KKStr& nominalValue) const; // -1 means not found.
109 
110  /**
111  *@brief Returns the nominal value for the given ordinal value.
112  *@details For example: you could have a Attribute called DayOfTheWeek that would be type 'Nominal'
113  * where its possible values are "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", and "Sat". In this case a call
114  * to this method where 'code' == 3 would return "Wed".
115  */
116  const
117  KKStr& GetNominalValue (kkint32 code) const;
118 
120 
121  const
122  KKStr& Name () const {return name;}
123 
124  const
125  KKStr& NameUpper () const {return nameUpper;}
126 
127  void ReadXML (XmlStream& s,
128  XmlTagConstPtr tag,
129  VolConstBool& cancelFlag,
130  RunLog& log
131  );
132 
133  AttributeType Type () const {return type;}
134 
135  KKStr TypeStr () const;
136 
137  void WriteXML (const KKStr& varName,
138  std::ostream& o
139  ) const;
140 
141  Attribute& operator= (const Attribute& right);
142 
143  bool operator== (const Attribute& rightSide) const;
144  bool operator!= (const Attribute& rightSide) const;
145 
146  private:
147  void ValidateNominalType (const KKStr& funcName) const;
148 
149  kkint32 fieldNum;
150  KKStr name;
151  KKStr nameUpper;
152  KKStrListIndexed* nominalValuesUpper;
153  AttributeType type;
154  }; /* Attribute */
155 
157 
158 
160  {
161  public:
162  AttributeList ();
163 
164  AttributeList (bool owner);
165 
166  ~AttributeList ();
167 
168 
170 
171 
172  const
173  AttributePtr LookUpByName (const KKStr& attributeName) const;
174 
175 
177 
178 
179  void PushOnBack (AttributePtr attribute);
180 
181 
182  void PushOnFront (AttributePtr attribute);
183 
184 
185  void ReadXML (XmlStream& s,
186  XmlTagConstPtr tag,
187  VolConstBool& cancelFlag,
188  RunLog& log
189  );
190 
191 
192  void WriteXML (const KKStr& varName,
193  std::ostream& o
194  ) const;
195 
196  /**
197  *@brief Determines if two different attribute lists are the same; compares each respective attribute, name and type.
198  */
199  bool operator== (const AttributeList& right) const;
200 
201  /**
202  *@brief Determines if two different attribute lists are different; compares each respective attribute, name and type.
203  */
204  bool operator!= (const AttributeList& right) const;
205 
206  private:
207  void AddToNameIndex (AttributePtr attribute);
208 
209  std::map<KKStr, AttributePtr> nameIndex;
210  }; /* AttributeList */
211 
213 
215 
216 
217  const KKStr& AttributeTypeToStr (AttributeType type);
219 
220 
221 
224 
225 
228 
229 
232 
233 
234 } /* namespace KKMLL */
235 
236 #endif
void PushOnFront(AttributePtr attribute)
Definition: Attribute.cpp:391
Attribute(const Attribute &a)
Definition: Attribute.cpp:67
void AddANominalValue(const KKStr &nominalValue, bool &alreadyExists)
Adds a allowable Nominal value to the Nominal or Symbolic field that this attribute represents...
Definition: Attribute.cpp:123
const AttributePtr LookUpByName(const KKStr &attributeName) const
Definition: Attribute.cpp:370
void PushOnBack(AttributePtr attribute)
Definition: Attribute.cpp:384
__int32 kkint32
Definition: KKBaseTypes.h:88
const KKStr & GetNominalValue(kkint32 code) const
Returns the nominal value for the given ordinal value.
Definition: Attribute.cpp:143
bool operator!=(const AttributeList &right) const
Determines if two different attribute lists are different; compares each respective attribute...
Definition: Attribute.cpp:494
kkint32 GetNominalCode(const KKStr &nominalValue) const
Definition: Attribute.cpp:185
XmlElementAttribute * XmlElementAttributePtr
Definition: Attribute.h:227
XmlElementTemplate< Attribute > XmlElementAttribute
Definition: Attribute.h:226
XmlElementAttributeList * XmlElementAttributeListPtr
Definition: Attribute.h:231
bool operator!=(const Attribute &rightSide) const
Definition: Attribute.cpp:213
void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: Attribute.cpp:599
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
AttributeType operator++(AttributeType zed)
Definition: Attribute.cpp:566
describes a single Feature, Type and possible values.
Definition: Attribute.h:74
XmlElementTemplate< AttributeList > XmlElementAttributeList
Definition: Attribute.h:230
Attribute(const KKStr &_name, AttributeType _type, kkint32 _fieldNum)
Definition: Attribute.cpp:44
Attribute * AttributePtr
Definition: Attribute.h:156
kkint32 FieldNum() const
Definition: Attribute.h:106
KKTHread * KKTHreadPtr
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: Attribute.cpp:572
AttributeTypeVector * AttributeTypeVectorPtr
Definition: Attribute.h:72
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: Attribute.cpp:527
AttributeType AttributeTypeFromStr(const KKStr &s)
Definition: Attribute.cpp:442
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
AttributeTypeVector(kkuint32 initialSize, AttributeType initialValue)
Definition: Attribute.cpp:558
bool operator==(const AttributeList &right) const
Determines if two different attribute lists are the same; compares each respective attribute...
Definition: Attribute.cpp:466
kkint32 MemoryConsumedEstimated() const
Definition: Attribute.cpp:346
AttributeType
Definition: Attribute.h:36
kkint32 MemoryConsumedEstimated() const
Definition: Attribute.cpp:92
bool operator==(const Attribute &rightSide) const
Definition: Attribute.cpp:194
AttributeTypeVectorPtr CreateAttributeTypeVector() const
Definition: Attribute.cpp:406
AttributeType Type() const
Definition: Attribute.h:133
AttributeList(bool owner)
Definition: Attribute.cpp:334
AttributeList const * AttributeListConstPtr
Definition: Attribute.h:214
void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: Attribute.cpp:283
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
AttributeList * AttributeListPtr
Definition: Attribute.h:212
XmlElementAttributeTypeVector * XmlElementAttributeTypeVectorPtr
Definition: Attribute.h:223
const KKStr & Name() const
Definition: Attribute.h:122
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: Attribute.cpp:501
XmlElementTemplate< AttributeTypeVector > XmlElementAttributeTypeVector
Definition: Attribute.h:222
KKStr TypeStr() const
Definition: Attribute.cpp:241
const KKStr & NameUpper() const
Definition: Attribute.h:125
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: Attribute.cpp:249
kkint32 Cardinality() const
Returns back the cardinality of the attribute; the number of possible values it can take...
Definition: Attribute.cpp:173
const KKStr & AttributeTypeToStr(AttributeType type)
Definition: Attribute.cpp:422
Attribute & operator=(const Attribute &right)
Definition: Attribute.cpp:222
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163