Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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,v 1.9 2004/07/21 16:30:51 chrish42 Exp $ 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 <map> 00055 00056 namespace PLearn { 00057 using namespace std; 00058 00059 00060 //class StringFieldMapping; //fwd decl. 00061 00062 class StringFieldMapping 00063 { 00064 public: 00065 00066 real dft_val; 00067 mutable hash_map<string, real> mapping; 00068 00069 StringFieldMapping() 00070 {} 00071 00072 StringFieldMapping(string filename_, real dft_val_= MISSING_VALUE) 00073 :dft_val(dft_val_), mapping() 00074 { 00075 /*ifstream*/ PIFStream f(filename_.c_str()); 00076 while(f) 00077 { 00078 string s; 00079 f >> s; 00080 //PLearn::read(f, s); 00081 real val; 00082 f >> val; 00083 //PLearn::read(f, val); 00084 if(f) mapping[s]= val; 00085 } 00086 } 00087 00088 real operator[](const string& s) const 00089 { 00090 if(mapping.end() == mapping.find(s)) 00091 return dft_val; 00092 return mapping[s]; 00093 } 00094 00095 }; 00096 00097 00098 class NumToStringMapping 00099 { 00100 public: 00101 00102 string filename; 00103 string dft_val; 00104 mutable hash_map<real, string> mapping; 00105 mutable bool loaded; 00106 00107 NumToStringMapping() 00108 :loaded(false) 00109 {} 00110 00111 NumToStringMapping(string filename_, const string& dft_val_= "") 00112 :filename(filename_), dft_val(dft_val_), mapping(), loaded(false) 00113 {} 00114 00115 void read(istream& in) const 00116 { 00117 while(in) 00118 { 00119 string s; 00120 PLearn::read(in, s); 00121 real val; 00122 PLearn::read(in, val); 00123 if(in) mapping[val]= s; 00124 } 00125 loaded= true; 00126 } 00127 00128 void load(string filename_= "") 00129 { 00130 if(filename_ != "") 00131 filename= filename_; 00132 ifstream f(filename.c_str()); 00133 read(f); 00134 } 00135 00136 void load() const 00137 { 00138 ifstream f(filename.c_str()); 00139 read(f); 00140 } 00141 00142 const string& operator[](real x) const 00143 { 00144 if(!loaded) load(); 00145 if(mapping.end() == mapping.find(x)) 00146 return dft_val; 00147 return mapping[x]; 00148 } 00149 00150 }; 00151 00152 00154 00155 class AutoSDBVMatrix: public RowBufferedVMatrix 00156 { 00157 public: 00158 AutoSDBVMatrix(const string& dbname); 00159 00160 00162 inline int nstrings() { return sdb_.width() - width(); } 00163 00165 void getMappings(); 00166 00169 virtual string getValString(int col, real val) const 00170 { 00171 hash_map<string, NumToStringMapping>::const_iterator it= 00172 num2string_map.find(fieldName(col)); 00173 if(it != num2string_map.end()) 00174 return it->second[val]; 00175 return ""; 00176 } 00177 00178 protected: 00179 00180 virtual void getNewRow(int i, const Vec& v) const; 00181 00182 SDB sdb_; 00183 mutable Row row_; 00184 hash_map<string, StringFieldMapping> string_field_map; 00185 hash_map<string, NumToStringMapping> num2string_map; 00186 }; 00187 00188 00189 00190 00191 00192 } // end of namespace PLearn 00193 00194 #endif

Generated on Tue Aug 17 15:48:36 2004 for PLearn by doxygen 1.3.7