FreeLing  4.0
dep_rules.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 _DEPRULES
00030 #define _DEPRULES
00031 
00032 #include <sstream>
00033 #include <iostream>
00034 #include <set>
00035 #include <list>
00036 
00037 #include "freeling/regexp.h"
00038 #include "freeling/morfo/language.h"
00039 #include "freeling/morfo/semdb.h"
00040 
00041 namespace freeling {
00042   
00043   class dep_txala;
00044 
00048 
00049   class matching_attrib {
00050   public:
00052     std::wstring type;
00054     std::wstring value;
00056     freeling::regexp re;
00057 
00059     matching_attrib();
00061     matching_attrib(const matching_attrib &);
00063     ~matching_attrib();     
00064   };
00065 
00071 
00072   class matching_condition {
00073   public:
00074     bool neg;
00075     std::wstring label;
00076     std::list<matching_attrib> attrs;
00077   };
00078 
00079 
00086 
00087   class completer_rule {
00088  
00089   public:
00092     std::wstring line;
00093 
00095     std::wstring leftChk;
00096     std::wstring rightChk;
00098     matching_condition leftConds;
00099     matching_condition rightConds;
00100 
00102     std::wstring pairClass;
00104     std::wstring node1;
00105     std::wstring attr1;
00106     std::wstring node2;
00107     std::wstring attr2;
00108 
00110     std::wstring newNode1;
00111     std::wstring newNode2;
00113     matching_condition matchingCond;
00115     std::wstring operation;
00117     std::vector<matching_condition> leftContext;
00118     std::vector<matching_condition> rightContext;
00120     bool context_neg;
00122     int weight;
00123 
00125     std::set<std::wstring> enabling_flags;
00127     std::set<std::wstring> flags_toggle_on;
00129     std::set<std::wstring> flags_toggle_off;
00130    
00132     completer_rule();
00133     completer_rule(const std::wstring &, const std::wstring &, const std::wstring&);
00134     completer_rule( const completer_rule &);
00136     completer_rule & operator=( const completer_rule &);
00137    
00139     int operator<(const completer_rule & a ) const;
00140   };
00141 
00142 
00143 
00144 
00148 
00149   class rule_expression {
00150   friend class dep_txala;
00151 
00152   protected:
00153     static void parse_node_ref(std::wstring, dep_tree::const_iterator, std::list<dep_tree::const_iterator> &);
00154     static void parse_node_ref(std::wstring, parse_tree::const_iterator, std::list<parse_tree::const_iterator> &);
00155 
00156     // node the expression has to be checked against (p/d)
00157     std::wstring node1,node2;
00158     // set of values (if any) to check against.
00159     std::set<std::wstring> valueList;
00160     // obtain the iterator to the nodes to be checked 
00161     // and the operation AND/OR to perform
00162     bool nodes_to_check(const std::wstring &node, dep_tree::const_iterator, dep_tree::const_iterator, std::list<dep_tree::const_iterator> &) const;
00163 
00164   public:
00165     // types of operations that the conditions may perform
00166     typedef enum {AND,NOT,SIDE,LEMMA,POS,LABEL,WORDCLASS,TONTO,SEMFILE,SYNON,ASYNON,PAIRCLASS} expression_type;
00167 
00168     // constructors and destructors
00169     rule_expression();
00170     rule_expression(expression_type t);
00171     rule_expression(expression_type t, const rule_expression &re);
00172     rule_expression(expression_type t, const std::wstring &nd, const std::wstring &val);
00173     rule_expression(expression_type t, const std::wstring &nd, const std::wstring &val, const std::set<std::wstring> &wclas);
00174     rule_expression(expression_type t, const std::wstring &nd1, const std::wstring &nd2, const std::wstring &val, const std::set<std::wstring> &pairclas);
00175     rule_expression(expression_type t, const std::wstring &nd, const std::wstring &val, const semanticDB &sdb);
00176     ~rule_expression();
00177 
00178     // add a subexpression to the list
00179     void add(const rule_expression &re);
00180     // set expression type;
00181     void set_type(expression_type t);
00182 
00183     // search a value in expression list
00184     bool find(const std::wstring &) const;
00185     bool find_match(const std::wstring &) const;
00186     bool match(const std::wstring &) const;
00187     bool find_any(const std::list<std::wstring> &) const;
00188     bool find_any_match(const std::list<std::wstring> &) const;
00189 
00190     // evaluate expressions involving parent and daughter (AND, NOT, A.x, E.x, side)
00191     bool check(dep_tree::const_iterator, dep_tree::const_iterator) const;
00192     // evaluate expressions involving one node and one constant value (lemma, class, pos, label, tonto, semfile, synon, asynon)
00193     bool eval(dep_tree::const_iterator n1, dep_tree::const_iterator n2=(const dep_tree*)NULL) const;
00194 
00195   private:
00196     // type of the expression
00197     expression_type type;
00198     // list of subexpressions (many for type=AND, one for type=NOT, empty for others)
00199     std::list<rule_expression> check_list;
00200     // word class set from labeler we belong to (only needed for type=WORDCLASS)
00201     const std::set<std::wstring> *wordclasses;
00202     // pair class set from labeler we belong to (only needed for type=PAIRCLASS)
00203     const std::set<std::wstring> *pairclasses;
00204     // semantic DB  from labeler we belong to (only needed for type=TONTO,SEMFILE,SYNON,ASYNON)
00205     const semanticDB *semdb;
00206  
00208     std::wstring extract_value(const std::wstring &fun, dep_tree::const_iterator n) const;
00209        
00210   };
00211 
00215 
00216   class labeler_rule {
00217 
00218   public:
00219     std::wstring label;
00220     rule_expression re;
00221     std::wstring ancestorLabel;
00223     std::wstring line;
00224 
00225     labeler_rule();
00226     labeler_rule(const std::wstring &, const rule_expression &);
00227     ~labeler_rule();
00228     bool check(dep_tree::const_iterator, dep_tree::const_iterator) const;
00229   };
00230 
00231 } // namespace
00232 
00233 #endif