📄 feature.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: Feature.h,v 1.4 2003/05/11 18:12:09 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>Feature.h</name>/// <overview>A class for feature functions</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_Feature_h_#define Amis_Feature_h_#include <amis/configure.h>#include <amis/Real.h>#include <amis/ErrorBase.h>#include <amis/objstream.h>#include <string>AMIS_NAMESPACE_BEGINtypedef int FeatureID;///////////////////////////////////////////////////////////////////////// <classdef>/// <name>IllegalFeatureError</name>/// <overview>Exception for illegal feature functions</overview>/// <desc>/// This class signals an illegal feature functions found/// </desc>/// <body>class IllegalFeatureError : public ErrorBase { public: IllegalFeatureError( const std::string& s ) : ErrorBase( s ) {} IllegalFeatureError( char* s ) : ErrorBase( s ) {}};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>BinaryFeature</name>/// <overview>Binary-valued feature function</overview>/// <desc>/// An implementation of feature functions which returns binary values./// </desc>/// <body>class BinaryFeature {public: typedef int FeatureFreq;private: FeatureID feature_id;public: static const FeatureFreq MAX_FEATURE_FREQ; /// Maximum value of a feature frequency static const FeatureFreq MIN_FEATURE_FREQ; /// Minimum value of a feature frequency static const std::string featureTypeName() { return "BinaryFeature"; }public: BinaryFeature() { feature_id = 0; } /// Make a feature without initial values BinaryFeature( FeatureID i, FeatureFreq f = 1 ) { if ( f != 1 ) throw( IllegalFeatureError( "Feature frequency must be 1 (BinaryFeature)" ) ); feature_id = i; } /// Make a feature with ID = i, frequency = f void set( FeatureID i, FeatureFreq f ) { feature_id = i; } FeatureID id() const { return feature_id; } /// The ID of the feature FeatureFreq freq() const { return 1; } /// The frequency of the feature void readObject( objstream& is ) { is >> feature_id; } /// Read a Feature object from a stream void writeObject( objstream& os ) { os << feature_id; } /// Write a Feature object to a stream // comparator for sorting bool operator<(const BinaryFeature& f) const { return feature_id < f.id(); }};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>IntegerFeature</name>/// <overview>Integer-valued feature function</overview>/// <desc>/// An implementation of feature functions which returns integer values./// </desc>/// <body>class IntegerFeature {public: typedef int FeatureFreq;private: FeatureID feature_id; FeatureFreq feature_freq;public: static const FeatureFreq MAX_FEATURE_FREQ; /// Maximum value of a feature frequency static const FeatureFreq MIN_FEATURE_FREQ; /// Minimum value of a feature frequency static const std::string featureTypeName() { return "IntegerFeature"; }public: IntegerFeature() { feature_id = 0; feature_freq = MIN_FEATURE_FREQ; } /// Make a feature without initial values IntegerFeature( FeatureID i, FeatureFreq f = 1 ) { if ( f <= 0 ) throw( IllegalFeatureError( "Feature frequency must be non positive (IntegerFeature)" ) ); feature_id = i; feature_freq = f; } /// Make a feature with ID = i, frequency = f void set( FeatureID i, FeatureFreq f ) { feature_id = i; feature_freq = f; } FeatureID id() const { return feature_id; } /// The ID of the feature FeatureFreq freq() const { return feature_freq; } /// The frequency of the feature void readObject( objstream& is ) { is >> feature_id; is >> feature_freq; } /// Read a Feature object from a stream void writeObject( objstream& os ) { os << feature_id; os << feature_freq; } /// Write a Feature object to a stream // comparator for sorting bool operator<(const IntegerFeature& f) const { return feature_id < f.id(); }};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>RealFeature</name>/// <overview>Real-valued feature function</overview>/// <desc>/// An implementation of feature functions which returns real values./// </desc>/// <body>class RealFeature {public: typedef Real FeatureFreq;private: FeatureID feature_id; FeatureFreq feature_freq;public: static const FeatureFreq MAX_FEATURE_FREQ; /// Maximum value of a feature frequency static const FeatureFreq MIN_FEATURE_FREQ; /// Minimum value of a feature frequency static const std::string featureTypeName() { return "RealFeature"; }public: RealFeature() { feature_id = 0; feature_freq = MIN_FEATURE_FREQ; } /// Make a feature without initial values RealFeature( FeatureID i, FeatureFreq f = 1 ) { if ( f <= 0.0 ) { AMIS_DEBUG_MESSAGE(2, "f=" << f << '\n' << std::flush); throw( IllegalFeatureError( "Feature frequency must be non positive (RealFeature)" ) ); } feature_id = i; feature_freq = f; } /// Make a feature with ID = i, frequency = f void set( FeatureID i, FeatureFreq f ) { feature_id = i; feature_freq = f; } FeatureID id() const { return feature_id; } /// The ID of the feature FeatureFreq freq() const { return feature_freq; } /// The frequency of the feature void readObject( objstream& is ) { is >> feature_id; is >> feature_freq; } /// Read a Feature object from a stream void writeObject( objstream& os ) { os << feature_id; os << feature_freq; } /// Write a Feature object to a stream // comparator for sorting bool operator<(const RealFeature& f) const { return feature_id < f.id(); }};/// </body>/// </classdef>AMIS_NAMESPACE_END#endif // Feature_h_// end of Feature_h_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -