ByteFeatures.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "features/ByteFeatures.h"
00012 #include "lib/common.h"
00013 #include "lib/File.h"
00014 
00015 CByteFeatures::CByteFeatures(E_ALPHABET a, INT size)
00016 : CSimpleFeatures<BYTE>(size)
00017 {
00018     alphabet=new CAlphabet(a);
00019 }
00020 
00021 CByteFeatures::CByteFeatures(CAlphabet* a, INT size)
00022 : CSimpleFeatures<BYTE>(size)
00023 {
00024     alphabet=a;
00025 }
00026 
00027 CByteFeatures::CByteFeatures(const CByteFeatures & orig)
00028 : CSimpleFeatures<BYTE>(orig)
00029 {
00030     alphabet=orig.alphabet;
00031 }
00032 
00033 CByteFeatures::CByteFeatures(E_ALPHABET a, BYTE* fm, INT num_feat, INT num_vec)
00034 : CSimpleFeatures<BYTE>(fm, num_feat, num_vec)
00035 {
00036     alphabet=new CAlphabet(a);
00037 }
00038 
00039 CByteFeatures::CByteFeatures(E_ALPHABET a, CHAR* fname)
00040 : CSimpleFeatures<BYTE>(fname)
00041 {
00042     alphabet=new CAlphabet(a);
00043     load(fname);
00044 }
00045 
00046 CByteFeatures::~CByteFeatures()
00047 {
00048     delete alphabet;
00049     alphabet=NULL;
00050 }
00051 
00052 bool CByteFeatures::load(CHAR* fname)
00053 {
00054     SG_INFO( "loading...\n");
00055     LONG length=0;
00056     LONG linelen=0;
00057 
00058     CFile f(fname, 'r', F_BYTE);
00059     free_feature_matrix();
00060     feature_matrix=f.load_byte_data(NULL, length);
00061 
00062     if (f.is_ok())
00063     {
00064         for (linelen=0; linelen<length; linelen++)
00065         {
00066             if (feature_matrix[linelen]=='\n')
00067             {
00068                 num_features=linelen;
00069                 linelen++;
00070                 break;
00071             }
00072         }
00073 
00074         num_vectors=length/linelen;
00075 
00076         SG_INFO( "file contains %ldx%ld vectors x features\n", num_vectors, num_features);
00077 
00078         if (length && (num_vectors*linelen==length))
00079         {
00080             for (INT lines=0; lines<num_vectors; lines++)
00081             {
00082                 for (INT columns=0; columns<num_features; columns++)
00083                     feature_matrix[lines*num_features+columns]=feature_matrix[lines*linelen+columns];
00084 
00085                 if (feature_matrix[lines*linelen+num_features]!='\n')
00086                 {
00087                SG_ERROR( "line %d in file \"%s\" is corrupt\n", lines, fname);
00088                     return false;
00089                 }
00090             }
00091 
00092             return true;
00093         }
00094         else
00095          SG_ERROR( "file is of zero size or no rectangular featurematrix of type BYTE\n");
00096     }
00097     else
00098       SG_ERROR( "reading file failed\n");
00099 
00100     return false;
00101 }
00102 
00103 bool CByteFeatures::save(CHAR* fname)
00104 {
00105     return false;
00106 }

SHOGUN Machine Learning Toolbox - Documentation