PLearn 0.1
StrTableVMatrix.cc
Go to the documentation of this file.
00001 // PLearn ("A C++ Machine Learning Library")
00002 // Copyright (C) 2002 Pascal Vincent and Julien Keable
00003 //
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //
00008 //  1. Redistributions of source code must retain the above copyright
00009 //     notice, this list of conditions and the following disclaimer.
00010 //
00011 //  2. Redistributions in binary form must reproduce the above copyright
00012 //     notice, this list of conditions and the following disclaimer in the
00013 //     documentation and/or other materials provided with the distribution.
00014 //
00015 //  3. The name of the authors may not be used to endorse or promote
00016 //     products derived from this software without specific prior written
00017 //     permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00022 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00024 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00026 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // This file is part of the PLearn library. For more information on the PLearn
00031 // library, go to the PLearn Web site at www.plearn.org
00032 
00033 /* *******************************************************
00034  * $Id: StrTableVMatrix.cc 5557 2006-05-10 20:36:58Z lamblin $
00035  * This file is part of the PLearn library.
00036  ******************************************************* */
00037 
00038 #include "StrTableVMatrix.h"
00039 #include <plearn/base/stringutils.h>
00040 
00041 namespace PLearn {
00042 using namespace std;
00043 
00044 StrTableVMatrix::StrTableVMatrix()
00045 {}
00046 
00047 
00048 /* This contructor takes a StringTable (which is simply a matrix of string) and converts it to a matrix of reals
00049    using real->string and string->real maps
00050 */
00051 StrTableVMatrix::StrTableVMatrix(const StringTable & st)
00052     : inherited(Mat(st.length(),st.width()))
00053 {
00054     map<string,real>::iterator it;
00055     double dbl;
00056     TVec<int> mapnum(st.width(),0);
00057     TVec<string> vec(st.width());
00058     TVec<bool> hasreal;
00059     Vec colmax;
00060     hasreal.resize(st.width());
00061     colmax.resize(st.width());
00062 
00063     for(int j=0;j<st.width();j++)
00064     {
00065         hasreal[j]=false;
00066         colmax[j]=0;
00067     }
00068 
00069     map_sr.resize(st.width());
00070     map_rs.resize(st.width());
00071 
00072     for(int j=0;j<st.width();j++)
00073         declareField(j,st.getFieldName(j), VMField::UnknownType);
00074 
00075     // 1st pass to detect maximums
00076     for(int i=0;i<st.length();i++)
00077     {
00078         vec=st(i);
00079         for(int j=0;j<st.width();j++)
00080             if(pl_isnumber(vec[j],&dbl))
00081             {
00082                 hasreal[j]=true;
00083                 if(!is_missing(dbl))
00084                     if(colmax[j]<dbl)
00085                         colmax[j]=dbl;
00086             }
00087     }
00088 
00089     for(int j=0;j<st.width();j++)
00090         if(hasreal[j])
00091             mapnum[j]=(int)ceil((double)colmax[j])+1;
00092 
00093     for(int i=0;i<st.length();i++)
00094     {
00095         vec=st(i);
00096         for(int j=0;j<st.width();j++)
00097             if(!pl_isnumber(vec[j],&dbl))
00098             {
00099                 if((it=map_sr[j].find(vec[j]))==map_sr[j].end())
00100                 {
00101                     data(i,j)=mapnum[j];
00102                     map_sr[j][vec[j]]=mapnum[j];
00103                     map_rs[j][mapnum[j]]=vec[j];
00104                     mapnum[j]++;
00105                 }
00106                 else data(i,j)=it->second;
00107             }
00108             else data(i,j)=dbl;
00109     }
00110 
00111     // We have modified the 'data' option.
00112     inherited::build();
00113 
00114 }
00115 
00116 
00117 void StrTableVMatrix::loadAllStringMappings()
00118 {
00119     // For a StrTableVMatrix, smap are already created
00120     return;
00121 }
00122 
00123 
00124 PLEARN_IMPLEMENT_OBJECT(StrTableVMatrix, "ONE LINE DESCR", "NO HELP");
00125 
00126 } // end of namespace PLearn
00127 
00128 
00129 /*
00130   Local Variables:
00131   mode:c++
00132   c-basic-offset:4
00133   c-file-style:"stroustrup"
00134   c-file-offsets:((innamespace . 0)(inline-open . 0))
00135   indent-tabs-mode:nil
00136   fill-column:79
00137   End:
00138 */
00139 // 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