FreeLing  3.1
fex_rule.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 General Public
00010 //    License as published by the Free Software Foundation; either
00011 //    version 3 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 //    General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU 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 #ifndef _FEX_RULE
00030 #define _FEX_RULE
00031 
00032 #include <map>
00033 #include <set>
00034 #include <boost/thread/mutex.hpp>
00035 #include "freeling/morfo/language.h"
00036 #include "freeling/morfo/tagset.h"
00037 #include "freeling/safe_map.h"
00038 #include "freeling/regexp.h"
00039 
00040 #define OP_NONE 0
00041 #define OP_AND 1
00042 #define OP_OR 2
00043 
00044 namespace freeling {
00045 
00049 
00050   class fex_status : public processor_status {
00051   public:
00054     std::map<std::wstring,std::map<int,std::list<std::wstring> > > features;
00057     std::map<std::wstring,std::vector<std::wstring> > re_result;
00058   };
00059 
00064 
00065   class fex_condition {
00066   private:
00068     std::wstring cid;
00070     std::wstring function;
00072     std::wstring focus;
00074     std::wstring split;
00076     std::wstring literal;
00078     std::set<std::wstring> *fileset;
00080     freeling::regexp match_re;
00082     bool negated;
00084     bool cond_true;
00085 
00087     static const freeling::regexp split_re;
00088 
00090     static std::map<std::wstring, std::set<std::wstring> > set_files;
00091     static boost::mutex sem_set_files;
00092 
00094     std::list<std::wstring> get_target(const word &, const tagset&) const;    
00095 
00096   public:
00097     // constructor
00098     fex_condition();
00100     fex_condition(const std::wstring&,const std::wstring&,const std::wstring&,const std::wstring&);
00102     fex_condition(const fex_condition &);
00104     fex_condition& operator=(const fex_condition&);
00105 
00107     bool check(const word&, const tagset&, fex_status *) const;
00109     bool is_true() const;
00111     std::wstring get_match(int, fex_status *) const;
00113     void trace(int) const;
00114   };
00115 
00120 
00121   class feature_function {  
00122   public: 
00123     virtual void extract (const sentence &, int, std::list<std::wstring> &) const =0;
00125     virtual ~feature_function() {};
00126   };
00127 
00128 
00132 
00133   class fex_rule {  
00134   private:
00136     std::wstring rid;
00138     std::wstring pattern;
00140     int left,right;
00142     std::list<fex_condition> conds;
00144     int operation;
00145 
00147     static const freeling::regexp rulepat;
00148     static const freeling::regexp rulepat_anch;
00149     static const freeling::regexp subexpr;
00150     static const freeling::regexp featfun;
00151 
00154     void pattern_instance(const sentence &, int, const tagset &, std::list<std::wstring> &) const;
00155     void get_replacements(const std::wstring &, const word &, const tagset &, std::list<std::wstring> &) const;
00156 
00157   public:
00159     fex_rule (const std::wstring &, const std::wstring &, const std::wstring &, int, const std::list<fex_condition> &);
00161     fex_rule(const fex_rule &);
00163     fex_rule& operator=(const fex_rule&);
00164 
00165     // get rule id
00166     std::wstring get_id() const;
00169     void precompute(const sentence&, int, const tagset&) const;
00172     void extract(const sentence&, int, int, const tagset&, std::list<std::wstring> &) const;
00174     int get_left() const;
00176     int get_right() const;
00177 
00179     static bool check_conds(const std::list<fex_condition> &, int, const word &, const tagset &, fex_status*);
00180 
00182     void trace(int) const;
00183 
00185     static safe_map<std::wstring,const feature_function *> feat_functs;
00186   };
00187 
00188 
00193 
00194   class fex_rulepack  {
00195   public:
00197     std::list<fex_condition> conds;
00199     int operation;
00201     std::list<fex_rule> rules;
00202 
00204     fex_rulepack();
00206     fex_rulepack(const fex_rulepack &);
00208     fex_rulepack& operator=(const fex_rulepack&);
00209 
00211     void trace(int) const;
00212   };
00213 
00214 } // namespace
00215 
00216 #endif
00217