PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // NetflixVMatrix.cc 00004 // 00005 // Copyright (C) 2006 Pierre-Antoine Manzagol 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 // Authors: Pierre-Antoine Manzagol 00036 00040 #include "NetflixVMatrix.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 PLEARN_IMPLEMENT_OBJECT( 00046 NetflixVMatrix, 00047 "Used for holding the netflix data. Minimal implementation.", 00048 "Loads from a text file into a TVec< TVec< int > >.\n" 00049 "A hack for netflix." 00050 ); 00051 00053 // NetflixVMatrix // 00055 NetflixVMatrix::NetflixVMatrix() 00056 /* ### Initialize all fields to their default value here */ 00057 { 00058 // ... 00059 00060 // ### You may (or not) want to call build_() to finish building the object 00061 // ### (doing so assumes the parent classes' build_() have been called too 00062 // ### in the parent classes' constructors, something that you must ensure) 00063 } 00064 00066 // declareOptions // 00068 void NetflixVMatrix::declareOptions(OptionList& ol) 00069 { 00070 // ### Declare all of this object's options here. 00071 // ### For the "flags" of each option, you should typically specify 00072 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00073 // ### OptionBase::tuningoption. If you don't provide one of these three, 00074 // ### this option will be ignored when loading values from a script. 00075 // ### You can also combine flags, for example with OptionBase::nosave: 00076 // ### (OptionBase::buildoption | OptionBase::nosave) 00077 00078 // ### ex: 00079 declareOption(ol, "sourceFileName", &NetflixVMatrix::sourceFileName, 00080 OptionBase::buildoption, 00081 "Name of the source file - a netflix user profiles file."); 00082 00083 // Now call the parent class' declareOptions 00084 inherited::declareOptions(ol); 00085 } 00086 00088 // build // 00090 void NetflixVMatrix::build() 00091 { 00092 inherited::build(); 00093 build_(); 00094 } 00095 00097 // build_ // 00099 void NetflixVMatrix::build_() 00100 { 00101 00102 cout << "NetflixVMatrix::build_()" << endl; 00103 00104 // ### This method should do the real building of the object, 00105 // ### according to set 'options', in *any* situation. 00106 // ### Typical situations include: 00107 // ### - Initial building of an object from a few user-specified options 00108 // ### - Building of a "reloaded" object: i.e. from the complete set of 00109 // ### all serialised options. 00110 // ### - Updating or "re-building" of an object after a few "tuning" 00111 // ### options have been modified. 00112 // ### You should assume that the parent class' build_() has already been 00113 // ### called. 00114 00115 // Open source file 00116 ifstream input( sourceFileName.c_str() ); 00117 00118 if( ! input.is_open() ) { 00119 PLERROR("Could not open source file %s.", sourceFileName.c_str()); 00120 } 00121 00122 string str_line; 00123 int userIndex = 0; 00124 stringstream ss_tokens; 00125 int dummy; 00126 list< int > l_userProfile; 00127 00128 // Determine number of users and resize data 00129 while( getline(input, str_line) ) { 00130 userIndex++; 00131 } 00132 00133 cout << userIndex << " users" << endl; 00134 00135 length_ = userIndex; 00136 00137 data.resize(userIndex); 00138 input.clear(); 00139 input.seekg(0, ios::beg); 00140 userIndex = 0; 00141 00142 // Actual loading 00143 while( getline(input, str_line) ) { 00144 ss_tokens.str(str_line); 00145 ss_tokens.clear(); 00146 00147 while(ss_tokens >> dummy) { 00148 l_userProfile.push_back(dummy); 00149 } 00150 00151 data[userIndex].resize( l_userProfile.size() ); 00152 00153 list< int >::iterator itr_userProfile = l_userProfile.begin(); 00154 for(int i=0; itr_userProfile != l_userProfile.end(); itr_userProfile++, i++) { 00155 data[userIndex][i] = (*itr_userProfile); 00156 } 00157 l_userProfile.clear(); 00158 userIndex++; 00159 } 00160 00161 00162 00163 cout << "NetflixVMatrix::build_() - DONE!" << endl; 00164 00165 } 00166 00168 // get // 00170 real NetflixVMatrix::get(int i, int j) const 00171 { 00172 // get element at i-th row, j-th column 00173 PLERROR("Not implemented!"); 00174 return 0.0; 00175 } 00176 00178 // getExample // 00180 void NetflixVMatrix::getExample(int i, Vec& input, Vec& target, real& weight) 00181 { 00182 input.resize( data[i].length() ); 00183 input << data[i]; 00184 00185 } 00186 00187 /* 00189 // getSubRow // 00191 void NetflixVMatrix::getSubRow(int i, int j, Vec v) const 00192 { 00193 // get part or all of the i-th, starting at the j-th column, 00194 // with v.length() elements; these elements are put in v. 00195 PLERROR("Not implemented!"); 00196 } 00197 00199 // getRow // 00201 void NetflixVMatrix::getRow(int i, Vec v) const 00202 { 00203 PLERROR("Not implemented!"); 00204 } 00205 */ 00207 // makeDeepCopyFromShallowCopy // 00209 void NetflixVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00210 { 00211 inherited::makeDeepCopyFromShallowCopy(copies); 00212 00213 // ### Call deepCopyField on all "pointer-like" fields 00214 // ### that you wish to be deepCopied rather than 00215 // ### shallow-copied. 00216 // ### ex: 00217 deepCopyField(sourceFileName, copies); 00218 deepCopyField(data, copies); 00219 00220 // ### Remove this line when you have fully implemented this method. 00221 PLERROR("NetflixVMatrix::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00222 } 00223 00224 } // end of namespace PLearn 00225 00226 00227 /* 00228 Local Variables: 00229 mode:c++ 00230 c-basic-offset:4 00231 c-file-style:"stroustrup" 00232 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00233 indent-tabs-mode:nil 00234 fill-column:79 00235 End: 00236 */ 00237 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :