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

📄 amiseventformat.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
////////////////////////////////////////////////////////////////////////////  Copyright (c) 2000, Yusuke Miyao///  You may distribute under the terms of the Artistic License.//////  <id>$Id: AmisEventFormat.h,v 1.4 2003/05/16 05:11:05 yusuke Exp $</id>///  <collection>Maximum Entropy Estimator</collection>///  <name>AmisEventFormat.h</name>///  <overview>Parser for Amis-style event files</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_AmisEventFormat_h_#define Amis_AmisEventFormat_h_#include <amis/configure.h>#include <amis/EventFormat.h>#include <amis/Model.h>#include <amis/EventSpace.h>#include <amis/Tokenizer.h>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>AmisEventFormat</name>/// <overview>Parser for Amis-style event files</overview>/// <desc>/// The class imports event data from Amis-style data files./// The Amis format enables non-binary features and packed events./// For details, see README./// </desc>/// <see>Event, EventSpace, EventFormat</see>/// <body>template < class Feature, class Name = std::string >class AmisEventFormat : public EventFormat {public:  typedef typename Feature::FeatureFreq FeatureFreq;  static FeatureFreq extractFreq( std::string& feature ) {    std::string::size_type pos = feature.find( ':' );    if ( pos == std::string::npos ) return 1;    FeatureFreq freq;    str2FeatureFreq( feature.substr( pos + 1 ), freq );    feature.erase( pos );    //string tmp_s( feature.substr(0, pos) );    //feature = tmp_s;    //cerr << "feature=" << feature << std::endl;    return freq;  }  /// Extract frequency from the string description of a featureprivate:  std::string current_event;protected:  static void str2FeatureFreq( const std::string& s, int& f ) {    f = Tokenizer::str2int( s );  }  /// Convert a string into feature frequency  static void str2FeatureFreq( const std::string& s, Real& f ) {    f = Tokenizer::str2Real( s );  }  /// Convert a string into feature frequency    void inputFeatureList( const Model< Name >& model, Tokenizer& t, std::vector< Feature >& fl ) const {    std::string feature_str;    while ( t.nextToken( feature_str ) ) {      FeatureFreq freq = extractFreq( feature_str );      if ( freq <= static_cast< FeatureFreq >( 0 ) ) {        throw IllegalEventFormatError( "Feature frequency must be positive in Event " + current_event,                                      t.lineNumber() );      }      IStringStream feature_is( feature_str );      Name feature;      feature_is >> feature;      fl.push_back( Feature( model.featureID( feature ), freq ) );    }  }  /// Input a feature listpublic:  AmisEventFormat() : EventFormat(), current_event() {  }  /// Constructor  virtual ~AmisEventFormat() {}  /// Destructor  std::string eventFormatName() const {    return "AmisEvent";  }  /// Name of the event format  bool inputEvent( std::istream& s,			   const Model< Name >& model,			   Event< Feature >& event ) {    Tokenizer t( s );    return inputEvent( t, model, event );  }  /// Input an event from stream  bool inputEvent( Tokenizer& t,                           const Model< Name >& model,                           Event< Feature >& event ) {    if ( ! t.nextToken( current_event ) ) return false; // empty line    std::string dummy;    Real prob = 0.0;    if ( t.nextToken( dummy ) ) {      prob = t.str2Real( dummy );      if ( t.nextToken( dummy ) ) {        throw IllegalEventFormatError( "Too many tokens found in Event " + current_event, t.lineNumber() );      }    } else {      prob = 1.0;    }#ifdef AMIS_JOINT_PROB    event.setEventProbability( prob );#endif // AMIS_JOINT_PROB    while ( true ) {      FeatureListFreq freq;      if ( ! t.nextToken( freq ) ) break;      if ( freq < 0 ) {        throw IllegalEventFormatError( "Feature list frequency must be positive or zero in Event " + current_event, t.lineNumber() );      }      //cerr << "freq = " << freq << std::endl;      std::vector< Feature > fl;      inputFeatureList( model, t, fl );      event.addFeatureList( fl, freq );    }    return true;  }  void inputEventSpace( std::istream& s, const ModelBase& model_base, EventSpaceBase& event_space_base ) {    const Model< Name >* model = dynamic_cast< const Model< Name >* >( &model_base );    if ( model == NULL ) {      throw IllegalEventFormatError( "AmisEventFormat can be used only for Model< ... > class", 0 );    }    EventSpace< Feature, Event< Feature > >* event_space =      dynamic_cast< EventSpace< Feature, Event< Feature > >* >( &event_space_base );    if ( event_space == NULL ) {      throw IllegalEventFormatError( "AmisEventFormat can be used only for EventSpace class", 0 );    }    inputEventSpace( s, *model, *event_space );  }  /// Interface defined by class EventFormat  virtual void inputEventSpace( std::istream& s,				const Model< Name >& model,				EventSpace< Feature, Event< Feature > >& event_space ) {    Tokenizer t( s );    AMIS_DEBUG_MESSAGE( 3, "\nInput events...\n" );    AMIS_DEBUG_MESSAGE( 5, "\t----------------------------------------\n" );    AMIS_DEBUG_MESSAGE( 5, "\tEvent\tFreq.\t# FLs\n" );    while ( ! t.endOfStream() ) {      Event< Feature > event;      if ( inputEvent( t, model, event ) ) {        AMIS_DEBUG_MESSAGE( 5, '\t' << current_event << '\t' << event.eventFrequency() << '\t' << event.numFeatureList() + 1 << '\n' );        try {          event_space.addEvent( event );        } catch ( IllegalEventError e ) {          throw IllegalEventFormatError( e.message() + " in Event " + current_event, t.lineNumber() );        }      }    }    //cerr << "Number of events = " << event_space->numEvents() << std::endl;    //cerr << "Count  of events = " << event_space->sumEventCount() << std::endl;    AMIS_DEBUG_MESSAGE( 5, "\t----------------------------------------\n" );  }  /// Input event data from an input stream};/// </body>/// </classdef>AMIS_NAMESPACE_END#endif // Amis_AmisEventFormat_h_// end of AmisEventFormat.h

⌨️ 快捷键说明

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