PLearn 0.1
OutputFeaturesCommand.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // OutputFeaturesCommand.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 "OutputFeaturesCommand.h"
00041 #include <plearn/feat/FeatureSet.h>
00042 #include <plearn/vmat/VMatrix.h>
00043 #include <plearn/vmat/SubVMatrix.h>
00044 #include <plearn/db/getDataSet.h>
00045 #include <plearn/io/load_and_save.h>
00046 #include <plearn/feat/HashMapFeatureSet.h>
00047 #include <plearn/feat/CachedFeatureSet.h>
00048 
00049 namespace PLearn {
00050 using namespace std;
00051 
00053 PLearnCommandRegistry OutputFeaturesCommand::reg_(new OutputFeaturesCommand);
00054 
00055 OutputFeaturesCommand::OutputFeaturesCommand()
00056     : PLearnCommand(
00057         "output-features",
00058         "PLearn command that outputs the features found in a VMatrix input fields",
00059         "Usage: output-features <dataset> <feat_set> <output_string> [used_feat_set]\n"
00060         "       Will use feat_set to obtain the features from dataset and output\n"
00061         "       them. If output_string == 1, the features will be in string \n"
00062         "       format and if output_string == 0, the feature IDs will be given.\n"
00063         "       Optionaly, feat_set can be saved in a new file (used_feat_set)\n"
00064         "       after it has been used. This can be useful when the feature set\n"
00065         "       maintains a cache of the previously requested features of tokens.\n"
00066         "       Only the input fields' features are output, with the features of\n"
00067         "       a single token separated with by ' ' and the features from \n"
00068         "       different fields separated by '\\t'."
00069         )
00070 {}
00071 
00073 void OutputFeaturesCommand::run(const vector<string>& args)
00074 {
00075     if(args.size() != 3 && args.size() != 4)
00076     {
00077         cerr << helpmsg << endl;
00078         return;
00079     }
00080     string vmat_file = args[0];
00081     string feat_file = args[1];
00082     string output_string = args[2];
00083     if(args[2] != "0" && args[2] != "1")
00084         PLERROR("In OutputFeaturesCommand::run(): output_string should be 0 or 1");
00085     bool bool_output_string = tobool(output_string);
00086     string used_feat_file = "";
00087     if(args.size() == 4) used_feat_file = args[3];
00088 
00089     VMat vmat = getDataSet(vmat_file);
00090     VMat get_input_vmat;
00091     get_input_vmat = 
00092         new SubVMatrix(vmat,0,0,vmat->length(),
00093                        vmat->inputsize());
00094     
00095     PP<FeatureSet> feat;
00096     load(feat_file,feat);
00097     
00098     Vec row(get_input_vmat->width());
00099     string token;
00100     TVec<int> features;
00101     TVec<string> f_str;
00102     int l = get_input_vmat->length();
00103     int w = get_input_vmat->width();
00104     for(int i=0; i<l; i++)
00105     {
00106         get_input_vmat->getRow(i,row);
00107         for(int j=0; j<w; j++)
00108         {            
00109             token = get_input_vmat->getValString(j,row[j]);
00110             feat->getFeatures(token, features);
00111             if(bool_output_string)
00112                 for(int k=0; k<features.length(); k++)
00113                 {
00114                     cout << feat->getStringFeature(features[k]);
00115                     if( k < features.length()-1)
00116                         cout << " ";
00117                 }
00118             else
00119                 for(int k=0; k<features.length(); k++)
00120                 {
00121                     cout << features[k];
00122                     if( k < features.length()-1)
00123                         cout << " ";
00124                     
00125                 }
00126             if(j < l-1)
00127                 cout << "\t";
00128         }
00129         cout << endl; 
00130     }
00131 
00132     if(used_feat_file != "")
00133         save(used_feat_file,feat);
00134 }
00135 
00136 } // end of namespace PLearn
00137 
00138 
00139 /*
00140   Local Variables:
00141   mode:c++
00142   c-basic-offset:4
00143   c-file-style:"stroustrup"
00144   c-file-offsets:((innamespace . 0)(inline-open . 0))
00145   indent-tabs-mode:nil
00146   fill-column:79
00147   End:
00148 */
00149 // 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