Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

example.h

Go to the documentation of this file.
00001 
00002 //
00003 //    FreeLing - Open Source Language Analyzers
00004 //
00005 //    Copyright (C) 2004   TALP Research Center
00006 //                         Universitat Politecnica de Catalunya
00007 //
00008 //    This library is free software; you can redistribute it and/or
00009 //    modify it under the terms of the GNU Lesser General Public
00010 //    License as published by the Free Software Foundation; either
00011 //    version 2.1 of the License, or (at your option) any later version.
00012 //
00013 //    This library is distributed in the hope that it will be useful,
00014 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //    Lesser General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU Lesser General Public
00019 //    License along with this library; if not, write to the Free Software
00020 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021 //
00022 //    contact: Lluis Padro (padro@lsi.upc.es)
00023 //             TALP Research Center
00024 //             despatx C6.212 - Campus Nord UPC
00025 //             08034 Barcelona.  SPAIN
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 /*  iFeature:  a feature with integer label and real value                      */
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 /*  input:  vector of features                                                  */
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   // static functions
00089   static void set_consider_value (bool);
00090   static void set_label_value_separator (char);
00091   
00092   // constructors & destructor
00093   map_input()
00094     : _dimension(0)
00095     {}
00096 
00097   // string format: <l1>:<v1> <l2>:<v2> <ldim>:<vdim>
00098   map_input(string);
00099 
00100   // new input is f1*i1 + f2*i2
00101   map_input(double f1, const map_input& i1, double f2, const map_input& i2);
00102 
00103   ~map_input();
00104 
00105   // update functions
00106   void add_feature(const iFeature& f);
00107   void add_feature(int l, double v = 1.0);
00108 
00109   // string format: <l1>:<v1> <l2>:<v2> <ldim>:<vdim>
00110   void parse_string(string);
00111 
00112   // consultors
00113   double feature_value(int label) const;
00114   int    size() const { return mf.size(); }
00115   int    dimension() const { return _dimension; }
00116 
00117   // calcul
00118   double inner_product(map_input*) const;
00119 
00120   // recorregut de features
00121   // typedef  map<int,iFeature*>::iterator iterator;
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   // static functions
00163   static void set_consider_value (bool);
00164   static void set_label_value_separator (char);
00165   
00166   // constructors & destructor
00167   set_input()
00168     : _dimension(0)
00169     {}
00170 
00171   // string format: <l1>:<v1> <l2>:<v2> <ldim>:<vdim>
00172   set_input(string);
00173 
00174   // string format, char* parameter for SWIG compatibility
00175   set_input(char* s) 
00176     : _dimension(0)
00177     { string s0(s); parse_string(s0); }
00178 
00179   // copy constructor
00180   set_input(const set_input& i1);
00181 
00182   // new input is f1*i1 + f2*i2
00183   set_input(double f1, const set_input& i1, double f2, const set_input& i2);
00184 
00185   ~set_input();
00186 
00187   // update functions
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   // string format: <l1>:<v1> <l2>:<v2> <ldim>:<vdim>
00195   void parse_string(string);
00196 
00197   // consultors
00198   double feature_value(int label) const;
00199   int    size() const { return sf.size(); }
00200   int    dimension() const { return _dimension; }
00201 
00202   // calcul
00203   double inner_product(set_input*) const;
00204   double norm() const;
00205 
00206   // recorregut de features
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   // array of labels
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   // labels joined by separator
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 /*  example:  multilabelled example                                           */
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

Generated on Wed Apr 26 12:55:30 2006 for FreeLing by  doxygen 1.4.4