Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

SvmProblemRepositoryInFile.h

Go to the documentation of this file.
00001 #ifndef Impala_Persistency_SvmProblemRepositoryInFile_h
00002 #define Impala_Persistency_SvmProblemRepositoryInFile_h
00003 
00004 #include "Persistency/RepositoryInFileSystem.h"
00005 #include "Persistency/ModelLocator.h"
00006 //#include "Core/Training/SvmProblem.h"
00007 #include "Link/Svm/LinkSvm.h"
00008 
00009 namespace Impala
00010 {
00011 namespace Persistency
00012 {
00013 
00014 
00015 class SvmProblemRepositoryInFile
00016 {
00017 public:
00018 
00019     SvmProblemRepositoryInFile()
00020     {
00021     }
00022 
00023     bool
00024     Exists(const ModelLocator& loc)
00025     {
00026         String dir = GetDir(loc);
00027         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".problem.txt",
00028                                     false, true);
00029         return file.Valid();
00030     }
00031 
00032     svm_problem*
00033     Get(const ModelLocator& loc)
00034     {
00035         String dir = GetDir(loc);
00036         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".problem.txt",
00037                                     false, false);
00038         if (!file.Valid())
00039             return 0;
00040         svm_problem* res = Read(file);
00041         return res;
00042     }
00043 
00044     void
00045     Add(const ModelLocator& loc, svm_problem* problem)
00046     {
00047         String dir = GetDir(loc);
00048         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".problem.txt",
00049                                     true, false);
00050         Write(problem, file);
00051     }
00052 
00053     void
00054     Delete(const ModelLocator& loc)
00055     {
00056         String dir = GetDir(loc);
00057         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".problem.txt",
00058                                     true, true);
00059         file.Delete();
00060     }
00061 
00062 private:
00063 
00064     RepositoryInFileSystem&
00065     RepFS()
00066     {
00067         return RepositoryInFileSystem::GetInstance();
00068     }
00069 
00070     String
00071     GetDir(const ModelLocator& loc)
00072     {
00073         String dir = "ConceptModels";
00074         dir = FileNameConcat(dir, loc.GetConceptSet());
00075         dir = FileNameConcat(dir, loc.GetModel());
00076         dir = FileNameConcat(dir, loc.GetFeature());
00077         return dir;
00078     }
00079 
00080     svm_problem*
00081     Read(File file)
00082     {
00083         const double cInvalid = 666;
00084         Util::IOBuffer* buf = file.GetReadBuffer();
00085         if (!buf)
00086             return 0;
00087 
00088         int sample = 0;
00089         int nrNodes = 0;
00090         while (buf->Available() > 0)
00091         {
00092             std::istringstream iss(buf->ReadLine());
00093             double label = cInvalid;
00094             iss >> label;
00095             if (label == cInvalid) // no label: empty line at the end of the file
00096                 break;
00097             sample++;
00098             while(!iss.eof())
00099             {
00100                 int index;
00101                 double value;
00102                 char c = 0;
00103                 iss >> index >> c >> value; // c should be ':'
00104                 if (c != ':')
00105                 {
00106                     ILOG_ERROR("Read: parse error in svm file "
00107                                << file.GetPath() << " on line " << sample);
00108                     delete buf;
00109                     return 0;
00110                 }
00111                 nrNodes++;
00112             }
00113             nrNodes++; // extra node with index == -1
00114         }
00115         buf->SetPosition(0);
00116 
00117         // seccond pass:
00118         svm_problem* p = new svm_problem;
00119         p->l = sample;
00120         p->y = new double[sample];
00121         p->x = new svm_node*[sample];
00122         svm_node* nodes = new svm_node[nrNodes];
00123         svm_node* dst = nodes;
00124         for (int i=0 ; i<sample ; i++)
00125         {
00126             std::istringstream iss(buf->ReadLine());
00127             iss >> p->y[i];
00128             p->x[i] = dst;
00129             while (!iss.eof())
00130             {
00131                 char c;
00132                 iss >> dst->index >> c >> dst->value;
00133                 dst++;
00134             }
00135             dst->index = -1;
00136             dst++;
00137         }
00138         delete buf;
00139         return p;
00140     }
00141 
00142     void
00143     Write(const svm_problem* p, File file)
00144     {
00145         Util::IOBuffer* buf = file.GetWriteBuffer();
00146         if (!buf)
00147             return;
00148 
00149         for (int i=0 ; i<p->l ; ++i)
00150         {
00151             String s = MakeString(p->y[i]);
00152             for (int j=0 ; p->x[i][j].index != -1 ; ++j)
00153             {
00154                 s += " " + MakeString(p->x[i][j].index) + ":"
00155                     + MakeString(p->x[i][j].value);
00156             }
00157             buf->Puts(s);
00158         }
00159         delete buf;
00160     }
00161 
00162     ILOG_VAR_DEC;
00163 };
00164 
00165 ILOG_VAR_INIT(SvmProblemRepositoryInFile, Impala.Persistency);
00166 
00167 } // namespace Persistency
00168 } // namespace Impala
00169 
00170 #endif

Generated on Thu Jan 13 09:05:12 2011 for ImpalaSrc by  doxygen 1.5.1