diff options
author | Hieu Hoang <fishandfrolick@gmail.com> | 2013-06-10 19:29:07 +0400 |
---|---|---|
committer | Hieu Hoang <fishandfrolick@gmail.com> | 2013-06-10 19:29:07 +0400 |
commit | 96d116dba09f4c29fbbfa1edd3d52a31a4b74b49 (patch) | |
tree | 14c82d72d0045215254f453aa3192d56d030ede6 /moses | |
parent | 14e676ab035382cf55bb6cd6023eca4070ce7de4 (diff) |
refactor parsing of feature functiona args
Diffstat (limited to 'moses')
-rw-r--r-- | moses/DecodeFeature.cpp | 36 | ||||
-rw-r--r-- | moses/DecodeFeature.h | 1 | ||||
-rw-r--r-- | moses/FF/FeatureFunction.cpp | 73 | ||||
-rw-r--r-- | moses/FF/FeatureFunction.h | 3 | ||||
-rw-r--r-- | moses/TranslationModel/PhraseDictionary.cpp | 9 | ||||
-rw-r--r-- | moses/TranslationModel/PhraseDictionary.h | 2 | ||||
-rw-r--r-- | moses/TranslationModel/PhraseDictionaryMultiModelCounts.cpp | 32 | ||||
-rw-r--r-- | moses/TranslationModel/PhraseDictionaryMultiModelCounts.h | 2 |
8 files changed, 108 insertions, 50 deletions
diff --git a/moses/DecodeFeature.cpp b/moses/DecodeFeature.cpp index 7d2027126..37af49e3f 100644 --- a/moses/DecodeFeature.cpp +++ b/moses/DecodeFeature.cpp @@ -34,18 +34,17 @@ DecodeFeature::DecodeFeature( const std::string& description : StatelessFeatureFunction(description, line) { VERBOSE(2,"DecodeFeature:" << std::endl); - for (size_t i = 0; i < m_args.size(); ++i) { - const vector<string> &args = m_args[i]; - - if (args[0] == "input-factor") { - m_input =Tokenize<FactorType>(args[1], ","); - m_inputFactors = FactorMask(m_input); - } else if (args[0] == "output-factor") { - m_output =Tokenize<FactorType>(args[1], ","); - m_outputFactors = FactorMask(m_output); + size_t ind = 0; + while (ind < m_args.size()) { + vector<string> &args = m_args[ind]; + bool consumed = OverrideParameter(args[0], args[1]); + if (consumed) { + m_args.erase(m_args.begin() + ind); + } + else { + ++ind; } } - } DecodeFeature::DecodeFeature( const std::string& description @@ -69,6 +68,23 @@ DecodeFeature::DecodeFeature(const std::string& description VERBOSE(2,"DecodeFeature: input=" << m_inputFactors << " output=" << m_outputFactors << std::endl); } +bool DecodeFeature::OverrideParameter(const std::string& key, const std::string& value) +{ + if (key == "input-factor") { + m_input =Tokenize<FactorType>(value, ","); + m_inputFactors = FactorMask(m_input); + } + else if (key == "output-factor") { + m_output =Tokenize<FactorType>(value, ","); + m_outputFactors = FactorMask(m_output); + } + else { + return StatelessFeatureFunction::OverrideParameter(key, value); + } + + return true; +} + const FactorMask& DecodeFeature::GetOutputFactorMask() const { diff --git a/moses/DecodeFeature.h b/moses/DecodeFeature.h index 15092ee80..c7b4b3b7b 100644 --- a/moses/DecodeFeature.h +++ b/moses/DecodeFeature.h @@ -61,6 +61,7 @@ public: const std::vector<FactorType>& GetOutput() const; bool IsUseable(const FactorMask &mask) const; + virtual bool OverrideParameter(const std::string& key, const std::string& value); protected: std::vector<FactorType> m_input; diff --git a/moses/FF/FeatureFunction.cpp b/moses/FF/FeatureFunction.cpp index 7413c1914..092caea32 100644 --- a/moses/FF/FeatureFunction.cpp +++ b/moses/FF/FeatureFunction.cpp @@ -1,6 +1,7 @@ #include <stdexcept> #include "util/check.hh" +#include "util/exception.hh" #include "FeatureFunction.h" #include "moses/Hypothesis.h" @@ -33,39 +34,41 @@ FeatureFunction &FeatureFunction::FindFeatureFunction(const std::string& name) FeatureFunction::FeatureFunction(const std::string& description, const std::string &line) : m_tuneable(true) { - ParseLine(description, line); - - if (m_description == "") { - // not been given a name. Make a unique name - size_t index = description_counts.count(description); - - ostringstream dstream; - dstream << description; - dstream << index; - - description_counts.insert(description); - m_description = dstream.str(); - } - - ScoreComponentCollection::RegisterScoreProducer(this); - m_producers.push_back(this); + Initialize(description, line); } FeatureFunction::FeatureFunction(const std::string& description, size_t numScoreComponents, const std::string &line) : m_numScoreComponents(numScoreComponents) , m_tuneable(true) { + Initialize(description, line); +} + +void FeatureFunction::Initialize(const std::string& description, const std::string &line) +{ ParseLine(description, line); + size_t ind = 0; + while (ind < m_args.size()) { + vector<string> &args = m_args[ind]; + bool consumed = OverrideParameter(args[0], args[1]); + if (consumed) { + m_args.erase(m_args.begin() + ind); + } + else { + ++ind; + } + } + if (m_description == "") { - size_t index = description_counts.count(description); + size_t index = description_counts.count(description); - ostringstream dstream; - dstream << description; - dstream << index; + ostringstream dstream; + dstream << description; + dstream << index; - description_counts.insert(description); - m_description = dstream.str(); + description_counts.insert(description); + m_description = dstream.str(); } ScoreComponentCollection::RegisterScoreProducer(this); @@ -84,22 +87,24 @@ void FeatureFunction::ParseLine(const std::string& description, const std::strin for (size_t i = 1; i < toks.size(); ++i) { vector<string> args = Tokenize(toks[i], "="); CHECK(args.size() == 2); - - if (args[0] == "num-features") { - m_numScoreComponents = Scan<size_t>(args[1]); - } else if (args[0] == "name") { - m_description = args[1]; - } else if (args[0] == "tuneable") { - m_tuneable = Scan<bool>(args[1]); - } else { - m_args.push_back(args); - } + m_args.push_back(args); } } -void FeatureFunction::OverrideParameter(const std::string& key, const std::string& value) +bool FeatureFunction::OverrideParameter(const std::string& key, const std::string& value) { - throw "unknown key" + key; + if (key == "num-features") { + m_numScoreComponents = Scan<size_t>(value); + } else if (key == "name") { + m_description = value; + } else if (key == "tuneable") { + m_tuneable = Scan<bool>(value); + } else { + //UTIL_THROW(util::Exception, "unknown key" << key); + return false; + } + + return true; } } diff --git a/moses/FF/FeatureFunction.h b/moses/FF/FeatureFunction.h index 7fd92802b..71688b436 100644 --- a/moses/FF/FeatureFunction.h +++ b/moses/FF/FeatureFunction.h @@ -40,6 +40,7 @@ protected: //In case there's multiple producers with the same description static std::multiset<std::string> description_counts; + void Initialize(const std::string& description, const std::string &line); void ParseLine(const std::string& description, const std::string &line); public: @@ -105,7 +106,7 @@ public: , ScoreComponentCollection &scoreBreakdown) const {} - virtual void OverrideParameter(const std::string& key, const std::string& value); + virtual bool OverrideParameter(const std::string& key, const std::string& value); }; } diff --git a/moses/TranslationModel/PhraseDictionary.cpp b/moses/TranslationModel/PhraseDictionary.cpp index f4984b0f0..22802f6b5 100644 --- a/moses/TranslationModel/PhraseDictionary.cpp +++ b/moses/TranslationModel/PhraseDictionary.cpp @@ -70,16 +70,17 @@ GetTargetPhraseCollection(InputType const& src,WordsRange const& range) const return GetTargetPhraseCollection(phrase); } -void PhraseDictionary::OverrideParameter(const std::string& key, const std::string& value) +bool PhraseDictionary::OverrideParameter(const std::string& key, const std::string& value) { if (key == "table-limit") { m_tableLimit = Scan<size_t>(value); } - else { - // call parent function, all the way to the top if necessary - DecodeFeature::OverrideParameter(key, value); + return DecodeFeature::OverrideParameter(key, value); } + + return true; + } } diff --git a/moses/TranslationModel/PhraseDictionary.h b/moses/TranslationModel/PhraseDictionary.h index a4cfd0a31..80033ac48 100644 --- a/moses/TranslationModel/PhraseDictionary.h +++ b/moses/TranslationModel/PhraseDictionary.h @@ -93,7 +93,7 @@ public: return m_featuresToApply; } - void OverrideParameter(const std::string& key, const std::string& value); + bool OverrideParameter(const std::string& key, const std::string& value); protected: size_t m_tableLimit; diff --git a/moses/TranslationModel/PhraseDictionaryMultiModelCounts.cpp b/moses/TranslationModel/PhraseDictionaryMultiModelCounts.cpp index cbc178526..06b53e9c6 100644 --- a/moses/TranslationModel/PhraseDictionaryMultiModelCounts.cpp +++ b/moses/TranslationModel/PhraseDictionaryMultiModelCounts.cpp @@ -101,6 +101,38 @@ PhraseDictionaryMultiModelCounts::PhraseDictionaryMultiModelCounts(const std::st } +bool PhraseDictionaryMultiModelCounts::OverrideParameter(const std::string& key, const std::string& value) +{ + if (key == "mode") { + m_mode = value; + if (m_mode == "instance_weighting") + m_combineFunction = InstanceWeighting; + else if (m_mode == "interpolate") { + m_combineFunction = LinearInterpolationFromCounts; + } else { + ostringstream msg; + msg << "combination mode unknown: " << m_mode; + throw runtime_error(msg.str()); + } + + } else if (key == "lex-e2f") { + m_lexE2FStr = Tokenize(value, ","); + CHECK(m_lexE2FStr.size() == m_pdStr.size()); + } else if (key == "lex-f2e") { + m_lexF2EStr = Tokenize(value, ","); + CHECK(m_lexF2EStr.size() == m_pdStr.size()); + } + + else if (key == "target-table") { + m_targetTable = Tokenize(value, ","); + CHECK(m_targetTable.size() == m_pdStr.size()); + } + + else { + PhraseDictionaryMultiModel::OverrideParameter(key, value); + } +} + PhraseDictionaryMultiModelCounts::~PhraseDictionaryMultiModelCounts() { RemoveAllInColl(m_lexTable_e2f); diff --git a/moses/TranslationModel/PhraseDictionaryMultiModelCounts.h b/moses/TranslationModel/PhraseDictionaryMultiModelCounts.h index eaa6da956..1d6e5f681 100644 --- a/moses/TranslationModel/PhraseDictionaryMultiModelCounts.h +++ b/moses/TranslationModel/PhraseDictionaryMultiModelCounts.h @@ -103,6 +103,8 @@ public: /* Don't do anything source specific here as this object is shared between threads.*/ } + bool OverrideParameter(const std::string& key, const std::string& value); + private: std::vector<PhraseDictionary*> m_inverse_pd; std::vector<lexicalTable*> m_lexTable_e2f, m_lexTable_f2e; |