PLearn 0.1
ConfigParsing.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ConfigParsing.cc
00004 //
00005 // Copyright (C) 2009 Frederic Bastien
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: Frederic Bastien
00036 
00040 #include "ConfigParsing.h"
00041 #include <plearn/vmat/TextFilesVMatrix.h>
00042 #include <plearn/io/openFile.h>
00043 #include <plearn/io/pl_log.h>
00044 #include <plearn/base/stringutils.h>
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00050 PLearnCommandRegistry ConfigParsing::reg_(new ConfigParsing);
00051 
00052 ConfigParsing::ConfigParsing()
00053     : PLearnCommand(
00054         "ConfigParsing",
00055         ">>>> INSERT A SHORT ONE LINE DESCRIPTION HERE",
00056         ">>>> INSERT SYNTAX AND \n"
00057         "FULL DETAILED HELP HERE \n"
00058         )
00059 {}
00060 
00062 void ConfigParsing::run(const vector<string>& args)
00063 {
00064 /*    args0 = conf/conf.all.csv;
00065     args1 = conf/1convertCSV0709toPLearn.inc;
00066     args2 = conf/3b_remove_col.inc;
00067     args3 = conf/3fix_missing.inc;
00068     args4 = conf/9dichotomize.inc;
00069     args5 = conf/global_imputation_specifications.inc;
00070 */
00071     PLCHECK(args.size()==6);
00072     TextFilesVMatrix input = TextFilesVMatrix();
00073     input.auto_build_map = 0  ;
00074     input.default_spec="char";
00075     input.build_vmatrix_stringmap = 1  ;
00076     input.delimiter = ",;"  ;
00077     input.quote_delimiter = '"';
00078     input.skipheader.append(1);
00079     input.reorder_fieldspec_from_headers=1;
00080     input.txtfilenames.append(args[0]);
00081     input.partial_match=1;
00082     input.setMetaDataDir("output.prepro/"+args[0]+".metadatadir");
00083     input.build();
00084     bool all_uptodate = true;
00085     for(int i=1;i<=5;i++)
00086         if(!input.isUpToDate(args[i])){
00087             all_uptodate = false;
00088             break;
00089         }
00090     if(all_uptodate){
00091         NORMAL_LOG << "All config file are uptodate. We don't regenerate them."<<endl;
00092         return;
00093     }
00094     PStream f_csv = openFile(PPath(args[1]),PStream::raw_ascii,"w");
00095     PStream f_remove = openFile(args[2],PStream::raw_ascii,"w");
00096     PStream f_missing = openFile(args[3],PStream::raw_ascii,"w");
00097     PStream f_dichotomize = openFile(args[4],PStream::raw_ascii,"w");
00098     PStream f_imputation = openFile(args[5],PStream::raw_ascii,"w");
00099 
00100     for(int i=0;i<input.length();i++){
00101         TVec<string> r = input.getTextFields(i);
00102         char c = r[0][0];
00103         if(c=='#' || r[0].empty())//comment
00104             continue;
00105         if(!r[1].empty()){
00106             f_csv << (r[0]);
00107             f_csv << (" : ");
00108             f_csv << (r[1]) << endl;
00109         }
00110         string y = lowerstring(r[2]);//TODO check that this is an accepted command.
00111         bool remove=false;
00112         if(y=="y" ||y=="yes"){//comment
00113             f_remove << (r[0]) << endl;
00114             remove=true;
00115         }else if(y=="n" ||y=="no"||y==""){
00116         }else{
00117             PLERROR("Unknow value in column C:'%s'",r[2].c_str());
00118         }
00119         if(!r[3].empty() && !remove){
00120             f_missing << (r[0]);
00121             f_missing << (" : ");
00122             f_missing << (r[3]);//TODO check that this is an accepted command.
00123             f_missing << endl;
00124         }
00125         if(!r[4].empty() && !remove){
00126             f_imputation << (r[0]);
00127             f_imputation << (" : ");
00128             f_imputation << (r[4]);//TODO check that this is an accepted command.
00129             f_imputation << endl;
00130         }
00131         if(!r[5].empty() && !remove){
00132             string s = r[0];
00133             if(s[s.length()-1]=='*')
00134                 s=s.substr(0,s.length()-1);
00135             f_dichotomize << s <<" : ["<< r[5] << " ]"<<endl;
00136         }
00137     }
00138 }
00139 
00140 } // end of namespace PLearn
00141 
00142 
00143 /*
00144   Local Variables:
00145   mode:c++
00146   c-basic-offset:4
00147   c-file-style:"stroustrup"
00148   c-file-offsets:((innamespace . 0)(inline-open . 0))
00149   indent-tabs-mode:nil
00150   fill-column:79
00151   End:
00152 */
00153 // 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