src/IO/Serialization.h

Go to the documentation of this file.
00001 /*
00002 * This file is part of MultiBoost, a multi-class 
00003 * AdaBoost learner/classifier
00004 *
00005 * Copyright (C) 2005 Norman Casagrande
00006 * For informations write to nova77@gmail.com
00007 *
00008 * This library is free software; you can redistribute it and/or
00009 * modify it under the terms of the GNU Lesser General Public
00010 * License as published by the Free Software Foundation; either
00011 * version 2.1 of the License, or (at your option) any later version.
00012 *
00013 * This library is distributed in the hope that it will be useful,
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 * Lesser General Public License for more details.
00017 *
00018 * You should have received a copy of the GNU Lesser General Public
00019 * License along with this library; if not, write to the Free Software
00020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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> // for fill
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    // helper functions for creating xml tags
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       // The header of the vector
00133       ss << "<" << tagName << " size=\"" << values.size() << "\">\n";
00134       
00135       // The vector values
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       // closing tag
00143       ss << getTabs(numTab);
00144       ss << "</" << tagName << ">"; // close tag
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          // keeps getting the next token until it finds the tag
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       // move until the tag arrayName, then get the value of the param "size"
00289       seekAndParseParamTag(st, arrayName, tagParam, paramVal);
00290 
00291       // resize the vector to vector size
00292       // (FIXME: not very safe!)
00293       vecToFill.resize( atoi(paramVal.c_str() ) );
00294 
00295       T tmpVal;
00296       string className;
00297       string tag, param;
00298 
00299       // parses the xml vector definition
00300       const string closeTag = "/" + arrayName;
00301       while ( !nor_utils::cmp_nocase(tag, closeTag) && st.has_token() )
00302       {
00303          // seek the parameter "class"
00304          parseParamTag(st.next_token(), tag, param, paramVal );
00305          if ( !nor_utils::cmp_nocase(tag, "class") )
00306             continue;
00307 
00308          // convert the string into a value
00309          istringstream ss( st.next_token() );
00310          ss >> tmpVal;
00311 
00312          // stores the value, using the mapping from the class name
00313          // to the class index.
00314          vecToFill[ ClassMappings::getIdxFromClassName(paramVal) ] = tmpVal;
00315       }
00316 
00317    }
00318 
00319 };
00320 
00321 } // end of namespace MultiBoost
00322 
00323 #endif // __SERIALIZATION_H

Generated on Mon Nov 28 21:43:46 2005 for MultiBoost by  doxygen 1.4.5