PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // CachedFeatureSet.cc 00004 // 00005 // Copyright (C) 2006 Hugo Larochelle 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: Hugo Larochelle 00036 00040 #include "CachedFeatureSet.h" 00041 #include <plearn/vmat/VMatrix.h> 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 CachedFeatureSet, 00048 "Feature set that maintains a cached mapping between tokens and their features", 00049 "This cache permits potentially faster access to a token's\n" 00050 "features using the getFeatures() function, when a call to\n" 00051 "to that function has been done previously for that given token.\n" 00052 "A source FeatureSet that provides the features most be given.\n" 00053 "It is assumed that adding features will not change the index\n" 00054 "assignements of the previously added features.\n" 00055 ); 00056 00057 CachedFeatureSet::CachedFeatureSet() 00058 : report_progress(false) 00059 {} 00060 00061 // ### Nothing to add here, simply calls build_ 00062 void CachedFeatureSet::build() 00063 { 00064 inherited::build(); 00065 build_(); 00066 } 00067 00068 void CachedFeatureSet::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00069 { 00070 inherited::makeDeepCopyFromShallowCopy(copies); 00071 deepCopyField(source, copies); 00072 deepCopyField(token_to_features, copies); 00073 deepCopyField(f, copies); 00074 } 00075 00076 void CachedFeatureSet::declareOptions(OptionList& ol) 00077 { 00078 declareOption(ol, "source", &CachedFeatureSet::source, 00079 OptionBase::buildoption, 00080 "Source feature set, for which a buffer is maintained"); 00081 declareOption(ol, "token_to_features", &CachedFeatureSet::token_to_features, 00082 OptionBase::learntoption, 00083 "Token to features mapping"); 00084 declareOption(ol, "report_progress", &CachedFeatureSet::report_progress, 00085 OptionBase::buildoption, 00086 "Indication that progress should be reported in a ProgressBar\n" 00087 "when adding features in the cache in addFeatures(VMat,int).\n"); 00088 00089 // Now call the parent class' declareOptions 00090 inherited::declareOptions(ol); 00091 } 00092 00093 void CachedFeatureSet::build_() 00094 {} 00095 00096 void CachedFeatureSet::getFeatures(string token, TVec<int>& feats) 00097 { 00098 if(token_to_features.find(token) != token_to_features.end()) 00099 { 00100 f = token_to_features[token]; 00101 feats.resize(f.length()); 00102 feats << f; 00103 return; 00104 } 00105 source->getFeatures(token,feats); 00106 token_to_features[token] = feats.copy(); 00107 } 00108 00109 string CachedFeatureSet::getStringFeature(int index) 00110 { 00111 return source->getStringFeature(index); 00112 } 00113 00114 int CachedFeatureSet::getIndexFeature(string str) 00115 { 00116 return source->getIndexFeature(str); 00117 } 00118 00119 int CachedFeatureSet::size() 00120 { 00121 return source->size(); 00122 } 00123 00124 void CachedFeatureSet::addFeatures(string token) 00125 { 00126 source->addFeatures(token); 00127 // Insert these features in the cache 00128 getFeatures(token,f_temp); 00129 return; 00130 } 00131 00132 void CachedFeatureSet::addFeatures(VMat tokens, int min_freq) 00133 { 00134 source->addFeatures(tokens, min_freq); 00135 00136 // Inserting these features in the cache 00137 string token; 00138 Vec row(tokens->width()); 00139 00140 PP<ProgressBar> pb; 00141 int id=0; 00142 00143 if(report_progress) 00144 pb = new ProgressBar("Adding in cache the features of " + tostring(tokens->length()*tokens->width()) + " tokens", tokens->length()*tokens->width()); 00145 00146 for(int i=0; i<tokens->length(); i++) 00147 { 00148 tokens->getRow(i,row); 00149 for(int j=0; j<tokens->width(); j++) 00150 { 00151 token = tokens->getValString(j,row[j]); 00152 getFeatures(token,f_temp); 00153 if(report_progress) pb->update(++id); 00154 } 00155 } 00156 return; 00157 } 00158 00159 void CachedFeatureSet::clear() 00160 { 00161 source->clear(); 00162 token_to_features.clear(); 00163 } 00164 00165 void CachedFeatureSet::getNewFeaturesString(string token, TVec<string>& feats_str) 00166 { 00167 source->getNewFeaturesString(token,feats_str); 00168 } 00169 00170 } // end of namespace PLearn 00171 00172 00173 /* 00174 Local Variables: 00175 mode:c++ 00176 c-basic-offset:4 00177 c-file-style:"stroustrup" 00178 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00179 indent-tabs-mode:nil 00180 fill-column:79 00181 End: 00182 */ 00183 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :