⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 amismodelformat.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
////////////////////////////////////////////////////////////////////////////  Copyright (c) 2000, Yusuke Miyao///  You may distribute under the terms of the Artistic License.//////  <id>$Id: AmisModelFormat.h,v 1.4 2003/05/16 12:10:44 yusuke Exp $</id>///  <collection>Maximum Entropy Estimator</collection>///  <name>AmisModelFormat.h</name>///  <overview>Parser for Amis-style model files</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_AmisModelFormat_h_#define Amis_AmisModelFormat_h_#include <amis/configure.h>#include <amis/ModelFormat.h>#include <amis/Model.h>#include <amis/Tokenizer.h>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>AmisModelFormat</name>/// <overview>Parser for Amis-style model files</overview>/// <desc>/// The class imports model data from Amis-style model files./// The Amis format enables non-binary features./// For details, see README./// </desc>/// <see>Model, ModelFormat</see>/// <body>AMIS_NAMESPACE_BEGINtemplate < FeatureWeightType FW = LAMBDA, class Name = std::string >class AmisModelFormat : public ModelFormat {public:  AmisModelFormat() : ModelFormat() {}  /// Constructor  virtual ~AmisModelFormat() {}  /// Destructor  std::string modelFormatName() const {    return "AmisModel";  }  /// Name of the model format  void inputModel( std::istream& is, ModelBase& model_base ) {    Model< Name >* model = dynamic_cast< Model< Name >* >( &model_base );    if ( model == NULL ) {      throw IllegalModelFormatError( "AmisModelFormat can be used only for Model< ... > class", 0 );    }    inputModel( is, *model );  }  virtual void inputModel( std::istream& is, Model< Name >& model ) {    Tokenizer t( is );    while ( ! t.endOfStream() ) {      std::string feature_str;      if ( ! t.nextToken( feature_str ) ) continue; // empty line      if ( feature_str.find( ':' ) != std::string::npos ) {        throw IllegalModelFormatError( "Feature name cannot contain \":\" in Feature " + feature_str, t.lineNumber() );      }      IStringStream feature_is( feature_str );      Name feature;      feature_is >> feature;      if ( ! feature_is ) {	throw IllegalModelFormatError( "Cannot read feature name: " + feature_str, t.lineNumber() );      }      Real lambda;      if ( ! t.nextToken( lambda ) ) {        OStringStream os;        os << "Lambda value not found for Feature " << feature;        throw IllegalModelFormatError( os.str(), t.lineNumber() );      }      model.newFeature( feature, inputLambda< FW >( lambda ) );      std::string dummy;      if ( t.nextToken( dummy ) ) {        OStringStream os;        os << "Too many tokens for Feature " << feature;        throw IllegalModelFormatError( os.str(), t.lineNumber() );      }    }  }  /// Input model data from an input stream  void outputModel( const ModelBase& model_base, std::ostream& os, int precision = DEFAULT_PRECISION ) const {    const Model< Name >* model = dynamic_cast< const Model< Name >* >( &model_base );    if ( model == NULL ) {      throw IllegalModelFormatError( "AmisModelFormat can be used only for Model< ... > class", 0 );    }    outputModel( *model, os, precision );  }  virtual void outputModel( const Model< Name >& model, std::ostream& os, int precision = DEFAULT_PRECISION ) const {    os.precision( precision );    os.setf( std::ios::scientific );    for ( int i = 0; i < model.numFeatures(); ++i ) {      os << model.featureName( i ) << '\t' << outputLambda< FW >( model.getLambda( i ) ) << '\n';    }  }  /// Output model data into an output stream};AMIS_NAMESPACE_END/// </body>/// </classdef>#endif // AmisModelFormat_h_// end of AmisModelFormat.h

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -