FreeLing  4.0
hmm_tagger.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 _TAGGER
00030 #define _TAGGER
00031 
00032 #include <map>
00033 #include <list>
00034 #include <set>
00035 
00036 #include "freeling/windll.h"
00037 #include "freeling/safe_map.h"
00038 #include "freeling/morfo/language.h"
00039 #include "freeling/morfo/tagger.h"
00040 #include "freeling/morfo/tagset.h"
00041 
00042 namespace freeling {
00043 
00054 
00055   typedef std::pair<std::wstring, std::wstring> bigram;
00056 
00057   class trellis {     
00058   private:
00059 
00062     class element {
00063     public:
00065       bigram state;
00067       int kbest;
00069       double prob;
00070 
00072       element(const bigram &, int, double);
00074       ~element();
00076       bool operator<(const element &) const;
00077       bool operator>(const element &) const;
00079       bool operator==(const element &) const;
00080     };
00081 
00085     typedef std::multiset<element,std::greater<element> > path_elements;
00086     std::vector<std::map <bigram, path_elements > > trl;
00087 
00089     unsigned int kbest;
00090 
00091   public:
00093     trellis(int, unsigned int kb=1);
00095     ~trellis();
00096 
00098     void insert(int, const bigram &, const bigram &, int kb, double);
00100     double delta(int, const bigram &, unsigned int k=0) const;
00102     std::pair<bigram, int> phi(int, const bigram &, unsigned int k=0) const;
00104     int nbest(int, const bigram &) const;
00105 
00107     static const float ZERO_logprob;
00108     static const bigram initState;
00109     static const bigram endState;
00110   };
00111 
00112 
00120 
00121   class emission_states: public std::set<bigram > {};
00122 
00129 
00130   class WINDLL hmm_tagger: public POS_tagger {
00131   private:
00132     // Tagset description
00133     const tagset *Tags;
00134 
00136     std::map <std::wstring, double> PTag;
00137     std::map <bigram, double> PBg;
00138     std::map <std::wstring, double> PTrg;
00139     std::map <bigram, double> PInitial;
00140     std::map <std::wstring, double> PWord;
00141 
00143     std::multimap <std::wstring, std::wstring> Forbidden;
00144 
00145     // probability for unobserved sentence initial trigrams
00146     double probInitial;
00147     // probability for unobserved words  
00148     double probUnobserved;
00149 
00151     safe_map<std::wstring,double> *pA_cache;
00152     //prob_cache *pB_cache;
00153 
00155     unsigned int kbest;
00156 
00158     double c[3];
00159 
00160     bool is_forbidden(const std::wstring &, sentence::const_iterator) const;
00161     double ProbA_log(const bigram &, const bigram &, sentence::const_iterator) const;
00162     double ProbB_log(const bigram &, const word &) const;
00163     double ProbPi_log(const bigram &) const;
00164 
00166     std::list<emission_states> FindStates(const sentence &) const;
00167 
00168   public:
00170     hmm_tagger(const std::wstring &, bool, unsigned int, unsigned int kb=1);
00172     ~hmm_tagger();
00173 
00175     void annotate(sentence &) const;
00178     double SequenceProb_log(const sentence &, int k=0) const;
00179 
00180   };
00181 
00182 } // namespace
00183 
00184 #endif
00185 
00186