00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00028
00029
00030 #ifndef _EXAMPLE
00031 #define _EXAMPLE
00032
00033 #include <string>
00034 #include <map>
00035 #include <list>
00036 #include <set>
00037
00038 using namespace std;
00039
00040
00041
00042
00043
00044
00045
00046
00047 class iFeature {
00048 private:
00049 int _label;
00050 double _value;
00051
00052 public:
00053 iFeature(int l, double v = 1.0)
00054 : _label(l), _value(v)
00055 {}
00056
00057 int label() const { return _label; }
00058 double value() const { return _value; }
00059 void set_value(double v) { _value = v; }
00060
00061 bool operator<(const iFeature& rhs) const { return _label < rhs._label; }
00062
00063 };
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 class map_input;
00074 class set_input;
00075
00076 typedef map_input input;
00077
00078 class map_input {
00079 private:
00080 map<int,iFeature> mf;
00081 int _dimension;
00082
00083 static bool consider_value;
00084 static char label_value_separator;
00085
00086 public:
00087
00088
00089 static void set_consider_value (bool);
00090 static void set_label_value_separator (char);
00091
00092
00093 map_input()
00094 : _dimension(0)
00095 {}
00096
00097
00098 map_input(string);
00099
00100
00101 map_input(double f1, const map_input& i1, double f2, const map_input& i2);
00102
00103 ~map_input();
00104
00105
00106 void add_feature(const iFeature& f);
00107 void add_feature(int l, double v = 1.0);
00108
00109
00110 void parse_string(string);
00111
00112
00113 double feature_value(int label) const;
00114 int size() const { return mf.size(); }
00115 int dimension() const { return _dimension; }
00116
00117
00118 double inner_product(map_input*) const;
00119
00120
00121
00122 class const_iterator {
00123 private :
00124 map<int,iFeature>::const_iterator i;
00125
00126 public:
00127 const_iterator()
00128 {};
00129 const_iterator(map<int,iFeature>::const_iterator i0)
00130 : i(i0)
00131 {}
00132
00133 const iFeature* operator->() const { return &(i->second); }
00134
00135 const_iterator& operator++() { ++i; return *this; }
00136
00137 bool operator==(const const_iterator& rhs) { return i == rhs.i; }
00138 bool operator!=(const const_iterator& rhs) { return i != rhs.i; }
00139
00140 };
00141
00142 const_iterator begin() const { const_iterator i(mf.begin()); return i; }
00143 const_iterator end() const { const_iterator i(mf.end()); return i; }
00144
00145
00146 void print(ostream& o) const;
00147 void write(ostream& o) const;
00148 };
00149
00150
00151
00152 class set_input {
00153 private:
00154 set<iFeature> sf;
00155 int _dimension;
00156
00157 static bool consider_value;
00158 static char label_value_separator;
00159
00160 public:
00161
00162
00163 static void set_consider_value (bool);
00164 static void set_label_value_separator (char);
00165
00166
00167 set_input()
00168 : _dimension(0)
00169 {}
00170
00171
00172 set_input(string);
00173
00174
00175 set_input(char* s)
00176 : _dimension(0)
00177 { string s0(s); parse_string(s0); }
00178
00179
00180 set_input(const set_input& i1);
00181
00182
00183 set_input(double f1, const set_input& i1, double f2, const set_input& i2);
00184
00185 ~set_input();
00186
00187
00188 void add_feature(const iFeature& f);
00189 void add_feature(int l, double v = 1.0);
00190
00191 void multiply_by_factor(double f);
00192 void normalize();
00193
00194
00195 void parse_string(string);
00196
00197
00198 double feature_value(int label) const;
00199 int size() const { return sf.size(); }
00200 int dimension() const { return _dimension; }
00201
00202
00203 double inner_product(set_input*) const;
00204 double norm() const;
00205
00206
00207 typedef set<iFeature>::const_iterator const_iterator;
00208
00209 const_iterator begin() const { return sf.begin(); }
00210 const_iterator end() const { return sf.end(); }
00211
00212 void print(ostream& o) const;
00213 void write(ostream& o) const;
00214 };
00215
00216
00217
00218
00219 class mlOutput {
00220 private:
00221
00222 struct label {
00223 bool b;
00224 double weight;
00225 double prediction;
00226
00227 label():
00228 b(false), weight(0.0), prediction(0.0)
00229 { }
00230 };
00231
00232
00233 label* ml;
00234
00235
00236 static char _separator;
00237 void parse_string(string);
00238
00239 protected:
00240 static int _nlabels;
00241
00242 public:
00243 static void set_separator(char);
00244 static void set_nlabels(int);
00245 static int nlabels();
00246
00247 mlOutput();
00248
00249 mlOutput(string);
00250 ~mlOutput();
00251
00252 void set_label(int l, bool b) { ml[l].b = b; }
00253 bool belongs_to(int l) const { return ml[l].b; }
00254 int sign(int l) const { return ml[l].b ? +1 : -1; }
00255
00256 void set_weight(int l, double w) { ml[l].weight = w; }
00257 double weight(int l) const { return ml[l].weight; }
00258
00259 void set_prediction(int l, double pr) { ml[l].prediction = pr; }
00260 double prediction(int l) { return ml[l].prediction; }
00261
00262
00263 };
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 class example: public mlOutput, public input {
00274
00275 public:
00276 example(string ml, string f) : mlOutput(ml), input(f) {}
00277 example() : mlOutput(), input() {}
00278
00279 void print(ostream& o) const;
00280
00281 typedef input::const_iterator feature_iterator;
00282 };
00283
00284
00285
00286 #endif