00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "ClassMappings.h"
00024
00025 #include "Defaults.h"
00026 #include <fstream>
00027 #include <iostream>
00028
00031
00032 namespace MultiBoost {
00033
00034
00035 vector<string> ClassMappings::_mapIdxToClass;
00036 map<string, int> ClassMappings::_mapClassToIdx;
00037 int ClassMappings::_numRegClasses = 0;
00038
00039
00040
00041 int ClassMappings::addClassName(const string& className)
00042 {
00043
00044 if ( _mapClassToIdx.find(className) == _mapClassToIdx.end() )
00045 {
00046 _mapClassToIdx[className] = _numRegClasses;
00047 _mapIdxToClass.push_back(className);
00048 ++_numRegClasses;
00049 }
00050
00051 return _mapClassToIdx[className];
00052 }
00053
00054
00055
00056 void ClassMappings::loadClassMapFile(const string& classMapFileName)
00057 {
00058 ifstream classMapFile(classMapFileName.c_str());
00059 if ( !classMapFile.is_open() )
00060 {
00061 cerr << "ERROR: Cannot find classmap file <" << classMapFileName << ">!" << endl;
00062 exit(1);
00063 }
00064
00065 string className;
00066 do {
00067 classMapFile >> className;
00068 addClassName(className);
00069 } while ( !classMapFile.eof() && !className.empty() );
00070
00071 }
00072
00073
00074
00075
00076 string ClassMappings::getClassNameFromIdx(const int idx)
00077 {
00078 #if MB_DEBUG
00079 if ( idx >= _mapIdxToClass.size() )
00080 {
00081 cerr << "ERROR: trying to map a class index that does not exists. The input file" << endl;
00082 cerr << "might not have all the classes used for training. Use -classmap to define" << endl;
00083 cerr << "the list of classes instead." << endl;
00084 exit(1);
00085 }
00086 #endif
00087 return _mapIdxToClass[idx];
00088 }
00089
00090
00091
00092 int ClassMappings::getIdxFromClassName(const string& className)
00093 {
00094 #if MB_DEBUG
00095 if ( _mapClassToIdx.find(className) == _mapClassToIdx.end() )
00096 {
00097 cerr << "ERROR: trying to map a class (" << className << ") that does not exists." << endl;
00098 cerr << "The input file might not have all the classes used for training." << endl;
00099 cerr << "Use -classmap to define the list of classes instead." << endl;
00100 exit(1);
00101 }
00102 #endif
00103 return _mapClassToIdx[className];
00104 }
00105
00106
00107
00108
00109 }