00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_Parameter_h
00023 #define moses_Parameter_h
00024
00025 #include <string>
00026 #include <set>
00027 #include <map>
00028 #include <vector>
00029 #include "TypeDef.h"
00030 #include "Util.h"
00031 #include <boost/program_options.hpp>
00032
00033 namespace Moses
00034 {
00035 typedef std::vector<std::string> PARAM_VEC;
00036 typedef std::map<std::string, PARAM_VEC > PARAM_MAP;
00037 typedef std::map<std::string, bool> PARAM_BOOL;
00038 typedef std::map<std::string, std::string > PARAM_STRING;
00039
00044 class Parameter
00045 {
00046 typedef boost::program_options::options_description options_description;
00047 typedef boost::program_options::value_semantic value_semantic;
00048 protected:
00049 PARAM_MAP m_setting;
00050 PARAM_BOOL m_valid;
00051 PARAM_STRING m_abbreviation;
00052 PARAM_STRING m_description;
00053 PARAM_STRING m_fullname;
00054
00055
00056 options_description m_options;
00057
00058 std::map<std::string, std::vector<float> > m_weights;
00059
00060 std::string FindParam(const std::string ¶mSwitch, int argc, char const* argv[]);
00061 void OverwriteParam(const std::string ¶mSwitch, const std::string ¶mName,
00062 int argc, char const* argv[]);
00063 bool ReadConfigFile(const std::string &filePath );
00064 bool FilesExist(const std::string ¶mName, int fieldNo, std::vector<std::string> const& fileExtension=std::vector<std::string>(1,""));
00065 bool isOption(const char* token);
00066 bool Validate();
00067
00068 void
00069 AddParam(options_description& optgroup,
00070 value_semantic const* optvalue,
00071 std::string const& paramName,
00072 std::string const& description);
00073
00074 void
00075 AddParam(options_description& optgroup,
00076 std::string const ¶mName,
00077 std::string const &description);
00078
00079 void
00080 AddParam(options_description& optgroup,
00081 value_semantic const* optvalue,
00082 std::string const& paramName,
00083 std::string const& abbrevName,
00084 std::string const& description);
00085
00086 void
00087 AddParam(options_description& optgroup,
00088 std::string const& paramName,
00089 std::string const& abbrevName,
00090 std::string const& description);
00091
00092 void PrintCredit();
00093 void PrintFF() const;
00094
00095 void SetWeight(const std::string &name, size_t ind, float weight);
00096 void SetWeight(const std::string &name, size_t ind, const std::vector<float> &weights);
00097 void AddWeight(const std::string &name, size_t ind, const std::vector<float> &weights);
00098 void ConvertWeightArgs();
00099 void ConvertWeightArgsSingleWeight(const std::string &oldWeightName, const std::string &newWeightName);
00100 void ConvertWeightArgsPhraseModel(const std::string &oldWeightName);
00101 void ConvertWeightArgsLM();
00102 void ConvertWeightArgsDistortion();
00103 void ConvertWeightArgsGeneration(const std::string &oldWeightName, const std::string &newWeightName);
00104 void ConvertWeightArgsPhrasePenalty();
00105 void ConvertWeightArgsWordPenalty();
00106 void ConvertPhrasePenalty();
00107 void CreateWeightsMap();
00108 void CreateWeightsMap(const PARAM_VEC &vec);
00109 void WeightOverwrite();
00110 void AddFeature(const std::string &line);
00111 void AddFeaturesCmd();
00112
00113
00114 public:
00115 Parameter();
00116 ~Parameter();
00117 bool LoadParam(int argc, char const* argv[]);
00118 bool LoadParam(const std::string &filePath);
00119 void Explain();
00120
00122 const PARAM_VEC *GetParam(const std::string ¶mName) const;
00123
00125 bool isParamSpecified(const std::string ¶mName) const {
00126 return m_setting.find( paramName ) != m_setting.end();
00127 }
00128
00129 void OverwriteParam(const std::string ¶mName, PARAM_VEC values);
00130
00131 std::vector<float> GetWeights(const std::string &name);
00132 const std::map<std::string, std::vector<float> > &GetAllWeights() const {
00133 return m_weights;
00134 }
00135 std::set<std::string> GetWeightNames() const;
00136
00137 const PARAM_MAP &GetParams() const {
00138 return m_setting;
00139 }
00140
00141 void Save(const std::string path);
00142
00143 template<typename T>
00144 void SetParameter(T &var, const std::string &name, const T &defaultValue) const {
00145 const PARAM_VEC *params = GetParam(name);
00146 if (params && params->size()) {
00147 var = Scan<T>( params->at(0));
00148 } else {
00149 var = defaultValue;
00150 }
00151 }
00152
00153 void SetParameter(bool& var, std::string const& name);
00154
00155 bool SetBooleanSwitch(bool& val, std::string const name) {
00156
00157 const PARAM_VEC *params = GetParam(name);
00158 val = (params && params->size());
00159 if (val && params->size() != 1) {
00160 TRACE_ERR("ERROR: wrong format for switch -" << name);
00161 return false;
00162 }
00163 return true;
00164 }
00165
00166 };
00167
00168 template<>
00169 void Parameter::SetParameter<bool>(bool &var, const std::string &name, const bool &defaultValue) const;
00170
00171 }
00172
00173 #endif