/////////////////////////////////////////////////////////////////////////////// // // // This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. // // // // ModelBlocks is free software: you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation, either version 3 of the License, or // // (at your option) any later version. // // // // ModelBlocks is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public License // // along with ModelBlocks. If not, see . // // // // ModelBlocks developers designate this particular file as subject to // // the "Moses" exception as provided by ModelBlocks developers in // // the LICENSE file that accompanies this code. // // // /////////////////////////////////////////////////////////////////////////////// #ifndef _NL_RACPT__ #define _NL_RACPT__ template class GenericRACPTModel : public SimpleHash { private: typedef SimpleHash HKP; // typedef typename SimpleHash::const_iterator IYP; //HKYP h; public: //typedef Y RVType; //typedef BaseIterVal,Y> IterVal; //typedef typename HKYP::const_iterator const_key_iterator; bool contains ( const K& k ) const { return ( SimpleHash::contains(k) ); } /* P getProb ( const IterVal& ikyp, const K& k ) const { if ( ikyp.iter.first == ikyp.iter.second ) { cerr<<"ERROR: no iterator to fix probability: "<second ); } */ P getProb ( const K& k ) const { return SimpleHash::get(k); } P& setProb ( const K& k ) { return SimpleHash::set(k); } /* void normalize ( ) { for ( typename HKYP::const_iterator ik=HKYP::begin(); ik!=HKYP::end(); ik++ ) { K k=ik->first; P p=P(); IterVal y; for(bool by=setFirst(y,k); by; by=setNext(y,k)) p+=getProb(y,k); if (p!=P()) for(bool by=setFirst(y,k); by; by=setNext(y,k)) setProb(y,k)/=p; } } */ /* void transmit ( int tSockfd, const char* psId ) const { for ( typename HKYP::const_iterator ik=HKYP::begin(); ik!=HKYP::end(); ik++ ) { K k=ik->first; IterVal y; // For each non-zero probability in model... for ( bool b=setFirst(y,k); b; b=setNext(y,k) ) { //if ( getProb(y,k) != P() ) { String str(1000); str<first; os << psId<<" "<& m ) { for ( typename HKP::const_iterator ik=m.HKP::begin(); ik!=m.HKP::end(); ik++ ) { K k=ik->first; setProb(k) = m.getProb(k); // IterVal y; // for ( bool b=m.setFirst(y,k); b; b=m.setNext(y,k) ) // setProb(y,k) = m.getProb(y,k); } } void clear ( ) { SimpleHash::clear(); } friend pair*> operator>> ( StringInput si, GenericRACPTModel& m ) { return pair*>(si,&m); } friend StringInput operator>> ( pair*> delimbuff, const char* psD ) { K k; StringInput si,si2,si3; GenericRACPTModel& m = *delimbuff.second; si=delimbuff.first; if ( si==NULL ) return si; // Kill the colon since we're treating the whole thing as the condition char * str = si.c_str(); char * p = strchr(str, ':'); if(p){ p[0] = ' '; } si=str; while((si2=si>>" ")!=NULL)si=si2; si=si>>k>>" "; while((si2=si>>" ")!=NULL)si=si2; si=si>>"= "; while((si2=si>>" ")!=NULL)si=si2; return (si!=NULL) ? si>>m.setProb(k)>>psD : si; } }; template class RandAccCPT1DModel : public GenericRACPTModel,P> { public: // typedef typename GenericCPTModel,P>::IterVal IterVal; bool contains ( const Y& y ) const { return GenericRACPTModel,P>::contains ( MapKey1D(y) ); } /* P getProb ( const IterVal& ixyp ) const { return GenericCPTModel,P>::getProb ( ixyp, MapKey1D(Y()) ); } */ P getProb ( const Y& y ) const { return GenericRACPTModel,P>::getProb ( MapKey1D(y) ); } /* P& setProb ( const Y& y ) { cerr << "setProb called on racpt1d" << endl; return GenericRACPTModel,P>::setProb ( MapKey1D(y) ); } */ /* bool readFields ( Array& aps ) { if ( 3==aps.size() ) { GenericRACPTModel,P>::setProb ( MapKey1D(aps[1]) ) = atof(aps[2]); return true; } return false; } */ }; //////////////////// template class RandAccCPT2DModel : public GenericRACPTModel,P> { public: // This stuff only for deterministic 'Determ' models... // typedef X1 Dep1Type; // typedef P ProbType; // MapKey1D condKey; bool contains ( const Y& y, const X1& x1 ) const { // MapKey2D temp = MapKey2D(x1,y); return GenericRACPTModel,P>::contains ( MapKey2D(x1,y) ); } P getProb ( const Y& y, const X1& x1 ) const { return GenericRACPTModel,P>::getProb ( MapKey2D(x1,y) ); } /* P& setProb ( const Y& y, const X1& x1 ) { cerr << "setProb called on racpt2d" << endl; return GenericRACPTModel,P>::setProb ( MapKey2D(y,x1) ); } */ }; //////////////////// template class RandAccCPT3DModel : public GenericRACPTModel,P> { public: bool contains ( const Y& y, const X1& x1, const X2& x2 ) const { return GenericRACPTModel,P>::contains ( MapKey3D(x1,x2,y) ); } P getProb ( const Y& y, const X1& x1, const X2& x2 ) const { return GenericRACPTModel,P>::getProb ( MapKey3D(x1,x2,y) ); } /* P& setProb ( const Y& y, const X1& x1, const X2& x2 ) { return GenericRACPTModel,P>::setProb ( MapKey3D(x1,x2,y) ); } */ }; /* //////////////////// template class CPT4DModel : public GenericCPTModel,P> { public: typedef typename GenericCPTModel,P>::IterVal IterVal; bool setFirst ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3 ) const { return GenericCPTModel,P>::setFirst ( ixyp, MapKey3D(x1,x2,x3) ); } bool setNext ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3 ) const { return GenericCPTModel,P>::setNext ( ixyp, MapKey3D(x1,x2,x3) ); } bool contains ( const Y& y, const X1& x1, const X2& x2, const X3& x3 ) const { return GenericCPTModel,P>::contains ( y, MapKey3D(x1,x2,x3) ); } bool contains ( const X1& x1, const X2& x2, const X3& x3 ) const { return GenericCPTModel,P>::contains ( MapKey3D(x1,x2,x3) ); } P getProb ( const IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3 ) const { return GenericCPTModel,P>::getProb ( ixyp, MapKey3D(x1,x2,x3) ); } P getProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3 ) const { return GenericCPTModel,P>::getProb ( y, MapKey3D(x1,x2,x3) ); } P& setProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3 ) { return GenericCPTModel,P>::setProb ( y, MapKey3D(x1,x2,x3) ); } bool readFields ( Array& aps ) { if ( 6==aps.size() ) { GenericCPTModel,P>::setProb ( Y(aps[4]), MapKey3D(aps[1],aps[2],aps[3]) ) = atof(aps[5]); return true; } return false; } }; //////////////////// template class CPT5DModel : public GenericCPTModel,P> { public: typedef typename GenericCPTModel,P>::IterVal IterVal; bool setFirst ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const { return GenericCPTModel,P>::setFirst ( ixyp, MapKey4D(x1,x2,x3,x4) ); } bool setNext ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const { return GenericCPTModel,P>::setNext ( ixyp, MapKey4D(x1,x2,x3,x4) ); } bool contains ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const { return GenericCPTModel,P>::contains ( y, MapKey4D(x1,x2,x3,x4) ); } bool contains ( const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const { return GenericCPTModel,P>::contains ( MapKey4D(x1,x2,x3,x4) ); } P getProb ( const IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const { return GenericCPTModel,P>::getProb ( ixyp, MapKey4D(x1,x2,x3,x4) ); } P getProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) const { return GenericCPTModel,P>::getProb ( y, MapKey4D(x1,x2,x3,x4) ); } P& setProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4 ) { return GenericCPTModel,P>::setProb ( y, MapKey4D(x1,x2,x3,x4) ); } bool readFields ( Array& aps ) { if ( 7==aps.size() ) { GenericCPTModel,P>::setProb ( Y(aps[5]), MapKey4D(aps[1],aps[2],aps[3],aps[4]) ) = atof(aps[6]); return true; } return false; } }; //////////////////// template class RACPT6DModel : public GenericCPTModel,P> { public: typedef typename GenericCPTModel,P>::IterVal IterVal; bool setFirst ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const { return GenericCPTModel,P>::setFirst ( ixyp, MapKey5D(x1,x2,x3,x4,x5) ); } bool setNext ( IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const { return GenericCPTModel,P>::setNext ( ixyp, MapKey5D(x1,x2,x3,x4,x5) ); } bool contains ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const { return GenericCPTModel,P>::contains ( y, MapKey5D(x1,x2,x3,x4,x5) ); } bool contains ( const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const { return GenericCPTModel,P>::contains ( MapKey5D(x1,x2,x3,x4,x5) ); } P getProb ( const IterVal& ixyp, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const { return GenericCPTModel,P>::getProb ( ixyp, MapKey5D(x1,x2,x3,x4,x5) ); } P getProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) const { return GenericCPTModel,P>::getProb ( y, MapKey5D(x1,x2,x3,x4,x5) ); } P& setProb ( const Y& y, const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5 ) { return GenericCPTModel,P>::setProb ( y, MapKey5D(x1,x2,x3,x4,x5) ); } bool readFields ( Array& aps ) { if ( 8==aps.size() ) { GenericCPTModel,P>::setProb ( Y(aps[6]), MapKey5D(aps[1],aps[2],aps[3],aps[4],aps[5]) ) = atof(aps[7]); return true; } return false; } }; */ #endif //_NL_RACPT__