FreeLing  4.0
traces.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 _TRACES
00030 #define _TRACES
00031 
00032 #include <iostream>
00033 #include <string>
00034 #include <list>
00035 #include <cstdlib>
00036 
00037 #include "freeling/windll.h"
00038 #include "freeling/morfo/language.h"
00039 
00041 #define SPLIT_TRACE         0x00000001
00042 #define TOKEN_TRACE         0x00000002
00043 #define MACO_TRACE          0x00000004
00044 #define LANGIDENT_TRACE     0x00000008
00045 #define NUMBERS_TRACE       0x00000010
00046 #define DATES_TRACE         0x00000020
00047 #define PUNCT_TRACE         0x00000040
00048 #define DICT_TRACE          0x00000080
00049 #define AFF_TRACE           0x00000100
00050 #define LOCUT_TRACE         0x00000200
00051 #define NP_TRACE            0x00000400
00052 #define PROB_TRACE          0x00000800
00053 #define QUANT_TRACE         0x00001000
00054 #define NEC_TRACE           0x00002000
00055 #define AUTOMAT_TRACE       0x00004000
00056 #define TAGGER_TRACE        0x00008000
00057 #define SENSES_TRACE        0x00010000
00058 #define CHART_TRACE         0x00020000
00059 #define GRAMMAR_TRACE       0x00040000
00060 #define DEP_TRACE           0x00080000
00061 #define COREF_TRACE         0x00100000
00062 #define UTIL_TRACE          0x00200000
00063 #define WSD_TRACE           0x00400000
00064 #define ALTERNATIVES_TRACE  0x00800000
00065 #define DATABASE_TRACE      0x01000000
00066 #define FEX_TRACE           0x02000000
00067 #define OMLET_TRACE         0x04000000
00068 #define PHONETICS_TRACE     0x08000000
00069 #define MENTIONS_TRACE      0x10000000
00070 #define OUTPUT_TRACE        0x20000000
00071 #define SEMGRAPH_TRACE      0x40000000
00072 
00073 #define SUMMARIZER_TRACE    0x80000000
00074 
00075 // MOD_TRACECODE and MOD_TRACENAME are empty. The class 
00076 // using the trace is expected to set them
00077 #undef MOD_TRACECODE
00078 #undef MOD_TRACENAME
00079 
00080 namespace freeling {
00081 
00085 
00086   class WINDLL traces {
00087   public:
00088     // current trace level
00089     static int TraceLevel;
00090     // modules to trace
00091     static unsigned long TraceModule;
00092 
00093     static void error_crash(const std::wstring &, const std::wstring &);
00094     static void warning(const std::wstring &, const std::wstring &);
00095     static void trace(int,const std::wstring &, const std::wstring &, unsigned long);
00096     static void trace_word (int lv, const word &, const std::wstring &, unsigned long);
00097     static void trace_word_list(int,const std::list<word> &, const std::wstring &, unsigned long);
00098     static void trace_sentence(int,const sentence &, const std::wstring &, unsigned long);
00099     static void trace_sentence_list(int,const std::list<sentence> &, const std::wstring &, unsigned long);
00100   };
00101 
00103   //---------------------------------
00104   inline void traces::error_crash(const std::wstring &msg, const std::wstring &modname) {
00105     std::wcerr<<modname<<L": "<<msg<<std::endl; 
00106     exit(1);
00107   }
00108 
00109   //---------------------------------
00110   inline void traces::warning(const std::wstring &msg, const std::wstring &modname) {
00111     std::wcerr<<modname<<L": "<<msg<<std::endl; 
00112   }
00113 
00114   //---------------------------------
00115   inline void traces::trace(int lv, const std::wstring &msg, const std::wstring &modname, unsigned long modcode) {
00116     if (traces::TraceLevel>=lv && (traces::TraceModule&modcode))
00117       std::wcerr<<modname<<L": "<<msg<<std::endl;
00118   }
00119 
00120 
00121   //---------------------------------
00122   inline void traces::trace_word (int lv, const word &wd, const std::wstring &modname, unsigned long modcode) {
00123     if (traces::TraceLevel>=lv && (traces::TraceModule&modcode)) {
00124 
00125       std::wcerr<<L"Word form ["
00126                 <<wd.get_form()<<L"] ("
00127                 <<wd.get_span_start()<<L","<<wd.get_span_finish()<<L")"<<std::endl;
00128   
00129       for (word::const_iterator an=wd.unselected_begin(); an!=wd.unselected_end(); an++)
00130         std::wcerr<<L"   analysis: <"
00131                   <<an->get_lemma()<<L","
00132                   <<an->get_tag()<<L","
00133                   <<an->get_prob()<<L">"<<std::endl;
00134 
00135       for (word::const_iterator an=wd.selected_begin(); an!=wd.selected_end(); an++) 
00136         std::wcerr<<L"   analysis: <"
00137                   <<an->get_lemma()<<L","
00138                   <<an->get_tag()<<L","
00139                   <<an->get_prob()<<L"> **"<<std::endl;
00140   
00141       if (wd.is_multiword()) { 
00142         std::wcerr<<L"   is a multiword composed by:"<<std::endl;
00143         const std::list<word> & mw = wd.get_words_mw(); 
00144         for (std::list<word>::const_iterator p=mw.begin(); p!=mw.end(); p++) 
00145           std::wcerr<<L"     ("<<p->get_form()<<L")"<<std::endl; 
00146       }
00147     }
00148   }
00149 
00150   //---------------------------------
00151   inline void traces::trace_word_list(int lv, const std::list<word> &wl, const std::wstring &modname, unsigned long modcode) {
00152     if (traces::TraceLevel>=lv && (traces::TraceModule&modcode)) {
00153       for (std::list<word>::const_iterator wd=wl.begin(); wd!=wl.end(); wd++) { 
00154         traces::trace_word(lv, *wd, modname, modcode);
00155       }
00156     }
00157   }
00158 
00159   //---------------------------------
00160   inline void traces::trace_sentence(int lv, const sentence &s, const std::wstring &modname, unsigned long modcode) {
00161      
00162     if (traces::TraceLevel>=lv && (traces::TraceModule&modcode)) {
00163       traces::trace(lv, L"BEGIN sentence",modname,modcode); 
00164       for ( sentence::const_iterator wd=s.begin(); wd!=s.end(); wd++) 
00165         traces::trace_word(lv, *wd, modname, modcode);    
00166       traces::trace(lv,L"END sentence",modname,modcode); 
00167     }
00168   }
00169 
00170   //---------------------------------
00171   inline void traces::trace_sentence_list(int lv, const std::list<sentence> &ls, const std::wstring &modname, unsigned long modcode) {   
00172     if (traces::TraceLevel>=lv && (traces::TraceModule&modcode)) {
00173       for(std::list<sentence>::const_iterator s=ls.begin(); s!=ls.end(); s++) 
00174         traces::trace_sentence(lv,*s,modname,modcode);  
00175     }
00176   }
00177 
00178 
00179 } // namespace
00180 
00181 
00184 
00185 //#define ERROR_CRASH(msg) freeling::traces::error_crash(msg,MOD_TRACENAME)
00186 
00187 #define ERROR_CRASH(msg) { std::wcerr<<MOD_TRACENAME<<L": "<<msg<<std::endl; \
00188                            exit(1); \
00189                          }
00190  
00193 #ifdef NO_WARNINGS
00194 #define WARNING(msg)
00195 #else
00196 //#define WARNING(msg) freeling::traces::warning(msg,MOD_TRACENAME)
00197 #define WARNING(msg)  { std::wcerr<<MOD_TRACENAME<<L": "<<msg<<std::endl; }
00198 #endif
00199 
00202 #ifdef VERBOSE   
00203 
00204 //#define TRACE(x,y) freeling::traces::trace(x,y,MOD_TRACENAME,MOD_TRACECODE)
00205 //#define TRACE_WORD(x,y) freeling::traces::trace_word(x,y,MOD_TRACENAME,MOD_TRACECODE)
00206 //#define TRACE_WORD_LIST(x,y) freeling::traces::trace_word_list(x,y,MOD_TRACENAME,MOD_TRACECODE)
00207 //#define TRACE_SENTENCE(x,y) freeling::traces::trace_sentence(x,y,MOD_TRACENAME,MOD_TRACECODE)
00208 //#define TRACE_SENTENCE_LIST(x,y) freeling::traces::trace_sentence_list(x,y,MOD_TRACENAME,MOD_TRACECODE)
00209 
00210 #define TRACE(lv,msg) { if (freeling::traces::TraceLevel>=lv && (freeling::traces::TraceModule&MOD_TRACECODE)) { \
00211                           std::wcerr << MOD_TRACENAME << L": " << msg << std::endl; \
00212                         } \
00213                       }
00214                       
00215 
00216 #define TRACE_WORD(lv,wd) { if (freeling::traces::TraceLevel>=lv && (freeling::traces::TraceModule&MOD_TRACECODE)) { \
00217                               TRACE(lv, L"Word form ["<<wd.get_form()<<L"] ("<<wd.get_span_start()<<L","<<wd.get_span_finish()<<L")"); \
00218                               for (word::const_iterator an=wd.unselected_begin(); an!=wd.unselected_end(); an++) \
00219                                 TRACE(lv, L"   analysis: <"<<an->get_lemma()<<L","<<an->get_tag()<<L","<<an->get_prob()<<L">"); \
00220                               for (word::const_iterator an=wd.selected_begin(); an!=wd.selected_end(); an++) \
00221                                 TRACE(lv, L"   analysis: <"<<an->get_lemma()<<L","<<an->get_tag()<<L","<<an->get_prob()<<L"> **");  \
00222                               if (wd.is_multiword()) {   \
00223                                 TRACE(lv, L"   is a multiword composed by:"); \
00224                                 const std::list<word> & mw = wd.get_words_mw(); \
00225                                 for (std::list<word>::const_iterator p=mw.begin(); p!=mw.end(); p++) \
00226                                   TRACE(lv, L"     ("<<p->get_form()<<L")"); \
00227                               } \
00228                             } \
00229                           }
00230 
00231 #define TRACE_WORD_LIST(lv,wl) { if (freeling::traces::TraceLevel>=lv && (freeling::traces::TraceModule&MOD_TRACECODE)) { \
00232                                    for (std::list<word>::const_iterator wd=wl.begin(); wd!=wl.end(); wd++) { \
00233                                      TRACE_WORD(lv, (*wd));             \
00234                                    } \
00235                                  } \
00236                                }
00237 
00238 #define TRACE_SENTENCE(lv,s)  { if (freeling::traces::TraceLevel>=lv && (freeling::traces::TraceModule&MOD_TRACECODE)) { \
00239                                    TRACE(lv,L"BEGIN sentence"); \
00240                                    TRACE_WORD_LIST(lv,s); \
00241                                    TRACE(lv,L"END sentence"); \
00242                                  } \
00243                               }
00244                   
00245 #define TRACE_SENTENCE_LIST(lv,ls) { if (freeling::traces::TraceLevel>=lv && (freeling::traces::TraceModule&MOD_TRACECODE)) { \
00246                                        for (std::list<sentence>::const_iterator s=ls.begin(); s!=ls.end(); s++) \
00247                                          TRACE_SENTENCE(lv,(*s)); \
00248                                      } \
00249                                    }
00250 
00251 #else
00252 
00253 #define TRACE(x,y)
00254 #define TRACE_WORD(x,y)
00255 #define TRACE_WORD_LIST(x,y)
00256 #define TRACE_SENTENCE(x,y)
00257 #define TRACE_SENTENCE_LIST(x,y)
00258 #endif
00259 
00260 #endif