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

📄 featurefreqarray.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
////////////////////////////////////////////////////////////////////////////  Copyright (c) 2000, Yusuke Miyao///  You may distribute under the terms of the Artistic License.//////  <id>$Id: FeatureFreqArray.h,v 1.6 2003/05/16 13:03:46 yusuke Exp $</id>///  <collection>Maximum Entropy Estimator</collection>///  <name>FeatureFreqArray.h</name>///  <overview>Array of feature frequencies</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_FeatureFreqArray_h_#define Amis_FeatureFreqArray_h_#include <amis/configure.h>#include <amis/Real.h>//#include <hash_map>#include <map>#include <vector>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>FeatureFreqValue</name>/// <overview>A feature frequency</overview>/// <desc>A feature frequency</desc>/// <body>class FeatureFreqValue {public:  //typedef typename Feature::FeatureFreq FeatureFreq;private:  Real value;public:  FeatureFreqValue( void ) {    value = 0.0;  }  ~FeatureFreqValue() {}  static const std::string featureFreqArrayName( void ) { return "FeatureFreqValue"; }  /// Get the name of this class  template < class FeatureFreq >  void reserve( FeatureFreq ) {    // do nothing  }  void clear( void ) {    value = 0.0;  }  template < class FeatureFreq >  void add( FeatureFreq f, Real x ) {    value += x;  }      void add( Real x ) { value += x; }    //Real& operator[]( const FeatureFreq& key ) { return freq_array[ key ]; }  operator Real( void ) const {    return value;  }    void ensureSafeValue(Real max_val, Real small_val) {	  if( !finite( value ) ) {		  AMIS_DEBUG_MESSAGE(2, "model expect value is not finite\n");		  value = max_val;	  }	  else if( value == 0.0) {		  AMIS_DEBUG_MESSAGE(2, "model expect value is zero\n" );		  value = small_val;	  }	  else {		  // no change	  }    }public:  FeatureFreqValue& operator+=( FeatureFreqValue x ) {    value += x;    return (*this);  }  FeatureFreqValue& operator*=( Real x ) {    value *= x;    return (*this);  }public:  void debugInfo( std::ostream& ostr ) const {    ostr << value;  }};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>FeatureFreqMap</name>/// <overview>A feature frequeycy vector with a map implementation</overview>/// <desc>A map implementation of FeatureFreq</desc>/// <body>template < class Feature >class FeatureFreqMap {public:  typedef typename Feature::FeatureFreq FeatureFreq;private:  std::map< FeatureFreq, Real > freq_array;public:  FeatureFreqMap( void ) {}  ~FeatureFreqMap() {}  static const std::string featureFreqArrayName( void ) { return "FeatureFreqMap"; }  /// Get the name of this class  void reserve( FeatureFreq ) {    // do nothing  }  void clear( void ) {    for ( typename std::map< FeatureFreq, Real >::iterator it = freq_array.begin();          it != freq_array.end();          ++it ) {      it->second = 0.0;    }  }  void add( FeatureFreq key, Real x ) {    freq_array[ key ] += x;  }  //Real& operator[]( const FeatureFreq& key ) { return freq_array[ key ]; }    void ensureSafeValue(Real max_val, Real small_val) {	 // Please implement  }public:  bool highestOrder( FeatureFreq& order, Real& freq ) const {    for ( typename std::map< FeatureFreq, Real >::const_reverse_iterator it = freq_array.rbegin();          it != freq_array.rend();          ++it ) {      if ( it->second != 0.0 ) {        order = it->first;        freq = it->second;        return true;      }    }    return false;  }  void computePolynomialDerivative( Real x, Real& polynomial, Real& derivative ) const {    for ( typename std::map< FeatureFreq, Real >::const_iterator it = freq_array.begin();          it != freq_array.end();          ++it ) {      Real factor = it->second * pow( x, it->first - 1 );      derivative += it->first * factor;      polynomial += factor * x;    }  }  FeatureFreqMap& operator+=( const FeatureFreqMap& array ) {    for ( typename std::map< FeatureFreq, Real >::const_iterator it = array.freq_array.begin();          it != array.freq_array.end();          ++it ) {      freq_array[ it->first ] += it->second;    }    return (*this);  }  FeatureFreqMap& operator*=( Real x ) {    for ( typename std::map< FeatureFreq, Real >::iterator it = freq_array.begin();	  it != freq_array.end();	  ++it ) {      it->second *= x;    }    return (*this);  }public:  void debugInfo( std::ostream& ostr ) const {    for ( typename std::map< FeatureFreq, Real >::const_iterator it = freq_array.begin();          it != freq_array.end();          ++it ) {      ostr << " (" << it->first << ',' << it->second << ')';    }  }};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>FeatureFreqVector</name>/// <overview>A feature frequeycy vector with a vector implementation</overview>/// <desc>A vector implementation of FeatureFreq</desc>/// <body>template < class Feature >class FeatureFreqVector {public:  typedef typename Feature::FeatureFreq FeatureFreq;private:  std::vector< Real > freq_array;public:  FeatureFreqVector( void ) {}  ~FeatureFreqVector() {}  static const std::string featureFreqArrayName( void ) { return "FeatureFreqVector"; }  /// Get the name of this class  void reserve( FeatureFreq n ) {    freq_array.resize( n, 0.0 );  }  void clear( void ) {    for ( typename std::vector< Real >::iterator it = freq_array.begin();          it != freq_array.end();          ++it ) {      (*it) = 0.0;    }  }  void add( FeatureFreq key, Real x ) {    freq_array[ key ] += x;  }  //Real& operator[]( int key ) { return freq_array[ key ]; }  void ensureSafeValue(Real max_val, Real small_val) {	 // Please implement    }public:  bool highestOrder( int& order, Real& freq ) const {    for ( size_t i = freq_array.size() - 1; i >= 0; --i ) {      if ( freq_array[ i ] != 0.0 ) {        order = i;        freq = freq_array[ i ];        return true;      }    }    return false;  }  void computePolynomialDerivative( Real x, Real& polynomial, Real& derivative ) const {    Real alpha = 1.0 / x;    for ( size_t k = 0; k < freq_array.size(); ++k ) {      derivative += alpha * freq_array[ k ] * k;      alpha *= x;      polynomial += alpha * freq_array[ k ];    }    // alpha represents pow(x, k-1) at each iteration. (J.K.)  }  FeatureFreqVector& operator+=( const FeatureFreqVector& array ) {    for ( size_t i = 0; i < array.freq_array.size(); ++i ) {      freq_array[ i ] += array.freq_array[ i ];    }    return (*this);  }  FeatureFreqVector& operator*=( Real x ) {    for ( size_t i = 0; i < freq_array.size(); ++i ) {      freq_array[ i ] *= x;    }    return (*this);  }public:  void debugInfo( std::ostream& ostr ) const {    for ( size_t i = 0; i < freq_array.size(); ++i ) {      if ( freq_array[ i ] != 0.0 ) {        ostr << " (" << i << ',' << freq_array[ i ] << ')';      }    }  }};template <>class FeatureFreqVector< RealFeature > : public FeatureFreqMap< RealFeature > {};AMIS_NAMESPACE_END/// </body>/// </classdef>#endif // FeatureFreqArray_h_// end of FeatureFreqArray.h

⌨️ 快捷键说明

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