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

svm_problem* Impala::Core::Training::ReadSvmFile ( const std::string &  filename  ) 

Definition at line 20 of file SvmFile.h.

References cInvalid, ILOG_ERROR, ILOG_VAR, svm_node::index, svm_problem::l, svm_node::value, svm_problem::x, and svm_problem::y.

Referenced by Impala::Core::Training::TesterIOHelper::ReadReferenceProblem().

00021 {
00022     ILOG_VAR(Impala.Core.Training.SvmFile);
00023     File f(filename, "r");
00024     if (!f.Valid())
00025         return 0;
00026 
00027     int sample=0;
00028     int nrNodes=0;
00029     while (!f.Eof())
00030     {
00031         std::istringstream iss(f.ReadLine(false));
00032         double label = cInvalid;
00033         iss >> label;
00034         if(label == cInvalid) // no label: empty line at the end of the file
00035             break;
00036         ++sample;
00037         while(!iss.eof())
00038         {
00039             int index;
00040             double value;
00041             char c = 0;
00042             iss >> index >> c >> value; // c should be ':'
00043             if(c != ':')
00044             {
00045                 ILOG_ERROR("parse error in svm file "<< filename <<
00046                            "on line "<< sample);
00047                 return 0;
00048             }
00049             ++nrNodes;
00050         }
00051         ++nrNodes; // extra node with index == -1
00052     }
00053     f.Rewind();
00054 
00055     // seccond pass:
00056     svm_problem* p = new svm_problem;
00057     p->l = sample;
00058     p->y = new double[sample];
00059     p->x = new svm_node*[sample];
00060     svm_node* nodes = new svm_node[nrNodes];
00061     svm_node* dst = nodes;
00062     for(int i=0 ; i<sample ; ++i)
00063     {
00064         std::istringstream iss(f.ReadLine(false));
00065         iss >> p->y[i];
00066         p->x[i] = dst;
00067         while(!iss.eof())
00068         {
00069             char c;
00070             iss >> dst->index >> c >> dst->value;
00071             ++dst;
00072         }
00073         dst->index = -1;
00074         ++dst;
00075     }
00076     f.Close();
00077     return p;
00078 }


Generated on Fri Mar 19 11:23:02 2010 for ImpalaSrc by  doxygen 1.5.1