00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00028 #ifndef __SERIALIZATION_H
00029 #define __SERIALIZATION_H
00030
00031 #include "WeakLearners/BaseLearner.h"
00032 #include "IO/ClassMappings.h"
00033 #include "Utils/StreamTokenizer.h"
00034
00035 #include "Utils/Utils.h"
00036
00037 #include <sstream>
00038 #include <algorithm>
00039
00040 using namespace std;
00041
00044
00045 namespace MultiBoost {
00046
00052 class Serialization
00053 {
00054 public:
00055
00064 Serialization(const string& shypFileName, const string& weakLearnerName);
00065
00070 ~Serialization();
00071
00077 void saveHypotheses(vector<BaseLearner*>& weakHypotheses);
00078
00085 void appendHypothesis(const int iteration, BaseLearner* pWeakHypothesis);
00086
00088
00089
00098 template <typename T>
00099 static string standardTag(const string& tagName, const T& value, const int numTab = 0)
00100 {
00101 ostringstream ss;
00102 ss << getTabs(numTab);
00103 ss << "<" << tagName << ">" << value << "</" << tagName << ">";
00104 return ss.str();
00105 }
00106
00108
00125 template <typename T>
00126 static string vectorTag(const string& tagName, const vector<T>& values,
00127 const int numTab = 0)
00128 {
00129 ostringstream ss;
00130 ss << getTabs(numTab);
00131
00132
00133 ss << "<" << tagName << " size=\"" << values.size() << "\">\n";
00134
00135
00136 for (int i = 0; i < (int)values.size(); ++i)
00137 {
00138 ss << getTabs(numTab+1);
00139 ss << "<class id=\"" << ClassMappings::getClassNameFromIdx(i) << "\">" << values[i] << "</class>\n";
00140 }
00141
00142
00143 ss << getTabs(numTab);
00144 ss << "</" << tagName << ">";
00145
00146 return ss.str();
00147 }
00148
00149 private:
00150
00151 string _shypFileName;
00152
00159 inline static string getTabs(const int numTabs)
00160 {
00161 string tabs;
00162 tabs.resize(numTabs);
00163 fill(tabs.begin(), tabs.end(), '\t');
00164 return tabs;
00165 }
00166
00167 };
00168
00169
00170
00171
00172
00178 class UnSerialization
00179 {
00180 public:
00188 void loadHypotheses(const string& shypFileName,
00189 vector<BaseLearner*>& weakHypotheses);
00190
00201 static bool seekSimpleTag(nor_utils::StreamTokenizer& st, const string& tag);
00202
00213 static bool seekParamTag(nor_utils::StreamTokenizer& st, const string& tag);
00214
00229 static bool seekAndParseParamTag(nor_utils::StreamTokenizer& st,
00230 const string& tag, string& tagParam, string& paramValue);
00231
00242 static void parseParamTag(const string& str,
00243 string& tag, string& tagParam, string& paramValue);
00244
00253 template <typename T>
00254 static T seekAndParseEnclosedValue(nor_utils::StreamTokenizer& st, const string& tag)
00255 {
00256 do {
00257
00258 if ( nor_utils::cmp_nocase( st.next_token(), tag ) )
00259 {
00260 T val;
00261 istringstream ss(st.next_token());
00262 ss >> val;
00263 return val;
00264 }
00265 } while( st.has_token() );
00266
00267 return T();
00268 }
00269
00280 template <typename T>
00281 static void seekAndParseVectorTag(nor_utils::StreamTokenizer& st,
00282 const string& arrayName,
00283 vector<T>& vecToFill)
00284 {
00285
00286 string tagParam, paramVal;
00287
00288
00289 seekAndParseParamTag(st, arrayName, tagParam, paramVal);
00290
00291
00292
00293 vecToFill.resize( atoi(paramVal.c_str() ) );
00294
00295 T tmpVal;
00296 string className;
00297 string tag, param;
00298
00299
00300 const string closeTag = "/" + arrayName;
00301 while ( !nor_utils::cmp_nocase(tag, closeTag) && st.has_token() )
00302 {
00303
00304 parseParamTag(st.next_token(), tag, param, paramVal );
00305 if ( !nor_utils::cmp_nocase(tag, "class") )
00306 continue;
00307
00308
00309 istringstream ss( st.next_token() );
00310 ss >> tmpVal;
00311
00312
00313
00314 vecToFill[ ClassMappings::getIdxFromClassName(paramVal) ] = tmpVal;
00315 }
00316
00317 }
00318
00319 };
00320
00321 }
00322
00323 #endif // __SERIALIZATION_H