PLearn 0.1
ProcessSymbolicSequenceVMatrix.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ProcessSymbolicSequenceVMatrix.h
00004 //
00005 // Copyright (C) 2004 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 /* *******************************************************
00036  * $Id: ProcessSymbolicSequenceVMatrix.h 6301 2006-10-15 22:41:57Z larocheh $
00037  ******************************************************* */
00038 
00039 // Authors: Hugo Larochelle
00040 
00044 #ifndef ProcessSymbolicSequenceVMatrix_INC
00045 #define ProcessSymbolicSequenceVMatrix_INC
00046 
00047 #include <plearn/vmat/SourceVMatrix.h>
00048 #include <plearn/vmat/VMat.h>
00049 #include <plearn/math/pl_math.h>
00050 #include <plearn/base/ProgressBar.h>
00051 //#include <hash_map>
00052 
00053 namespace PLearn {
00054 using namespace std;
00055 
00083 
00084 class ProcessSymbolicSequenceVMatrix: public SourceVMatrix
00085 {
00086 
00087 private:
00088 
00089     typedef SourceVMatrix inherited;
00090 
00091     typedef hash_map<int,int> this_hash_map;
00092 
00094     bool fixed_context;
00096     int max_context_length;
00098     int n_attributes;
00099 
00100     // fields for usage of last row buffer
00101 
00104     mutable int current_target_pos;
00107     mutable int lower_bound;
00109     mutable int upper_bound;
00111     mutable Vec current_row_i;
00114     mutable this_hash_map current_context_pos;
00115 
00117     TVec<int> indices;
00118 
00120     mutable TVec<int> left_positions, right_positions;
00121 
00123     mutable Vec left_context, right_context,
00124         row, element, target_element, subinput;
00125 
00126 protected:
00127 
00128     // *********************
00129     // * protected options *
00130     // *********************
00131 
00132 public:
00133 
00134     // ************************
00135     // * public build options *
00136     // ************************
00137 
00140     int n_left_context;
00141 
00144     int n_right_context;
00145 
00148     int conditions_offset;
00149 
00152     bool conditions_for_exclusion;
00153 
00156     bool full_context;
00157 
00160     bool put_only_target_attributes;
00161 
00163     bool use_last_context;
00164 
00167     bool exclude_missing_target_tokens;
00168 
00171     TVec< TVec< pair<int,int> > > conditions;
00172 
00175     TVec< TVec< pair<int,string> > > string_conditions;
00176 
00178     TVec< TVec< pair<int,int> > > delimiters;
00179 
00181     TVec< TVec< pair<int,string> > > string_delimiters;
00182 
00184     TVec< TVec< pair<int,int> > > ignored_context;
00185 
00187     TVec< TVec< pair<int,string> > > string_ignored_context;
00188 
00190     // DEPRECATED - use inherited::source
00191     // VMat source;
00192 
00193     // ****************
00194     // * Constructors *
00195     // ****************
00196 
00197     // Default constructor, make sure the implementation in the .cc
00198     // initializes all fields to reasonable default values.
00199     ProcessSymbolicSequenceVMatrix(bool call_build_ = false);
00200 
00201     ProcessSymbolicSequenceVMatrix(VMat s,
00202                                    int l_context, int r_context,
00203                                    bool call_build_ = true);
00204 
00205     // ******************
00206     // * Object methods *
00207     // ******************
00208 
00209 private:
00210 
00212     // (Please implement in .cc)
00213     void build_();
00214 
00215     void from_string_to_int_format(TVec< TVec< pair<int,string> > >& str_format,
00216                                    TVec< TVec< pair<int,int> > >& int_format)
00217     {
00218         int int_format_length = int_format.length();
00219         int_format.resize(int_format_length + str_format.length());
00220         for(int i=0; i<str_format.length(); i++)
00221             from_string_to_int_format(str_format[i],
00222                                       int_format[i+int_format_length]);
00223     }
00224 
00225     void from_string_to_int_format(TVec< pair<int,string> >& str_format,
00226                                    TVec< pair<int,int> >& int_format)
00227     {
00228         int int_format_length = int_format.length();
00229         int_format.resize(int_format_length+str_format.length());
00230         for(int i=0; i<str_format.length(); i++)
00231         {
00232             int_format[int_format_length+i].first = str_format[i].first;
00233             real value = source->getStringVal(str_format[i].first,
00234                                               str_format[i].second);
00235             if(is_missing(value))
00236                 PLERROR("In ProcessSymbolicSequenceVMatrix::"
00237                         "from_string_to_int_format :\n"
00238                         "%s not a valid string symbol for column %d\n",
00239                         str_format[i].second.c_str(), str_format[i].first);
00240             int_format[int_format_length+i].second = (int)value;
00241         }
00242     }
00243 
00244     bool is_true(const TVec< TVec< pair<int,int> > >& conditions,
00245                  const Vec row) const
00246     {
00247         bool condition_satisfied;
00248         for(int i=0; i<conditions.length(); i++)
00249         {
00250             condition_satisfied = true;
00251             if(conditions[i].length() == 0)
00252                 PLERROR("ProcessSymbolicSequenceVMatrix::is_true :\n"
00253                         "conditions[%d] should not be empty\n", i);
00254             for(int j=0; j<conditions[i].length(); j++)
00255             {
00256                 if(row[conditions[i][j].first] != conditions[i][j].second)
00257                 {
00258                     condition_satisfied = false;
00259                     break;
00260                 }
00261             }
00262             if(condition_satisfied) return true;
00263         }
00264 
00265         return false;
00266     }
00267 
00270     void fill_current_row(int i, int& cp, int& target_position) const;
00271 
00272 protected:
00273 
00275     // (Please implement in .cc)
00276     static void declareOptions(OptionList& ol);
00277 
00280     virtual void getNewRow(int i, const Vec& v) const;
00281 
00282 public:
00283 
00284     // Simply call inherited::build() then build_().
00285     virtual void build();
00286 
00288     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00289 
00292     virtual real getStringVal(int col, const string & str) const;
00293 
00296     virtual string getValString(int col, real val) const;
00297 
00298     virtual void getValues(int row, int col, Vec& values) const;
00299 
00300     virtual void getValues(const Vec& input, int col, Vec& values) const;
00301 
00302     virtual void getExample(int i, Vec& input, Vec& target, real& weight);
00303 
00306     virtual PP<Dictionary> getDictionary(int col) const;
00307 
00309     PLEARN_DECLARE_OBJECT(ProcessSymbolicSequenceVMatrix);
00310 
00311 
00312 };
00313 
00314 DECLARE_OBJECT_PTR(ProcessSymbolicSequenceVMatrix);
00315 
00316 } // end of namespace PLearn
00317 #endif
00318 
00319 
00320 /*
00321   Local Variables:
00322   mode:c++
00323   c-basic-offset:4
00324   c-file-style:"stroustrup"
00325   c-file-offsets:((innamespace . 0)(inline-open . 0))
00326   indent-tabs-mode:nil
00327   fill-column:79
00328   End:
00329 */
00330 // 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