FreeLing  4.0
fex_rule.h
Go to the documentation of this file.
00001 
00002 //
00003 //    FreeLing - Open Source Language Analyzers
00004 //
00005 //    Copyright (C) 2014   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 Affero 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 //    Affero General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU Affero 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 "freeling/morfo/language.h"
00035 #include "freeling/morfo/tagset.h"
00036 #include "freeling/regexp.h"
00037 
00038 #define OP_NONE 0
00039 #define OP_AND 1
00040 #define OP_OR 2
00041 
00042 namespace freeling {
00043 
00044   class fex_rulepack;  // predeclaration
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;
00078     std::wstring literal;
00080     std::set<std::wstring> *fileset;
00082     freeling::regexp match_re;
00083 
00085     bool negated;
00087     bool cond_true;
00088 
00090     static const freeling::regexp split_re;
00091 
00093     std::list<std::wstring> get_target(const word &, const tagset&) const;    
00094 
00095   public:
00096     // constructor
00097     fex_condition();
00099     fex_condition(const std::wstring&,const std::wstring&,const std::wstring&,const std::wstring&, 
00100                   std::map<std::wstring,std::set<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     const std::map<std::wstring,const feature_function*> & feat_functs;
00146 
00148     static const freeling::regexp rulepat;
00149     static const freeling::regexp rulepat_anch;
00150     static const freeling::regexp subexpr;
00151     static const freeling::regexp featfun;
00152 
00155     void pattern_instance(const sentence &, int, const tagset &, std::list<std::wstring> &) const;
00156     void get_replacements(const std::wstring &, const word &, const tagset &, std::list<std::wstring> &) const;
00157 
00158   public:
00160     fex_rule (const std::wstring &, const std::wstring &, const std::wstring &, int, 
00161               const std::list<fex_condition> &, const std::map<std::wstring,const feature_function*> &);
00163     fex_rule(const fex_rule &);
00165     fex_rule& operator=(const fex_rule&);
00166 
00167     // get rule id
00168     std::wstring get_id() const;
00171     void precompute(const sentence&, int, const tagset&) const;
00174     void extract(const sentence&, int, int, const tagset&, std::list<std::wstring> &) const;
00176     int get_left() const;
00178     int get_right() const;
00179 
00181     static bool check_conds(const std::list<fex_condition> &, int, const word &, const tagset &, fex_status*);
00182 
00184     void trace(int) const;
00185   };
00186 
00187 
00192 
00193   class fex_rulepack  {
00194 
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