PLearn 0.1
ConcatDisjointFeatureSet.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ConcatDisjointFeatureSet.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 "ConcatDisjointFeatureSet.h"
00041 #include <plearn/base/lexical_cast.h>
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 PLEARN_IMPLEMENT_OBJECT(
00047     ConcatDisjointFeatureSet,
00048     "Feature set that is the concatenation of disjoint feature sets",
00049     "This class concatenates different feature sets but forces them\n"
00050     "to be disjoint. This means that, even though two feature sets\n"
00051     "to concatenate contain the same feature, a distinction will be\n"
00052     "made between the feature coming from the first and second set.\n"
00053     );
00054 
00055 ConcatDisjointFeatureSet::ConcatDisjointFeatureSet()
00056 {}
00057 
00058 // ### Nothing to add here, simply calls build_
00059 void ConcatDisjointFeatureSet::build()
00060 {
00061     inherited::build();
00062     build_();
00063 }
00064 
00065 void ConcatDisjointFeatureSet::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00066 {
00067     inherited::makeDeepCopyFromShallowCopy(copies);
00068     deepCopyField(feature_sets, copies);
00069     //PLERROR("ConcatDisjointFeatureSet::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00070 }
00071 
00072 void ConcatDisjointFeatureSet::declareOptions(OptionList& ol)
00073 {
00074     // Now call the parent class' declareOptions
00075     declareOption(ol, "feature_sets", &ConcatDisjointFeatureSet::feature_sets,
00076                   OptionBase::buildoption,
00077                   "Feature sets to concatenate");
00078 
00079     inherited::declareOptions(ol);
00080 }
00081 
00082 void ConcatDisjointFeatureSet::build_()
00083 {
00084     // Making sure subfeats has a non-null storage
00085     subfeats.resize(1);
00086     subfeats.resize(0);
00087 }
00088 
00089 void ConcatDisjointFeatureSet::getFeatures(string token, TVec<int>& feats)
00090 {
00091     feats.resize(0);
00092     int s = 0;
00093     for(int i=0; i<feature_sets.length(); i++)
00094     {
00095         feature_sets[i]->getFeatures(token,subfeats);
00096         subfeats += s;
00097         feats.append(subfeats);
00098         s += feature_sets[i]->size();
00099     }
00100 }
00101 
00102 void ConcatDisjointFeatureSet::getNewFeaturesString(string token, TVec<string>& feats_str)
00103 {
00104     feats_str.resize(0);
00105     for(int i=0; i<feature_sets.length(); i++)
00106     {
00107         feature_sets[i]->getNewFeaturesString(token, f_str);
00108         for(int j=0; j<f_str.length(); j++)
00109             f_str[j] = "f" + tostring(i) + ":" + f_str[j];
00110         feats_str.append(f_str);
00111     }
00112 }
00113 
00114 string ConcatDisjointFeatureSet::getStringFeature(int index)
00115 {
00116 #ifdef BOUNDCHECK
00117     if(index<0 || index >= size()) PLERROR("In ConcatDisjointFeatureSet::getStringFeature(): index %d is an invalid feature index", index);
00118 #endif
00119     int sub_index = index;
00120     for(int i=0; i<feature_sets.length(); i++)
00121     {
00122         if(sub_index<feature_sets[i]->size()) return "f" + tostring(i) + ":" + feature_sets[i]->getStringFeature(sub_index);
00123         else sub_index -= feature_sets[i]->size();
00124     }
00125     PLERROR("In ConcatDisjointFeatureSet::getStringFeature(): index %d is an invalid feature index", index);
00126     return "";
00127 }
00128 
00129 int ConcatDisjointFeatureSet::getIndexFeature(string str)
00130 {
00131     int fi = toint(tostring(str[1]));
00132     int s = 0;
00133 #ifdef BOUNDCHECK
00134     if(fi >= feature_sets.length()) PLERROR("In ConcatDisjointFeatureSet::getIndexFeature(): string prefix %s of feature is invalid", str.substr(0,3).c_str());
00135 #endif
00136     for(int i=0; i<fi; i++)
00137         s += feature_sets[i]->size();
00138     return feature_sets[fi]->getIndexFeature(str.substr(3,str.length()-3))+s;
00139 }
00140 
00141 int ConcatDisjointFeatureSet::size()
00142 {
00143     int ret = 0;
00144     for(int i=0; i<feature_sets.length(); i++)
00145         ret += feature_sets[i]->size();
00146     return ret;
00147 }
00148 
00149 void ConcatDisjointFeatureSet::addFeatures(string token)
00150 {
00151     for(int i=0; i<feature_sets.length(); i++)
00152         feature_sets[i]->addFeatures(token);
00153 }
00154 
00155 void ConcatDisjointFeatureSet::addFeatures(VMat tokens, int min_freq)
00156 {
00157     for(int i=0; i<feature_sets.length(); i++)
00158         feature_sets[i]->addFeatures(tokens, min_freq);
00159 }
00160 
00161 void ConcatDisjointFeatureSet::clear()
00162 {
00163     for(int i=0; i<feature_sets.length(); i++)
00164         feature_sets[i]->clear();
00165 }
00166 
00167 } // end of namespace PLearn
00168 
00169 
00170 /*
00171   Local Variables:
00172   mode:c++
00173   c-basic-offset:4
00174   c-file-style:"stroustrup"
00175   c-file-offsets:((innamespace . 0)(inline-open . 0))
00176   indent-tabs-mode:nil
00177   fill-column:79
00178   End:
00179 */
00180 // 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