📄 amisdriver.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: AmisDriver.h,v 1.15 2003/05/16 05:11:05 yusuke Exp $/// <collection>Maximum Entropy Estimator</collection>/// <name>AmisDriver.h</name>/// <overview>Interface for running amis</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_AmisDriver_h_#define Amis_AmisDriver_h_#include <amis/configure.h>#include <amis/Property.h>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>ShowHelpMessage</name>/// <overview>Notification of showing help messages</overview>/// <body>class ShowHelpMessage : public ErrorBase {private: bool is_heavy;public: ShowHelpMessage( const std::string& m, bool h = false ) : ErrorBase( m ) { is_heavy = h; } /// Initialize with a message and "heavy" status ShowHelpMessage( const char* m, bool h = false ) : ErrorBase( m ) { is_heavy = h; } /// Initialize with a message and "heavy" status bool isHeavy() const { return is_heavy; } /// Whether to print the heavy message};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>ShowVersion</name>/// <overview>Notification of showing version</overview>/// <body>class ShowVersion : public ErrorBase {public: ShowVersion( const std::string& m ) : ErrorBase( m ) {} /// Initialize with a message ShowVersion( const char* m ) : ErrorBase( m ) {} /// Initialize with a error message};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>ShowOptionStatus</name>/// <overview>Notification of showing option status</overview>/// <body>class ShowOptionStatus : public ErrorBase {public: ShowOptionStatus( const std::string& m ) : ErrorBase( m ) {} /// Initialize with a message ShowOptionStatus( const char* m ) : ErrorBase( m ) {} /// Initialize with a error message};/// </body>/// </classdef>//////////////////////////////////////////////////////////////////////class AmisDriver {public: static const int AMIS_DEBUG_TYPE; static const int AMIS_PROFILE_TYPE; static const std::string AMIS_FEATURE_WEIGHT_TYPE; static const std::string AMIS_PARALLEL_TYPE; static const std::string AMIS_REAL_TYPE; static const std::string AMIS_USE_TAO_TYPE; //////////////////////////////////////////////////////////////////////private: Property property;public: AmisDriver() {} /// Constructor virtual ~AmisDriver() {} /// Destructorpublic: std::string amisName() const; /// Get the name, version and status of amis const Property& getProperty() { return property; } /// Get the property used by this AmisDriver //////////////////////////////////////////////////////////////////////public: virtual int run( int argc, const char** argv ); /// Main routine to run amis (using the following functions) virtual void importProperty( int argc, const char** argv ); /// Parse command-line arguments and import a property file virtual void initialize(); /// Initialize amis virtual void showStartup(); /// Show startup messages virtual ModelPtr newModel() { ModelPtr model = property.newModel(); // model->setFeatureFreezeType( property.featureFreezeType() ); // model->setFeatureFreezeThreshold( property.featureFreezeThreshold() ); // if(property.eventType() == Property::FIXKERNEL) { // Model* om = // property.newOrgModel(); // no effect if not using kernel method // } return model; } /// Get a new model specified by the property virtual EventSpacePtr newEventSpace() { EventSpacePtr event_space = property.newEventSpace(); return event_space; } /// Get a new event space specified by the property virtual ModelFormatPtr newModelFormat() { ModelFormatPtr model_format = property.newModelFormat(); return model_format; } /// Get a new model format specified by the property virtual EventFormatPtr newEventFormat() { EventFormatPtr event_format = property.newEventFormat(); return event_format; } /// Get a new event format specified by the property virtual EstimatorPtr newEstimator() { EstimatorPtr estimator = property.newEstimator(); return estimator; } /// Get a new estimator specified by the property virtual void inputModel( const ModelFormatPtr& format, ModelPtr& model ); /// Input a model by using the model format virtual void inputEventSpace( const EventFormatPtr& format, const ModelPtr& model, EventSpacePtr& event_space ); /// Input events by using the event format virtual void estimation( ModelPtr& model, EventSpacePtr& event_space, EstimatorPtr& estimator ); /// Start estimation virtual void outputModel( const ModelFormatPtr& format, const ModelPtr& model ); /// Output the estimated model virtual void outputStatistics( const ModelPtr& model, const ModelStatPtr& model_stat, const ProfTimer& total_timer, const ProfTimer& estimation_timer ); /// Output the statistics of the model and event space (not implemented yet) virtual void finalize(); /// Finalize the data virtual void showEnding(); /// Show ending messages};/*//////////////////////////////////////////////////////////////////////// Main routineclass AmisDriver { // read an event from an input stream, and set prodcuts of it // The most general API for runtime prediction. An event in the stream will be interpreted in the same way as in loading the event file. bool setProducts( std::istream& is, std::vector< std::pair<Name, Real> >& to ) const { if( property.eventType() == Property::FIX || property.eventType() == Property::FIXKERNEL ) { const ModelFix* m = dynamic_cast<const ModelFix*>( getModel() ); std::vector< Real > prods; //cerr << "prods.size()=" << prods.size() << "\n"; bool suc = const_cast<EventSpaceBase*>( getEventSpace() )->setProducts( is, prods ); if(!suc) { return false; } to.resize( 0 ); //cerr << "prods.size()=" << prods.size() << "\n" << flush; for( int i = 0; i < prods.size(); i++) { to.push_back( std::pair<Name, Real>( m->targetName( i ), prods[i] ) ); } return true; } else { // In this case, the concept of "target" is not exist. // Occasionally, i-th feature list is named "i" std::vector< Real > prods; bool suc = const_cast<EventSpaceBase*>( getEventSpace() )->setProducts( is, prods ); if(!suc) { return false; } to.resize( 0 ); for( int i = 0; i < prods.size(); i++) { Name name; NameConvert<int>::toName(i, name); // give a pseudo target name to.push_back( std::pair<Name, Real>( name, prods[i] ) ); } return true; } } // API for runtime prediction specialized for AmisFix and AmisFixKernel models. // Efficient since there is no stream interpretation. bool setProductsFix( const std::vector< std::pair<Name, Real> >& feats, std::vector< std::pair<Name, Real> >& to ) const { if( property.eventType() == Property::FIX || property.eventType() == Property::FIXKERNEL ) { const ModelFix* m = dynamic_cast<const ModelFix*>( getModel() ); bool suc = const_cast<EventSpaceBase*>( getEventSpace() )->setProductsFix( feats, to ); if(!suc) { return false; } return true; } else { AMIS_ABORT( "AmisDriver::setProcutsFix(...) is available only for AmisFix and AmisFixKernel models" ); } } // API for runtime preciction specialized for AmisFix and AmisFixKernel. // Useful in the case that your application needs classification of many events with slight differences (e.g., POS tagging using preceeding POS feature and the Viterbi search). // Give the common part as "static_features" and the differences as "dynamic_features_vec" bool setProductsFixPacked( const std::vector< std::pair<Name, Real> >& static_feats, const std::vector < std::vector< std::pair<Name,Real> > >& dynamic_feats_vec, std::vector< std::vector< std::pair<Name, Real> > >& to ) const { if( property.eventType() == Property::FIX || property.eventType() == Property::FIXKERNEL ) { bool suc = const_cast<EventSpaceBase*>( getEventSpace() )->setProductsFixPacked( static_feats, dynamic_feats_vec, to ); if(!suc) { return false; } return true; } else { AMIS_ABORT( "AmisDriver::setProcutsFixPacked(...) is available only for AmisFix and AmisFixKernel models" ); } } };*/AMIS_NAMESPACE_END#endif // Amis_AmisDriver_h_// end of AmisDriver.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -