PLearn 0.1
AutoSDBVMatrix.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037  
00038 
00039 /* *******************************************************      
00040  * $Id: AutoSDBVMatrix.h 8231 2007-11-07 20:50:01Z nouiz $
00041  * AUTHOR: Pascal Vincent
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00047 #ifndef AutoSDBVMatrix_INC
00048 #define AutoSDBVMatrix_INC
00049 
00050 //#include "general.h"
00051 #include "SimpleDB.h"
00052 #include <plearn/vmat/VMat.h>
00053 #include <plearn/vmat/RowBufferedVMatrix.h>
00054 #include <plearn/io/openFile.h>
00055 //#include <map>
00056 
00057 namespace PLearn {
00058 using namespace std;
00059 
00060 
00061 //class StringFieldMapping; //fwd decl.
00062 
00063 class StringFieldMapping
00064 {
00065 public:
00066 
00067     real dft_val;
00068     mutable hash_map<string, real> mapping;
00069 
00070     StringFieldMapping()
00071     {}
00072 
00073     StringFieldMapping(string filename_, real dft_val_= MISSING_VALUE)
00074         :dft_val(dft_val_), mapping()
00075     {
00076         /*ifstream*/ PStream f = openFile(filename_.c_str(),PStream::plearn_ascii);
00077         while(f)
00078         {
00079             string s;
00080             f >> s;
00081             //PLearn::read(f, s);
00082             real val;
00083             f >> val;
00084             //PLearn::read(f, val);
00085             if(f) mapping[s]= val;
00086         }
00087     }
00088 
00089     real operator[](const string& s) const
00090     {
00091         if(mapping.end() == mapping.find(s))
00092             return dft_val;
00093         return mapping[s]; 
00094     }
00095  
00096 };
00097 
00098 
00099 class NumToStringMapping
00100 {
00101 public:
00102 
00103     string filename;
00104     string dft_val;
00105     mutable hash_map<real, string> mapping;
00106     mutable bool loaded;
00107 
00108     NumToStringMapping()
00109         :loaded(false)
00110     {}
00111 
00112     NumToStringMapping(string filename_, const string& dft_val_= "")
00113         :filename(filename_), dft_val(dft_val_), mapping(), loaded(false)
00114     {}
00115 
00116     void read(istream& in) const
00117     {
00118         while(in)
00119         {
00120             string s;
00121             PLearn::read(in, s);
00122             real val;       
00123             PLearn::read(in, val);
00124             if(in) mapping[val]= s;
00125         }
00126         loaded= true;
00127     }
00128 
00129     void load(string filename_= "")
00130     {
00131         if(filename_ != "")
00132             filename= filename_;
00133         ifstream f(filename.c_str());
00134         read(f);
00135     }
00136 
00137     void load() const
00138     {
00139         ifstream f(filename.c_str());
00140         read(f);
00141     }
00142 
00143     const string& operator[](real x) const
00144     {
00145         if(!loaded) load();
00146         if(mapping.end() == mapping.find(x))
00147             return dft_val;
00148         return mapping[x]; 
00149     }
00150  
00151 };
00152 
00153 
00155 
00156 class AutoSDBVMatrix: public RowBufferedVMatrix
00157 {
00158 public:
00159     AutoSDBVMatrix(const string& dbname);
00160 
00161 
00163     inline int nstrings() { return sdb_.width() - width(); }
00164   
00166     void getMappings();
00167 
00170     virtual string getValString(int col, real val) const
00171     {
00172         hash_map<string, NumToStringMapping>::const_iterator it= 
00173             num2string_map.find(fieldName(col));
00174         if(it != num2string_map.end())
00175             return it->second[val];
00176         return "";
00177     }
00178 
00179 protected:
00180 
00181     virtual void getNewRow(int i, const Vec& v) const;  
00182 
00183     SDB sdb_;
00184     mutable Row row_;
00185     hash_map<string, StringFieldMapping> string_field_map;
00186     hash_map<string, NumToStringMapping> num2string_map;
00187 };
00188 
00189 
00190 
00191 
00192 
00193 } // end of namespace PLearn
00194 
00195 #endif
00196 
00197 
00198 /*
00199   Local Variables:
00200   mode:c++
00201   c-basic-offset:4
00202   c-file-style:"stroustrup"
00203   c-file-offsets:((innamespace . 0)(inline-open . 0))
00204   indent-tabs-mode:nil
00205   fill-column:79
00206   End:
00207 */
00208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines