diff options
author | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-12-10 06:17:36 +0300 |
---|---|---|
committer | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-12-11 04:09:22 +0300 |
commit | 29694af6e43c1cec7a6fd0b157eb44faca706129 (patch) | |
tree | acda250a83c5a3a4244be011f1f4b9e6337f5d6b /moses/Syntax | |
parent | 240b88c6834c9c94e8a6448a34dc4ad33bdf3fbd (diff) |
Code cleanup and refactoring.
Diffstat (limited to 'moses/Syntax')
-rw-r--r-- | moses/Syntax/F2S/GlueRuleSynthesizer.cpp | 24 | ||||
-rw-r--r-- | moses/Syntax/F2S/GlueRuleSynthesizer.h | 7 | ||||
-rw-r--r-- | moses/Syntax/F2S/Manager-inl.h | 9 | ||||
-rw-r--r-- | moses/Syntax/KBestExtractor.cpp | 2 | ||||
-rw-r--r-- | moses/Syntax/RuleTableFF.cpp | 19 | ||||
-rw-r--r-- | moses/Syntax/RuleTableFF.h | 2 | ||||
-rw-r--r-- | moses/Syntax/S2T/Manager-inl.h | 6 | ||||
-rw-r--r-- | moses/Syntax/S2T/OovHandler-inl.h | 6 | ||||
-rw-r--r-- | moses/Syntax/SHyperedge.cpp | 2 | ||||
-rw-r--r-- | moses/Syntax/T2S/GlueRuleSynthesizer.cpp | 21 | ||||
-rw-r--r-- | moses/Syntax/T2S/GlueRuleSynthesizer.h | 8 | ||||
-rw-r--r-- | moses/Syntax/T2S/Manager-inl.h | 5 |
12 files changed, 60 insertions, 51 deletions
diff --git a/moses/Syntax/F2S/GlueRuleSynthesizer.cpp b/moses/Syntax/F2S/GlueRuleSynthesizer.cpp index c8d867650..930138750 100644 --- a/moses/Syntax/F2S/GlueRuleSynthesizer.cpp +++ b/moses/Syntax/F2S/GlueRuleSynthesizer.cpp @@ -3,9 +3,8 @@ #include <sstream> #include "moses/FF/UnknownWordPenaltyProducer.h" -#include "moses/StaticData.h" #include "util/string_stream.hh" - +#include "moses/parameters/AllOptions.h" namespace Moses { namespace Syntax @@ -14,13 +13,13 @@ namespace F2S { GlueRuleSynthesizer:: -GlueRuleSynthesizer(HyperTree &trie, const std::vector<FactorType> &iFactors) - : m_hyperTree(trie) +GlueRuleSynthesizer(Moses::AllOptions const& opts, HyperTree &trie) + : m_input_default_nonterminal(opts.syntax.input_default_non_terminal) + , m_output_default_nonterminal(opts.syntax.output_default_non_terminal) + , m_hyperTree(trie) { - // const std::vector<FactorType> &inputFactorOrder = - // StaticData::Instance().GetInputFactorOrder(); Word *lhs = NULL; - m_dummySourcePhrase.CreateFromString(Input, iFactors, "hello", &lhs); + m_dummySourcePhrase.CreateFromString(Input, opts.input.factor_order, "hello", &lhs); delete lhs; } @@ -47,11 +46,10 @@ void GlueRuleSynthesizer::SynthesizeHyperPath(const Forest::Hyperedge &e, } } -TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase( - const Forest::Hyperedge &e) +TargetPhrase* +GlueRuleSynthesizer:: +SynthesizeTargetPhrase(const Forest::Hyperedge &e) { - const StaticData &staticData = StaticData::Instance(); - const UnknownWordPenaltyProducer &unknownWordPenaltyProducer = UnknownWordPenaltyProducer::Instance(); @@ -61,7 +59,7 @@ TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase( for (std::size_t i = 0; i < e.tail.size(); ++i) { const Word &symbol = e.tail[i]->pvertex.symbol; if (symbol.IsNonTerminal()) { - targetPhrase->AddWord(staticData.GetOutputDefaultNonTerminal()); + targetPhrase->AddWord(m_output_default_nonterminal); } else { // TODO Check this Word &targetWord = targetPhrase->AddWord(); @@ -75,7 +73,7 @@ TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase( float score = LOWEST_SCORE; targetPhrase->GetScoreBreakdown().Assign(&unknownWordPenaltyProducer, score); targetPhrase->EvaluateInIsolation(m_dummySourcePhrase); - Word *targetLhs = new Word(staticData.GetOutputDefaultNonTerminal()); + Word *targetLhs = new Word(m_output_default_nonterminal); targetPhrase->SetTargetLHS(targetLhs); targetPhrase->SetAlignmentInfo(alignmentSS.str()); diff --git a/moses/Syntax/F2S/GlueRuleSynthesizer.h b/moses/Syntax/F2S/GlueRuleSynthesizer.h index c66fb0ff8..b4128c5d7 100644 --- a/moses/Syntax/F2S/GlueRuleSynthesizer.h +++ b/moses/Syntax/F2S/GlueRuleSynthesizer.h @@ -9,6 +9,7 @@ namespace Moses { + class AllOptions; namespace Syntax { namespace F2S @@ -16,9 +17,11 @@ namespace F2S class GlueRuleSynthesizer : public HyperTreeCreator { + Word m_input_default_nonterminal; + Word m_output_default_nonterminal; public: - GlueRuleSynthesizer(HyperTree &, std::vector<FactorType> const& iFactors); - + GlueRuleSynthesizer(Moses::AllOptions const& opts, HyperTree &); + // Synthesize the minimal, monotone rule that can be applied to the given // hyperedge and add it to the rule trie. void SynthesizeRule(const Forest::Hyperedge &); diff --git a/moses/Syntax/F2S/Manager-inl.h b/moses/Syntax/F2S/Manager-inl.h index 77d4e0d58..29b1f2ba9 100644 --- a/moses/Syntax/F2S/Manager-inl.h +++ b/moses/Syntax/F2S/Manager-inl.h @@ -60,9 +60,9 @@ void Manager<RuleMatcher>::Decode() const StaticData &staticData = StaticData::Instance(); // Get various pruning-related constants. - const std::size_t popLimit = staticData.options().cube.pop_limit; + const std::size_t popLimit = staticData.options()->cube.pop_limit; const std::size_t ruleLimit = staticData.GetRuleLimit(); - const std::size_t stackLimit = staticData.options().search.stack_size; + const std::size_t stackLimit = staticData.options()->search.stack_size; // Initialize the stacks. InitializeStacks(); @@ -74,8 +74,7 @@ void Manager<RuleMatcher>::Decode() RuleMatcherCallback callback(m_stackMap, ruleLimit); // Create a glue rule synthesizer. - GlueRuleSynthesizer glueRuleSynthesizer(*m_glueRuleTrie, - options()->input.factor_order); + GlueRuleSynthesizer glueRuleSynthesizer(*options(), *m_glueRuleTrie); // Sort the input forest's vertices into bottom-up topological order. std::vector<const Forest::Vertex *> sortedVertices; @@ -256,7 +255,7 @@ void Manager<RuleMatcher>::ExtractKBest( // with 0 being 'unlimited.' This actually sets a large-ish limit in case // too many translations are identical. const StaticData &staticData = StaticData::Instance(); - const std::size_t nBestFactor = staticData.options().nbest.factor; + const std::size_t nBestFactor = staticData.options()->nbest.factor; std::size_t numDerivations = (nBestFactor == 0) ? k*1000 : k*nBestFactor; // Extract the derivations. diff --git a/moses/Syntax/KBestExtractor.cpp b/moses/Syntax/KBestExtractor.cpp index 741d8ce82..21d15cd78 100644 --- a/moses/Syntax/KBestExtractor.cpp +++ b/moses/Syntax/KBestExtractor.cpp @@ -75,7 +75,7 @@ void KBestExtractor::Extract( // Generate the target-side yield of the derivation d. Phrase KBestExtractor::GetOutputPhrase(const Derivation &d) { - FactorType placeholderFactor = StaticData::Instance().options().input.placeholder_factor; + FactorType placeholderFactor = StaticData::Instance().options()->input.placeholder_factor; Phrase ret(ARRAY_SIZE_INCR); diff --git a/moses/Syntax/RuleTableFF.cpp b/moses/Syntax/RuleTableFF.cpp index a44a95f5e..beee34a41 100644 --- a/moses/Syntax/RuleTableFF.cpp +++ b/moses/Syntax/RuleTableFF.cpp @@ -24,34 +24,35 @@ RuleTableFF::RuleTableFF(const std::string &line) s_instances.push_back(this); } -void RuleTableFF::Load(Moses::AllOptions const& opts) +void RuleTableFF::Load(Moses::AllOptions::ptr const& opts) { + m_options = opts; SetFeaturesToApply(); - if (opts.search.algo == SyntaxF2S || opts.search.algo == SyntaxT2S) { + if (opts->search.algo == SyntaxF2S || opts->search.algo == SyntaxT2S) { F2S::HyperTree *trie = new F2S::HyperTree(this); F2S::HyperTreeLoader loader; - loader.Load(opts, m_input, m_output, m_filePath, *this, *trie, m_sourceTerminalSet); + loader.Load(*opts, m_input, m_output, m_filePath, *this, *trie, m_sourceTerminalSet); m_table = trie; - } else if (opts.search.algo == SyntaxS2T) { - S2TParsingAlgorithm algorithm = opts.syntax.s2t_parsing_algo; // staticData.GetS2TParsingAlgorithm(); + } else if (opts->search.algo == SyntaxS2T) { + S2TParsingAlgorithm algorithm = opts->syntax.s2t_parsing_algo; // staticData.GetS2TParsingAlgorithm(); if (algorithm == RecursiveCYKPlus) { S2T::RuleTrieCYKPlus *trie = new S2T::RuleTrieCYKPlus(this); S2T::RuleTrieLoader loader; - loader.Load(opts,m_input, m_output, m_filePath, *this, *trie); + loader.Load(*opts,m_input, m_output, m_filePath, *this, *trie); m_table = trie; } else if (algorithm == Scope3) { S2T::RuleTrieScope3 *trie = new S2T::RuleTrieScope3(this); S2T::RuleTrieLoader loader; - loader.Load(opts, m_input, m_output, m_filePath, *this, *trie); + loader.Load(*opts, m_input, m_output, m_filePath, *this, *trie); m_table = trie; } else { UTIL_THROW2("ERROR: unhandled S2T parsing algorithm"); } - } else if (opts.search.algo == SyntaxT2S_SCFG) { + } else if (opts->search.algo == SyntaxT2S_SCFG) { T2S::RuleTrie *trie = new T2S::RuleTrie(this); T2S::RuleTrieLoader loader; - loader.Load(opts, m_input, m_output, m_filePath, *this, *trie); + loader.Load(*opts, m_input, m_output, m_filePath, *this, *trie); m_table = trie; } else { UTIL_THROW2( diff --git a/moses/Syntax/RuleTableFF.h b/moses/Syntax/RuleTableFF.h index d1f87b2f0..9f394373a 100644 --- a/moses/Syntax/RuleTableFF.h +++ b/moses/Syntax/RuleTableFF.h @@ -27,7 +27,7 @@ public: // FIXME Delete m_table? ~RuleTableFF() {} - void Load(AllOptions const& opts); + void Load(AllOptions::ptr const& opts); const RuleTable *GetTable() const { return m_table; diff --git a/moses/Syntax/S2T/Manager-inl.h b/moses/Syntax/S2T/Manager-inl.h index afb1f04c6..6bfc4a61c 100644 --- a/moses/Syntax/S2T/Manager-inl.h +++ b/moses/Syntax/S2T/Manager-inl.h @@ -163,9 +163,9 @@ void Manager<Parser>::Decode() const StaticData &staticData = StaticData::Instance(); // Get various pruning-related constants. - const std::size_t popLimit = staticData.options().cube.pop_limit; + const std::size_t popLimit = staticData.options()->cube.pop_limit; const std::size_t ruleLimit = staticData.GetRuleLimit(); - const std::size_t stackLimit = staticData.options().search.stack_size; + const std::size_t stackLimit = staticData.options()->search.stack_size; // Initialise the PChart and SChart. InitializeCharts(); @@ -303,7 +303,7 @@ void Manager<Parser>::ExtractKBest( // with 0 being 'unlimited.' This actually sets a large-ish limit in case // too many translations are identical. const StaticData &staticData = StaticData::Instance(); - const std::size_t nBestFactor = staticData.options().nbest.factor; + const std::size_t nBestFactor = staticData.options()->nbest.factor; std::size_t numDerivations = (nBestFactor == 0) ? k*1000 : k*nBestFactor; // Extract the derivations. diff --git a/moses/Syntax/S2T/OovHandler-inl.h b/moses/Syntax/S2T/OovHandler-inl.h index 773cc7a91..5b67080ec 100644 --- a/moses/Syntax/S2T/OovHandler-inl.h +++ b/moses/Syntax/S2T/OovHandler-inl.h @@ -57,7 +57,7 @@ OovHandler<RuleTrie>::SynthesizeTargetLhs(const std::string &lhsStr) { Word *targetLhs = new Word(true); targetLhs->CreateFromString(Output, - StaticData::Instance().options().output.factor_order, + StaticData::Instance().options()->output.factor_order, lhsStr, true); UTIL_THROW_IF2(targetLhs->GetFactor(0) == NULL, "Null factor for target LHS"); return targetLhs; @@ -83,7 +83,7 @@ TargetPhrase *OovHandler<RuleTrie>::SynthesizeTargetPhrase( targetPhrase->EvaluateInIsolation(srcPhrase); targetPhrase->SetTargetLHS(&targetLhs); targetPhrase->SetAlignmentInfo("0-0"); - if (!SD.options().output.detailed_tree_transrep_filepath.empty() || + if (!SD.options()->output.detailed_tree_transrep_filepath.empty() || SD.GetTreeStructure() != NULL) { std::string value = "[ " + targetLhs[0]->GetString().as_string() + " " + oov[0]->GetString().as_string() + " ]"; @@ -96,7 +96,7 @@ TargetPhrase *OovHandler<RuleTrie>::SynthesizeTargetPhrase( template<typename RuleTrie> bool OovHandler<RuleTrie>::ShouldDrop(const Word &oov) { - if (!StaticData::Instance().options().unk.drop) { + if (!StaticData::Instance().options()->unk.drop) { return false; } const Factor *f = oov[0]; // TODO hack. shouldn't know which factor is surface diff --git a/moses/Syntax/SHyperedge.cpp b/moses/Syntax/SHyperedge.cpp index 976b0f0e3..d554af231 100644 --- a/moses/Syntax/SHyperedge.cpp +++ b/moses/Syntax/SHyperedge.cpp @@ -11,7 +11,7 @@ namespace Syntax Phrase GetOneBestTargetYield(const SHyperedge &h) { - FactorType placeholderFactor = StaticData::Instance().options().input.placeholder_factor; + FactorType placeholderFactor = StaticData::Instance().options()->input.placeholder_factor; Phrase ret(ARRAY_SIZE_INCR); diff --git a/moses/Syntax/T2S/GlueRuleSynthesizer.cpp b/moses/Syntax/T2S/GlueRuleSynthesizer.cpp index f50f84629..04b5da4e7 100644 --- a/moses/Syntax/T2S/GlueRuleSynthesizer.cpp +++ b/moses/Syntax/T2S/GlueRuleSynthesizer.cpp @@ -3,7 +3,7 @@ #include <sstream> #include "moses/FF/UnknownWordPenaltyProducer.h" -#include "moses/StaticData.h" +#include <boost/scoped_ptr.hpp> namespace Moses { @@ -12,7 +12,9 @@ namespace Syntax namespace T2S { -void GlueRuleSynthesizer::SynthesizeRule(const InputTree::Node &node) +void +GlueRuleSynthesizer:: +SynthesizeRule(const InputTree::Node &node) { const Word &sourceLhs = node.pvertex.symbol; boost::scoped_ptr<Phrase> sourceRhs(SynthesizeSourcePhrase(node)); @@ -22,7 +24,9 @@ void GlueRuleSynthesizer::SynthesizeRule(const InputTree::Node &node) tpc->Add(tp); } -Phrase *GlueRuleSynthesizer::SynthesizeSourcePhrase(const InputTree::Node &node) +Phrase* +GlueRuleSynthesizer:: +SynthesizeSourcePhrase(const InputTree::Node &node) { Phrase *phrase = new Phrase(node.children.size()); for (std::vector<InputTree::Node*>::const_iterator p = node.children.begin(); @@ -37,11 +41,10 @@ Phrase *GlueRuleSynthesizer::SynthesizeSourcePhrase(const InputTree::Node &node) return phrase; } -TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase( - const InputTree::Node &node, const Phrase &sourceRhs) +TargetPhrase* +GlueRuleSynthesizer:: +SynthesizeTargetPhrase(const InputTree::Node &node, const Phrase &sourceRhs) { - const StaticData &staticData = StaticData::Instance(); - const UnknownWordPenaltyProducer &unknownWordPenaltyProducer = UnknownWordPenaltyProducer::Instance(); @@ -51,7 +54,7 @@ TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase( for (std::size_t i = 0; i < node.children.size(); ++i) { const Word &symbol = node.children[i]->pvertex.symbol; if (symbol.IsNonTerminal()) { - targetPhrase->AddWord(staticData.GetOutputDefaultNonTerminal()); + targetPhrase->AddWord(m_output_default_nonterminal); } else { // TODO Check this Word &targetWord = targetPhrase->AddWord(); @@ -65,7 +68,7 @@ TargetPhrase *GlueRuleSynthesizer::SynthesizeTargetPhrase( float score = LOWEST_SCORE; targetPhrase->GetScoreBreakdown().Assign(&unknownWordPenaltyProducer, score); targetPhrase->EvaluateInIsolation(sourceRhs); - Word *targetLhs = new Word(staticData.GetOutputDefaultNonTerminal()); + Word *targetLhs = new Word(m_output_default_nonterminal); targetPhrase->SetTargetLHS(targetLhs); targetPhrase->SetAlignmentInfo(alignmentSS.str()); diff --git a/moses/Syntax/T2S/GlueRuleSynthesizer.h b/moses/Syntax/T2S/GlueRuleSynthesizer.h index 3e51c08a4..3d9e4f8f9 100644 --- a/moses/Syntax/T2S/GlueRuleSynthesizer.h +++ b/moses/Syntax/T2S/GlueRuleSynthesizer.h @@ -16,9 +16,13 @@ namespace T2S class GlueRuleSynthesizer : public RuleTrieCreator { + Word m_output_default_nonterminal; public: - GlueRuleSynthesizer(RuleTrie &trie) : m_ruleTrie(trie) {} - + GlueRuleSynthesizer(RuleTrie &trie, Word dflt_nonterm) + : m_ruleTrie(trie) + , m_output_default_nonterminal(dflt_nonterm) + {} + // Synthesize the minimal, montone rule that can be applied to the given node // and add it to the rule trie. void SynthesizeRule(const InputTree::Node &); diff --git a/moses/Syntax/T2S/Manager-inl.h b/moses/Syntax/T2S/Manager-inl.h index 76aae54a8..ec97e76de 100644 --- a/moses/Syntax/T2S/Manager-inl.h +++ b/moses/Syntax/T2S/Manager-inl.h @@ -111,7 +111,8 @@ void Manager<RuleMatcher>::Decode() F2S::RuleMatcherCallback callback(m_stackMap, ruleLimit); // Create a glue rule synthesizer. - GlueRuleSynthesizer glueRuleSynthesizer(*m_glueRuleTrie); + Word dflt_nonterm = options()->syntax.output_default_non_terminal; + GlueRuleSynthesizer glueRuleSynthesizer(*m_glueRuleTrie, dflt_nonterm); // Visit each node of the input tree in post-order. for (std::vector<InputTree::Node>::const_iterator p = @@ -215,7 +216,7 @@ void Manager<RuleMatcher>::ExtractKBest( // with 0 being 'unlimited.' This actually sets a large-ish limit in case // too many translations are identical. const StaticData &staticData = StaticData::Instance(); - const std::size_t nBestFactor = staticData.options().nbest.factor; + const std::size_t nBestFactor = staticData.options()->nbest.factor; std::size_t numDerivations = (nBestFactor == 0) ? k*1000 : k*nBestFactor; // Extract the derivations. |