KSquare Utilities
KKMLL::Attribute Class Reference

describes a single Feature, Type and possible values. More...

#include <Attribute.h>

Public Member Functions

 Attribute ()
 
 Attribute (const KKStr &_name, AttributeType _type, kkint32 _fieldNum)
 
 Attribute (const Attribute &a)
 
 ~Attribute ()
 
void AddANominalValue (const KKStr &nominalValue, bool &alreadyExists)
 Adds a allowable Nominal value to the Nominal or Symbolic field that this attribute represents. More...
 
kkint32 Cardinality () const
 Returns back the cardinality of the attribute; the number of possible values it can take. More...
 
kkint32 FieldNum () const
 
kkint32 GetNominalCode (const KKStr &nominalValue) const
 
const KKStrGetNominalValue (kkint32 code) const
 Returns the nominal value for the given ordinal value. More...
 
kkint32 MemoryConsumedEstimated () const
 
const KKStrName () const
 
const KKStrNameUpper () const
 
bool operator!= (const Attribute &rightSide) const
 
Attributeoperator= (const Attribute &right)
 
bool operator== (const Attribute &rightSide) const
 
void ReadXML (XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
 
AttributeType Type () const
 
KKStr TypeStr () const
 
void WriteXML (const KKStr &varName, std::ostream &o) const
 

Detailed Description

describes a single Feature, Type and possible values.

Author
Kurt Kramer

Used to support 'FileDesc', 'FeatureVector', and is derived classes classes. FileDesc will maintain a list of 'Attribute' objects to describe each separate field in a given FeatureFile. A given Feature can be one of several types, Numeric, Nominal, Ordinal, and Symbolic.

Numeric - Any floating point value.
Nominal - Feature must be one of a specified possible values. The Attribute class will
maintain a list of possible values. This will be saved as a integer value from
in the FeatureVector object. When training a classifier it would be best to
bit encode these features. See the FeatureEncoder class.
Ordinal - Similar to Nominal except there is a definite ordering of value.
Symbolic - Similar to Nominal except you do not know all the possible values this attribute
can take on.
See also
KKMLL::FeatureEncoder, KKMLL::FeatureVector, KKMLL::FeatureFileIO

Definition at line 74 of file Attribute.h.

Constructor & Destructor Documentation

Attribute::Attribute ( )

Definition at line 33 of file Attribute.cpp.

References KKB::KKStr::KKStr(), and KKMLL::NULLAttribute.

33  :
34  fieldNum (-1),
35  name (),
36  nameUpper (),
37  nominalValuesUpper (NULL),
39 
40 {
41 }
Attribute::Attribute ( const KKStr _name,
AttributeType  _type,
kkint32  _fieldNum 
)

Definition at line 44 of file Attribute.cpp.

References KKB::KKStr::KKStr(), KKB::KKStrListIndexed::KKStrListIndexed(), KKMLL::Nominal, KKMLL::Symbolic, and KKB::KKStr::Upper().

47  :
48  fieldNum (_fieldNum),
49  name (_name),
50  nameUpper (_name),
51  nominalValuesUpper (NULL),
52  type (_type)
53 
54 {
55  nameUpper.Upper ();
56  if ((type == AttributeType::Nominal) ||
57  (type == AttributeType::Symbolic)
58  )
59  {
60  nominalValuesUpper = new KKStrListIndexed (true, // true == nominalValuesUpper will own its contents.
61  false // false = Not Case Sensitive.
62  );
63  }
64 }
void Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
Attribute::Attribute ( const Attribute a)

Definition at line 67 of file Attribute.cpp.

References KKB::KKStr::KKStr(), KKB::KKStrListIndexed::KKStrListIndexed(), KKMLL::Nominal, and KKMLL::Symbolic.

67  :
68  fieldNum (a.fieldNum),
69  name (a.name),
70  nameUpper (a.nameUpper),
71  nominalValuesUpper (NULL),
72  type (a.type)
73 
74 {
75  if ((type == AttributeType::Nominal) ||
76  (type == AttributeType::Symbolic)
77  )
78  {
79  nominalValuesUpper = new KKStrListIndexed (*(a.nominalValuesUpper));
80  }
81 }
Attribute::~Attribute ( )

Definition at line 86 of file Attribute.cpp.

87 {
88  delete nominalValuesUpper;
89 }

Member Function Documentation

void Attribute::AddANominalValue ( const KKStr nominalValue,
bool &  alreadyExists 
)

Adds a allowable Nominal value to the Nominal or Symbolic field that this attribute represents.

To only be used by instances of 'Attribute' that represent Nominal or Symbolic type attributes. If the Attribute type is not s 'Nominal' or 'Symbolic' then this method will throw am exception

Parameters
[in]nominalValueA possible value that this instance of 'Attribute' could represent.
[out]alreadyExistsIndicates if this instance of 'Attribute' already contains a nominal value called 'nominalValue'.

Definition at line 123 of file Attribute.cpp.

References KKB::KKStrListIndexed::Add(), KKB::KKStr::Concat(), GetNominalCode(), and KKB::KKStr::KKStr().

Referenced by KKMLL::FileDesc::AddANominalValue(), and KKMLL::FeatureFileIOC45::LoadFile().

126 {
127  ValidateNominalType ("AddANominalValue");
128  kkint32 code = GetNominalCode (nominalValue);
129  if (code >= 0)
130  {
131  alreadyExists = true;
132  return;
133  }
134 
135  alreadyExists = false;
136  nominalValuesUpper->Add (new KKStr (nominalValue));
137 } /* AddANominalValue */
kkint32 Add(KKStrPtr s)
Definition: KKStr.cpp:5506
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 GetNominalCode(const KKStr &nominalValue) const
Definition: Attribute.cpp:185
kkint32 Attribute::Cardinality ( ) const

Returns back the cardinality of the attribute; the number of possible values it can take.

Only attributes with type Nominal or Symbolic have a fixed number of possible values all others will return 999999999.

Definition at line 173 of file Attribute.cpp.

References KKMLL::Nominal, KKB::KKStrListIndexed::size(), and KKMLL::Symbolic.

Referenced by KKMLL::FileDesc::Cardinality(), KKMLL::FileDesc::DisplayAttributeMappings(), KKMLL::FeatureEncoder::FeatureEncoder(), and KKMLL::FeatureFileIOC45::SaveFile().

174 {
175  if ((type == AttributeType::Nominal) ||
176  (type == AttributeType::Symbolic)
177  )
178  return nominalValuesUpper->size ();
179  else
180  return 999999999;
181 } /* Cardinality */
kkuint32 size() const
Definition: KKStr.cpp:5447
kkint32 KKMLL::Attribute::FieldNum ( ) const
inline

Definition at line 106 of file Attribute.h.

Referenced by KKMLL::FileDesc::GetFieldNumFromAttributeName().

106 {return fieldNum;}
kkint32 Attribute::GetNominalCode ( const KKStr nominalValue) const

Definition at line 185 of file Attribute.cpp.

References KKB::KKStrListIndexed::LookUp().

Referenced by AddANominalValue(), KKMLL::FileDesc::DisplayAttributeMappings(), KKMLL::FeatureFileIODstWeb::LoadFile(), KKMLL::FeatureFileIOC45::LoadFile(), and KKMLL::FileDesc::LookUpNominalCode().

186 {
187  ValidateNominalType ("GetNominalCode");
188  kkint32 code = nominalValuesUpper->LookUp (nominalValue);
189  return code;
190 } /* GetNominalCode */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 LookUp(const KKStr &s) const
Definition: KKStr.cpp:5558
const KKStr & Attribute::GetNominalValue ( kkint32  code) const

Returns the nominal value for the given ordinal value.

For example: you could have a Attribute called DayOfTheWeek that would be type 'Nominal' where its possible values are "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", and "Sat". In this case a call to this method where 'code' == 3 would return "Wed".

Definition at line 143 of file Attribute.cpp.

References KKB::KKStr::Concat(), KKB::KKStr::EmptyStr(), and KKB::KKStrListIndexed::LookUp().

Referenced by KKMLL::FileDesc::DisplayAttributeMappings(), KKMLL::FeatureEncoder::FeatureEncoder(), KKMLL::FeatureFileIORoberts::SaveFile(), and KKMLL::FeatureFileIOC45::SaveFile().

144 {
145  ValidateNominalType ("GetNominalValue");
146 
147  static KKStr missingDataValue = "?";
148 
149  if (code == -1)
150  {
151  // Missing Value Flag.
152  return missingDataValue;
153  }
154 
155  if (!nominalValuesUpper)
156  return KKStr::EmptyStr ();
157 
158  KKStrConstPtr result = nominalValuesUpper->LookUp (code);
159  if (result == NULL)
160  {
161  cerr << endl << endl
162  << "Attribute::GetNominalValue ***ERROR*** Code[" << code << "] Does not exist." << endl
163  << " Attribute::Name[" << name << "]" << endl
164  << endl;
165  return KKStr::EmptyStr ();
166  }
167 
168  return *result;
169 } /* GetNominalValue */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
kkint32 LookUp(const KKStr &s) const
Definition: KKStr.cpp:5558
kkint32 Attribute::MemoryConsumedEstimated ( ) const

Definition at line 92 of file Attribute.cpp.

References KKB::KKStr::MemoryConsumedEstimated(), and KKB::KKStrListIndexed::MemoryConsumedEstimated().

93 {
94  kkint32 memoryConsumedEstimated = sizeof (Attribute) +
95  name.MemoryConsumedEstimated () +
96  nameUpper.MemoryConsumedEstimated ();
97 
98  if (nominalValuesUpper)
99  memoryConsumedEstimated += nominalValuesUpper->MemoryConsumedEstimated ();
100 
101  return memoryConsumedEstimated;
102 }
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:766
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 MemoryConsumedEstimated() const
Definition: KKStr.cpp:5454
const KKStr& KKMLL::Attribute::Name ( ) const
inline

Definition at line 122 of file Attribute.h.

Referenced by KKMLL::FeatureEncoder::FeatureEncoder(), and KKMLL::FeatureFileIOC45::LoadFile().

122 {return name;}
const KKStr& KKMLL::Attribute::NameUpper ( ) const
inline

Definition at line 125 of file Attribute.h.

125 {return nameUpper;}
bool Attribute::operator!= ( const Attribute rightSide) const

Definition at line 213 of file Attribute.cpp.

References operator==().

214 {
215  return !(*this == rightSide);
216 } /* operator== */
Attribute & Attribute::operator= ( const Attribute right)

Definition at line 222 of file Attribute.cpp.

References KKB::KKStrListIndexed::KKStrListIndexed(), and KKB::KKStr::operator=().

223 {
224  fieldNum = right.fieldNum;
225  name = right.name;
226  nameUpper = right.nameUpper;
227 
228  delete nominalValuesUpper;
229  if (nominalValuesUpper)
230  nominalValuesUpper = new KKStrListIndexed (*right.nominalValuesUpper);
231  else
232  nominalValuesUpper = NULL;
233 
234  type = right.type;
235  return *this;
236 } /* operator= */
bool Attribute::operator== ( const Attribute rightSide) const

Definition at line 194 of file Attribute.cpp.

References KKMLL::Nominal, KKB::KKStr::operator!=(), KKB::KKStrListIndexed::operator!=(), KKMLL::Symbolic, and Type().

Referenced by operator!=().

195 {
196  if ((nameUpper != rightSide.nameUpper) || (Type () != rightSide.Type ()))
197  return false;
198 
199  if ((type == AttributeType::Nominal) ||
200  (type == AttributeType::Symbolic)
201  )
202  {
203  // Lets make sure that the nominal values are equal. Not Case sensitive.
204  if ((*nominalValuesUpper) != (*rightSide.nominalValuesUpper))
205  return false;
206  }
207 
208  return true;
209 } /* operator== */
AttributeType Type() const
Definition: Attribute.h:133
void Attribute::ReadXML ( XmlStream s,
XmlTagConstPtr  tag,
VolConstBool cancelFlag,
RunLog log 
)

Definition at line 283 of file Attribute.cpp.

References KKMLL::AttributeTypeFromStr(), KKB::XmlTag::AttributeValueInt32(), KKB::XmlTag::AttributeValueKKStr(), KKB::XmlStream::GetNextToken(), KKB::KKStr::operator=(), and KKB::KKStr::ToUpper().

288 {
289  delete nominalValuesUpper;
290  nominalValuesUpper = NULL;
291  type = AttributeTypeFromStr (tag->AttributeValueKKStr ("Type"));
292  fieldNum = tag->AttributeValueInt32 ("FieldNum");
293  name = tag->AttributeValueKKStr ("Name");
294  nameUpper = name.ToUpper ();
295 
296  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
297  while (t && (!cancelFlag))
298  {
299  if ((t->SectionName ().EqualIgnoreCase ("NominalValues")) && (typeid(*t) == typeid(XmlElementVectorKKStr)))
300  {
301  delete nominalValuesUpper;
302  nominalValuesUpper = new KKStrListIndexed (true, // true == nominalValuesUpper will own its contents.
303  false // false = Not Case Sensitive.
304  );
306  if ((v != NULL) && (v->Value () != NULL))
307  {
308  VectorKKStr::const_iterator idx;
309  bool alreadyEixists = false;
310  for (idx = v->Value ()->begin (); idx != v->Value ()->end (); ++idx)
311  AddANominalValue (*idx, alreadyEixists);
312  }
313  }
314  delete t;
315  t = s.GetNextToken (cancelFlag, log);
316  }
317  delete t;
318  t = NULL;
319 }
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
T *const Value() const
Definition: XmlStream.h:441
KKStr ToUpper() const
Definition: KKStr.cpp:2517
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
virtual const KKStr & SectionName() const
Definition: XmlStream.h:265
AttributeType AttributeTypeFromStr(const KKStr &s)
Definition: Attribute.cpp:442
XmlElementTemplate< VectorKKStr > XmlElementVectorKKStr
Definition: XmlStream.h:674
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
KKStr Attribute::TypeStr ( ) const

Definition at line 241 of file Attribute.cpp.

References KKMLL::AttributeTypeToStr().

Referenced by WriteXML().

242 {
243  return AttributeTypeToStr (type);
244 } /* TypeStr */
const KKStr & AttributeTypeToStr(AttributeType type)
Definition: Attribute.cpp:422
void Attribute::WriteXML ( const KKStr varName,
std::ostream &  o 
) const

Definition at line 249 of file Attribute.cpp.

References KKB::XmlTag::AddAtribute(), KKB::KKStr::Empty(), KKMLL::EncodeProblem(), KKMLL::Nominal, KKMLL::Symbolic, KKB::XmlTag::tagEmpty, KKB::XmlTag::tagEnd, KKB::XmlTag::tagStart, TypeStr(), KKB::VectorKKStr::WriteXML(), KKB::XmlTag::WriteXML(), and KKB::XmlTag::XmlTag().

Referenced by KKMLL::AttributeList::WriteXML().

252 {
253  XmlTag::TagTypes startTagType = XmlTag::TagTypes::tagEmpty;
254  if ((type == KKMLL::AttributeType::Nominal) ||
256  )
257  startTagType = XmlTag::TagTypes::tagStart;
258 
259  XmlTag startTag ("Attribute", startTagType);
260  if (!varName.Empty ())
261  startTag.AddAtribute ("VarName", varName);
262  startTag.AddAtribute ("Name", name);
263  startTag.AddAtribute ("Type", TypeStr ());
264  startTag.AddAtribute ("FieldNum", fieldNum);
265  startTag.WriteXML (o);
266  o << endl;
267 
268  if (startTagType == XmlTag::TagTypes::tagStart)
269  {
270  VectorKKStr nominalValues;
271  for (kkint32 nominalIdx = 0; nominalIdx < Cardinality (); ++nominalIdx)
272  nominalValues.push_back (GetNominalValue (nominalIdx));
273  nominalValues.WriteXML ("NominalValues", o);
274  XmlTag endTag ("Attribute", XmlTag::TagTypes::tagEnd);
275  endTag.WriteXML (o);
276  o << endl;
277  }
278 } /* WriteXML */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__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 Empty() const
Definition: KKStr.h:241
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:5319
KKStr TypeStr() const
Definition: Attribute.cpp:241
kkint32 Cardinality() const
Returns back the cardinality of the attribute; the number of possible values it can take...
Definition: Attribute.cpp:173

The documentation for this class was generated from the following files: