CharFeatures.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "features/CharFeatures.h"
00012 #include "lib/common.h"
00013 #include "lib/File.h"
00014
00015 CCharFeatures::CCharFeatures(EAlphabet a, int32_t size)
00016 : CSimpleFeatures<char>(size)
00017 {
00018 alphabet=new CAlphabet(a);
00019 }
00020
00021 CCharFeatures::CCharFeatures(CAlphabet* a, int32_t size)
00022 : CSimpleFeatures<char>(size)
00023 {
00024 alphabet= new CAlphabet(a);
00025 }
00026
00027 CCharFeatures::CCharFeatures(const CCharFeatures & orig)
00028 : CSimpleFeatures<char>(orig)
00029 {
00030 alphabet=orig.alphabet;
00031 }
00032
00033 CCharFeatures::CCharFeatures(EAlphabet a, char* fm, int32_t num_feat, int32_t num_vec)
00034 : CSimpleFeatures<char>(fm, num_feat, num_vec)
00035 {
00036 alphabet=new CAlphabet(a);
00037 }
00038
00039 CCharFeatures::CCharFeatures(EAlphabet a, char* fname)
00040 : CSimpleFeatures<char>(fname)
00041 {
00042 alphabet=new CAlphabet(a);
00043 load(fname);
00044 }
00045
00046 CCharFeatures::~CCharFeatures()
00047 {
00048 delete alphabet;
00049 alphabet=NULL;
00050 }
00051
00052 bool CCharFeatures::load(char* fname)
00053 {
00054 SG_INFO( "loading...\n");
00055 int64_t length=0;
00056 int64_t linelen=0;
00057
00058 CFile f(fname, 'r', F_CHAR);
00059 free_feature_matrix();
00060 feature_matrix=f.load_char_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 (int32_t lines=0; lines<num_vectors; lines++)
00081 {
00082 for (int32_t 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 CHAR\n");
00096 }
00097 else
00098 SG_ERROR( "reading file failed\n");
00099
00100 return false;
00101 }
00102
00103 bool CCharFeatures::save(char* fname)
00104 {
00105 return false;
00106 }
00107