diff options
author | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-03-26 21:25:54 +0300 |
---|---|---|
committer | Ulrich Germann <Ulrich.Germann@gmail.com> | 2015-03-26 21:25:54 +0300 |
commit | 9dc75bfd8ad3092f08f7a6d2a6492323b7803e56 (patch) | |
tree | f13405e11bce5001e6718ae0089fdcf85d961c98 /moses | |
parent | 4410e9225a5bd495fc6d097b206290beb59f4839 (diff) |
Managers and feature functions now have access to the entire TranslationTask, not just the InputType.
Diffstat (limited to 'moses')
-rw-r--r-- | moses/ChartManager.cpp | 19 | ||||
-rw-r--r-- | moses/ChartManager.h | 4 | ||||
-rw-r--r-- | moses/ChartParser.cpp | 19 | ||||
-rw-r--r-- | moses/ChartParser.h | 7 | ||||
-rw-r--r-- | moses/FF/FeatureFunction.cpp | 11 | ||||
-rw-r--r-- | moses/FF/FeatureFunction.h | 42 | ||||
-rw-r--r-- | moses/Incremental.cpp | 14 | ||||
-rw-r--r-- | moses/Incremental.h | 3 | ||||
-rw-r--r-- | moses/Manager.cpp | 3 | ||||
-rw-r--r-- | moses/MockHypothesis.cpp | 33 | ||||
-rw-r--r-- | moses/MockHypothesis.h | 11 | ||||
-rw-r--r-- | moses/Sentence.cpp | 8 | ||||
-rw-r--r-- | moses/Sentence.h | 4 | ||||
-rw-r--r-- | moses/StaticData.cpp | 20 | ||||
-rw-r--r-- | moses/StaticData.h | 37 | ||||
-rw-r--r-- | moses/TrainingTask.h | 44 | ||||
-rw-r--r-- | moses/TranslationTask.cpp | 111 | ||||
-rw-r--r-- | moses/TranslationTask.h | 16 | ||||
-rw-r--r-- | moses/server/TranslationRequest.cpp | 6 |
19 files changed, 266 insertions, 146 deletions
diff --git a/moses/ChartManager.cpp b/moses/ChartManager.cpp index aa550e48b..d221bd0fc 100644 --- a/moses/ChartManager.cpp +++ b/moses/ChartManager.cpp @@ -39,21 +39,21 @@ using namespace std; namespace Moses { + extern bool g_mosesDebug; /* constructor. Initialize everything prior to decoding a particular sentence. * \param source the sentence to be decoded * \param system which particular set of models to use. */ -ChartManager::ChartManager(InputType const& source) - :BaseManager(source) - ,m_hypoStackColl(source, *this) - ,m_start(clock()) - ,m_hypothesisId(0) - ,m_parser(source, m_hypoStackColl) - ,m_translationOptionList(StaticData::Instance().GetRuleLimit(), source) -{ -} +ChartManager::ChartManager(ttasksptr const& ttask) + : BaseManager(ttask) + , m_hypoStackColl(m_source, *this) + , m_start(clock()) + , m_hypothesisId(0) + , m_parser(ttask, m_hypoStackColl) + , m_translationOptionList(StaticData::Instance().GetRuleLimit(), m_source) +{ } ChartManager::~ChartManager() { @@ -67,6 +67,7 @@ ChartManager::~ChartManager() //! decode the sentence. This contains the main laps. Basically, the CKY++ algorithm void ChartManager::Decode() { + VERBOSE(1,"Translating: " << m_source << endl); ResetSentenceStats(m_source); diff --git a/moses/ChartManager.h b/moses/ChartManager.h index 745a792cb..bf5851806 100644 --- a/moses/ChartManager.h +++ b/moses/ChartManager.h @@ -33,8 +33,6 @@ #include "BaseManager.h" #include "moses/Syntax/KBestExtractor.h" -#include <boost/shared_ptr.hpp> - namespace Moses { @@ -103,7 +101,7 @@ private: void Backtrack(const ChartHypothesis *hypo) const; public: - ChartManager(InputType const& source); + ChartManager(ttasksptr const& ttask); ~ChartManager(); void Decode(); void AddXmlChartOptions(); diff --git a/moses/ChartParser.cpp b/moses/ChartParser.cpp index 5281bafdf..b0d2a3333 100644 --- a/moses/ChartParser.cpp +++ b/moses/ChartParser.cpp @@ -28,6 +28,7 @@ #include "DecodeGraph.h" #include "moses/FF/UnknownWordPenaltyProducer.h" #include "moses/TranslationModel/PhraseDictionary.h" +#include "moses/TranslationTask.h" using namespace std; using namespace Moses; @@ -35,7 +36,10 @@ using namespace Moses; namespace Moses { -ChartParserUnknown::ChartParserUnknown() {} +ChartParserUnknown +::ChartParserUnknown(ttasksptr const& ttask) + : m_ttask(ttask) +{ } ChartParserUnknown::~ChartParserUnknown() { @@ -136,13 +140,16 @@ void ChartParserUnknown::Process(const Word &sourceWord, const WordsRange &range } } -ChartParser::ChartParser(InputType const &source, ChartCellCollectionBase &cells) : - m_decodeGraphList(StaticData::Instance().GetDecodeGraphs()), - m_source(source) +ChartParser +::ChartParser(ttasksptr const& ttask, ChartCellCollectionBase &cells) + : m_ttask(ttask) + , m_unknown(ttask) + , m_decodeGraphList(StaticData::Instance().GetDecodeGraphs()) + , m_source(*(ttask->GetSource().get())) { const StaticData &staticData = StaticData::Instance(); - staticData.InitializeForInput(source); + staticData.InitializeForInput(ttask); CreateInputPaths(m_source); const std::vector<PhraseDictionary*> &dictionaries = PhraseDictionary::GetColl(); @@ -161,7 +168,7 @@ ChartParser::ChartParser(InputType const &source, ChartCellCollectionBase &cells ChartParser::~ChartParser() { RemoveAllInColl(m_ruleLookupManagers); - StaticData::Instance().CleanUpAfterSentenceProcessing(m_source); + StaticData::Instance().CleanUpAfterSentenceProcessing(m_ttask.lock()); InputPathMatrix::const_iterator iterOuter; for (iterOuter = m_inputPathMatrix.begin(); iterOuter != m_inputPathMatrix.end(); ++iterOuter) { diff --git a/moses/ChartParser.h b/moses/ChartParser.h index e438cf8ad..372a05f60 100644 --- a/moses/ChartParser.h +++ b/moses/ChartParser.h @@ -1,3 +1,4 @@ +// -*- c++ -*- // $Id$ // vim:tabstop=2 /*********************************************************************** @@ -42,8 +43,9 @@ class DecodeGraph; class ChartParserUnknown { + ttaskwptr m_ttask; public: - ChartParserUnknown(); + ChartParserUnknown(ttasksptr const& ttask); ~ChartParserUnknown(); void Process(const Word &sourceWord, const WordsRange &range, ChartParserCallback &to); @@ -59,8 +61,9 @@ private: class ChartParser { + ttaskwptr m_ttask; public: - ChartParser(const InputType &source, ChartCellCollectionBase &cells); + ChartParser(ttasksptr const& ttask, ChartCellCollectionBase &cells); ~ChartParser(); void Create(const WordsRange &range, ChartParserCallback &to); diff --git a/moses/FF/FeatureFunction.cpp b/moses/FF/FeatureFunction.cpp index 5c4b65729..a13b97a4c 100644 --- a/moses/FF/FeatureFunction.cpp +++ b/moses/FF/FeatureFunction.cpp @@ -6,6 +6,7 @@ #include "moses/Hypothesis.h" #include "moses/Manager.h" #include "moses/TranslationOption.h" +#include "moses/TranslationTask.h" #include "moses/Util.h" #include "moses/FF/DistortionScoreProducer.h" @@ -186,5 +187,15 @@ void FeatureFunction::SetTuneableComponents(const std::string& value) } } +void +FeatureFunction +::InitializeForInput(ttasksptr const& ttask) +{ InitializeForInput(*(ttask->GetSource().get())); } + +void +FeatureFunction +::CleanUpAfterSentenceProcessing(ttasksptr const& ttask) +{ CleanupAfterSentenceProcessing(*(ttask->GetSource().get())); } + } diff --git a/moses/FF/FeatureFunction.h b/moses/FF/FeatureFunction.h index c8c78fb0f..0f620471e 100644 --- a/moses/FF/FeatureFunction.h +++ b/moses/FF/FeatureFunction.h @@ -114,31 +114,43 @@ public: virtual std::vector<float> DefaultWeights() const; + +protected: + virtual void + InitializeForInput(InputType const& source) { } + virtual void + CleanupAfterSentenceProcessing(InputType const& source) { } + +public: //! Called before search and collecting of translation options - virtual void InitializeForInput(InputType const& source) { - } + virtual void + InitializeForInput(ttasksptr const& ttask); // clean up temporary memory, called after processing each sentence - virtual void CleanUpAfterSentenceProcessing(const InputType& source) { - } + virtual void + CleanUpAfterSentenceProcessing(ttasksptr const& ttask); - const std::string &GetArgLine() const { - return m_argLine; - } + const std::string & + GetArgLine() const { return m_argLine; } // given a target phrase containing only factors specified in mask // return true if the feature function can be evaluated virtual bool IsUseable(const FactorMask &mask) const = 0; - // used by stateless ff and stateful ff. Calculate initial score estimate during loading of phrase table - // source phrase is the substring that the phrase table uses to look up the target phrase, + // used by stateless ff and stateful ff. Calculate initial score + // estimate during loading of phrase table + // + // source phrase is the substring that the phrase table uses to look + // up the target phrase, + // // may have more factors than actually need, but not guaranteed. - // For SCFG decoding, the source contains non-terminals, NOT the raw source from the input sentence - virtual void EvaluateInIsolation(const Phrase &source - , const TargetPhrase &targetPhrase - , ScoreComponentCollection &scoreBreakdown - , ScoreComponentCollection &estimatedFutureScore) const = 0; - + // For SCFG decoding, the source contains non-terminals, NOT the raw + // source from the input sentence + virtual void + EvaluateInIsolation(const Phrase &source, const TargetPhrase &targetPhrase, + ScoreComponentCollection& scoreBreakdown, + ScoreComponentCollection& estimatedFutureScore) const = 0; + // override this method if you want to change the input before decoding virtual void ChangeSource(InputType * const&input) const { } diff --git a/moses/Incremental.cpp b/moses/Incremental.cpp index 49573664e..17632b571 100644 --- a/moses/Incremental.cpp +++ b/moses/Incremental.cpp @@ -203,15 +203,15 @@ struct ChartCellBaseFactory { } // namespace -Manager::Manager(const InputType &source) : - BaseManager(source), - cells_(source, ChartCellBaseFactory()), - parser_(source, cells_), - n_best_(search::NBestConfig(StaticData::Instance().GetNBestSize())) {} +Manager::Manager(ttasksptr const& ttask) + : BaseManager(ttask) + , cells_(m_source, ChartCellBaseFactory()) + , parser_(ttask, cells_) + , n_best_(search::NBestConfig(StaticData::Instance().GetNBestSize())) +{ } Manager::~Manager() -{ -} +{ } template <class Model, class Best> search::History Manager::PopulateBest(const Model &model, const std::vector<lm::WordIndex> &words, Best &out) { diff --git a/moses/Incremental.h b/moses/Incremental.h index c1f5e40b3..91b5dc5a0 100644 --- a/moses/Incremental.h +++ b/moses/Incremental.h @@ -1,3 +1,4 @@ +// -*- c++ -*- #pragma once #include "lm/word_index.hh" @@ -24,7 +25,7 @@ namespace Incremental class Manager : public BaseManager { public: - Manager(const InputType &source); + Manager(ttasksptr const& ttask); ~Manager(); diff --git a/moses/Manager.cpp b/moses/Manager.cpp index fe8267626..98feaa4a7 100644 --- a/moses/Manager.cpp +++ b/moses/Manager.cpp @@ -44,6 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #include "moses/LM/Base.h" #include "moses/TranslationModel/PhraseDictionary.h" #include "moses/TranslationAnalysis.h" +#include "moses/TranslationTask.h" #include "moses/HypergraphOutput.h" #include "moses/mbr.h" #include "moses/LatticeMBR.h" @@ -85,7 +86,7 @@ Manager::~Manager() const InputType& Manager::GetSource() const -{ return m_source) ; } +{ return m_source ; } /** * Main decoder loop that translates a sentence by expanding diff --git a/moses/MockHypothesis.cpp b/moses/MockHypothesis.cpp index 999124a90..515f995cd 100644 --- a/moses/MockHypothesis.cpp +++ b/moses/MockHypothesis.cpp @@ -17,13 +17,12 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ***********************************************************************/ - - #include "MockHypothesis.h" +#include "TranslationOption.h" +#include "TranslationTask.h" #include <boost/test/unit_test.hpp> -#include "TranslationOption.h" using namespace Moses; using namespace std; @@ -31,29 +30,23 @@ using namespace std; namespace MosesTest { - -MockHypothesisGuard::MockHypothesisGuard( - const string& sourceSentence, +MockHypothesisGuard +::MockHypothesisGuard +( const string& sourceSentence, const vector<Alignment>& alignments, const vector<string>& targetSegments) - : m_initialTransOpt(), - m_wp("WordPenalty"), - m_uwp("UnknownWordPenalty"), - m_dist("Distortion"), - m_manager(m_sentence) + : m_initialTransOpt(), m_wp("WordPenalty"), + m_uwp("UnknownWordPenalty"), m_dist("Distortion") { BOOST_CHECK_EQUAL(alignments.size(), targetSegments.size()); - - std::vector<Moses::FactorType> factors; - factors.push_back(0); - - stringstream in(sourceSentence + "\n"); - m_sentence.Read(in,factors); - + std::vector<Moses::FactorType> factors(1,0); + m_sentence.reset(new Sentence(0, sourceSentence, &factors)); + m_ttask = TranslationTask::create(m_sentence); + m_manager.reset(new Manager(m_ttask)); //Initial empty hypothesis - m_manager.ResetSentenceStats(m_sentence); - m_hypothesis = Hypothesis::Create(m_manager, m_sentence, m_initialTransOpt); + m_manager->ResetSentenceStats(*m_sentence); + m_hypothesis = Hypothesis::Create(*m_manager, *m_sentence, m_initialTransOpt); //create the chain vector<Alignment>::const_iterator ai = alignments.begin(); diff --git a/moses/MockHypothesis.h b/moses/MockHypothesis.h index 78cfc104e..34a313553 100644 --- a/moses/MockHypothesis.h +++ b/moses/MockHypothesis.h @@ -1,3 +1,4 @@ +// -*- c++ -*- /*********************************************************************** Moses - factored phrase-based language decoder Copyright (C) 2010 University of Edinburgh @@ -45,10 +46,11 @@ class MockHypothesisGuard public: /** Creates a phrase-based hypothesis. */ - MockHypothesisGuard( - const std::string& sourceSentence, + MockHypothesisGuard + ( const std::string& sourceSentence, const std::vector<Alignment>& alignments, const std::vector<std::string>& targetSegments); + Moses::Hypothesis* operator*() const { return m_hypothesis; } @@ -58,11 +60,12 @@ public: private: Moses::TranslationOption m_initialTransOpt; - Moses::Sentence m_sentence; + boost::shared_ptr<Moses::Sentence> m_sentence; Moses::WordPenaltyProducer m_wp; Moses::UnknownWordPenaltyProducer m_uwp; Moses::DistortionScoreProducer m_dist; - Moses::Manager m_manager; + boost::shared_ptr<Moses::Manager> m_manager; + boost::shared_ptr<Moses::TranslationTask> m_ttask; Moses::Hypothesis* m_hypothesis; std::vector<Moses::TargetPhrase> m_targetPhrases; std::vector<Moses::TranslationOption*> m_toptions; diff --git a/moses/Sentence.cpp b/moses/Sentence.cpp index a16eae3b2..a7a3b1a18 100644 --- a/moses/Sentence.cpp +++ b/moses/Sentence.cpp @@ -383,10 +383,12 @@ CreateFromString(vector<FactorType> const& FOrder, string const& phraseString) } Sentence:: -Sentence(size_t const transId, string const& stext) : InputType(transId) +Sentence(size_t const transId, string const& stext, + vector<FactorType> const* IFO) + : InputType(transId) { - vector<FactorType> const& IFO = StaticData::Instance().GetInputFactorOrder(); - init(stext, IFO); + if (IFO) init(stext, *IFO); + else init(stext, StaticData::Instance().GetInputFactorOrder()); } } diff --git a/moses/Sentence.h b/moses/Sentence.h index 8e7e934a6..fe3ac642e 100644 --- a/moses/Sentence.h +++ b/moses/Sentence.h @@ -63,7 +63,9 @@ namespace Moses public: Sentence(); - Sentence(size_t const transId, std::string const& stext); + Sentence(size_t const transId, std::string const& stext, + std::vector<FactorType> const* IFO = NULL); + // Sentence(size_t const transId, std::string const& stext); ~Sentence(); InputTypeEnum GetType() const { diff --git a/moses/StaticData.cpp b/moses/StaticData.cpp index 8709d758f..035136014 100644 --- a/moses/StaticData.cpp +++ b/moses/StaticData.cpp @@ -846,27 +846,33 @@ float StaticData::GetWeightWordPenalty() const return weightWP; } -void StaticData::InitializeForInput(const InputType& source) const +void +StaticData +::InitializeForInput(ttasksptr const& ttask) const { - const std::vector<FeatureFunction*> &producers = FeatureFunction::GetFeatureFunctions(); + const std::vector<FeatureFunction*> &producers = FeatureFunction::GetFeatureFunctions(); for(size_t i=0; i<producers.size(); ++i) { FeatureFunction &ff = *producers[i]; if (! IsFeatureFunctionIgnored(ff)) { Timer iTime; iTime.start(); - ff.InitializeForInput(source); - VERBOSE(3,"InitializeForInput( " << ff.GetScoreProducerDescription() << " ) = " << iTime << endl); + ff.InitializeForInput(ttask); + VERBOSE(3,"InitializeForInput( " << ff.GetScoreProducerDescription() << " )" + << "= " << iTime << endl); } } } -void StaticData::CleanUpAfterSentenceProcessing(const InputType& source) const +void +StaticData +::CleanUpAfterSentenceProcessing(ttasksptr const& ttask) const { - const std::vector<FeatureFunction*> &producers = FeatureFunction::GetFeatureFunctions(); + const std::vector<FeatureFunction*> &producers + = FeatureFunction::GetFeatureFunctions(); for(size_t i=0; i<producers.size(); ++i) { FeatureFunction &ff = *producers[i]; if (! IsFeatureFunctionIgnored(ff)) { - ff.CleanUpAfterSentenceProcessing(source); + ff.CleanUpAfterSentenceProcessing(ttask); } } } diff --git a/moses/StaticData.h b/moses/StaticData.h index e06a67b9d..a1bdf774b 100644 --- a/moses/StaticData.h +++ b/moses/StaticData.h @@ -446,18 +446,28 @@ public: SearchAlgorithm GetSearchAlgorithm() const { return m_searchAlgorithm; } - bool IsSyntax() const { - return m_searchAlgorithm == CYKPlus || - m_searchAlgorithm == ChartIncremental || - m_searchAlgorithm == SyntaxS2T || - m_searchAlgorithm == SyntaxT2S || - m_searchAlgorithm == SyntaxT2S_SCFG || - m_searchAlgorithm == SyntaxF2S; - } - const ScoreComponentCollection& GetAllWeights() const { - return m_allWeights; - } + // bool IsSyntax() const { + // return m_searchAlgorithm == CYKPlus || + // m_searchAlgorithm == ChartIncremental || + // m_searchAlgorithm == SyntaxS2T || + // m_searchAlgorithm == SyntaxT2S || + // m_searchAlgorithm == SyntaxT2S_SCFG || + // m_searchAlgorithm == SyntaxF2S; + // } + + bool IsSyntax(SearchAlgorithm algo = DefaultSearchAlgorithm) const + { + if (algo == DefaultSearchAlgorithm) + algo = m_searchAlgorithm; + return (algo == CYKPlus || algo == ChartIncremental || + algo == SyntaxS2T || algo == SyntaxT2S || + algo == SyntaxF2S || algo == SyntaxT2S_SCFG); + } + + const ScoreComponentCollection& + GetAllWeights() const + { return m_allWeights; } void SetAllWeights(const ScoreComponentCollection& weights) { m_allWeights = weights; @@ -742,8 +752,9 @@ public: } //sentence (and thread) specific initialisationn and cleanup - void InitializeForInput(const InputType& source) const; - void CleanUpAfterSentenceProcessing(const InputType& source) const; + // void InitializeForInput(const InputType& source, ttaskptr const& ttask) const; + void InitializeForInput(ttasksptr const& ttask) const; + void CleanUpAfterSentenceProcessing(ttasksptr const& ttask) const; void LoadFeatureFunctions(); bool CheckWeights() const; diff --git a/moses/TrainingTask.h b/moses/TrainingTask.h index 885e8fd16..e4d792430 100644 --- a/moses/TrainingTask.h +++ b/moses/TrainingTask.h @@ -1,9 +1,11 @@ +//-*- c++ -*- #pragma once #include <boost/smart_ptr/shared_ptr.hpp> #include "moses/ThreadPool.h" #include "moses/TranslationOptionCollection.h" #include "moses/IOWrapper.h" +#include "moses/TranslationTask.h" namespace Moses { @@ -11,35 +13,57 @@ class InputType; class OutputCollector; -class TrainingTask : public Moses::Task +class TrainingTask : public Moses::TranslationTask { +protected: + TrainingTask(boost::shared_ptr<Moses::InputType> const source, + boost::shared_ptr<Moses::IOWrapper> const ioWrapper) + : TranslationTask(source, ioWrapper) + { } + public: - TrainingTask(Moses::InputType* source, Moses::IOWrapper &ioWrapper) - : m_source(source) - , m_ioWrapper(ioWrapper) { + // factory function + static boost::shared_ptr<TrainingTask> + create(boost::shared_ptr<InputType> const& source) + { + boost::shared_ptr<IOWrapper> nix; + boost::shared_ptr<TrainingTask> ret(new TrainingTask(source, nix)); + ret->m_self = ret; + return ret; } - ~TrainingTask() { + // factory function + static boost::shared_ptr<TrainingTask> + create(boost::shared_ptr<InputType> const& source, + boost::shared_ptr<IOWrapper> const& ioWrapper) + { + boost::shared_ptr<TrainingTask> ret(new TrainingTask(source, ioWrapper)); + ret->m_self = ret; + return ret; } + ~TrainingTask() + { } + void Run() { - StaticData::Instance().InitializeForInput(*m_source); + StaticData::Instance().InitializeForInput(this->self()); std::cerr << *m_source << std::endl; - TranslationOptionCollection *transOptColl = m_source->CreateTranslationOptionCollection(); + TranslationOptionCollection *transOptColl + = m_source->CreateTranslationOptionCollection(); transOptColl->CreateTranslationOptions(); delete transOptColl; - StaticData::Instance().CleanUpAfterSentenceProcessing(*m_source); + StaticData::Instance().CleanUpAfterSentenceProcessing(this->self()); } private: - Moses::InputType* m_source; - Moses::IOWrapper &m_ioWrapper; + // Moses::InputType* m_source; + // Moses::IOWrapper &m_ioWrapper; }; diff --git a/moses/TranslationTask.cpp b/moses/TranslationTask.cpp index 2b0d47baa..9100cd3ed 100644 --- a/moses/TranslationTask.cpp +++ b/moses/TranslationTask.cpp @@ -25,6 +25,16 @@ namespace Moses boost::shared_ptr<TranslationTask> TranslationTask +::create(boost::shared_ptr<InputType> const& source) +{ + boost::shared_ptr<IOWrapper> nix; + boost::shared_ptr<TranslationTask> ret(new TranslationTask(source, nix)); + ret->m_self = ret; + return ret; +} + +boost::shared_ptr<TranslationTask> +TranslationTask ::create(boost::shared_ptr<InputType> const& source, boost::shared_ptr<IOWrapper> const& ioWrapper) { @@ -42,6 +52,59 @@ TranslationTask TranslationTask::~TranslationTask() { } + +boost::shared_ptr<BaseManager> +TranslationTask +::SetupManager(SearchAlgorithm algo) +{ + boost::shared_ptr<BaseManager> manager; + StaticData const& staticData = StaticData::Instance(); + if (algo == DefaultSearchAlgorithm) algo = staticData.GetSearchAlgorithm(); + + if (!staticData.IsSyntax(algo)) + manager.reset(new Manager(this->self())); // phrase-based + + else if (algo == SyntaxF2S || algo == SyntaxT2S) + { // STSG-based tree-to-string / forest-to-string decoding (ask Phil Williams) + typedef Syntax::F2S::RuleMatcherCallback Callback; + typedef Syntax::F2S::RuleMatcherHyperTree<Callback> RuleMatcher; + manager.reset(new Syntax::F2S::Manager<RuleMatcher>(this->self())); + } + + else if (algo == SyntaxS2T) + { // new-style string-to-tree decoding (ask Phil Williams) + S2TParsingAlgorithm algorithm = staticData.GetS2TParsingAlgorithm(); + if (algorithm == RecursiveCYKPlus) + { + typedef Syntax::S2T::EagerParserCallback Callback; + typedef Syntax::S2T::RecursiveCYKPlusParser<Callback> Parser; + manager.reset(new Syntax::S2T::Manager<Parser>(this->self())); + } + else if (algorithm == Scope3) + { + typedef Syntax::S2T::StandardParserCallback Callback; + typedef Syntax::S2T::Scope3Parser<Callback> Parser; + manager.reset(new Syntax::S2T::Manager<Parser>(this->self())); + } + else UTIL_THROW2("ERROR: unhandled S2T parsing algorithm"); + } + + else if (algo == SyntaxT2S_SCFG) + { // SCFG-based tree-to-string decoding (ask Phil Williams) + typedef Syntax::F2S::RuleMatcherCallback Callback; + typedef Syntax::T2S::RuleMatcherSCFG<Callback> RuleMatcher; + manager.reset(new Syntax::T2S::Manager<RuleMatcher>(this->self())); + } + + else if (algo == ChartIncremental) // Ken's incremental decoding + manager.reset(new Incremental::Manager(this->self())); + + else // original SCFG manager + manager.reset(new ChartManager(this->self())); + + return manager; +} + void TranslationTask::Run() { UTIL_THROW_IF2(!m_source || !m_ioWrapper, @@ -69,52 +132,22 @@ void TranslationTask::Run() Timer initTime; initTime.start(); - // which manager - boost::scoped_ptr<BaseManager> manager; - - if (!staticData.IsSyntax()) { - // phrase-based - manager.reset(new Manager(*m_source)); - } else if (staticData.GetSearchAlgorithm() == SyntaxF2S || - staticData.GetSearchAlgorithm() == SyntaxT2S) { - // STSG-based tree-to-string / forest-to-string decoding (ask Phil Williams) - typedef Syntax::F2S::RuleMatcherCallback Callback; - typedef Syntax::F2S::RuleMatcherHyperTree<Callback> RuleMatcher; - manager.reset(new Syntax::F2S::Manager<RuleMatcher>(*m_source)); - } else if (staticData.GetSearchAlgorithm() == SyntaxS2T) { - // new-style string-to-tree decoding (ask Phil Williams) - S2TParsingAlgorithm algorithm = staticData.GetS2TParsingAlgorithm(); - if (algorithm == RecursiveCYKPlus) { - typedef Syntax::S2T::EagerParserCallback Callback; - typedef Syntax::S2T::RecursiveCYKPlusParser<Callback> Parser; - manager.reset(new Syntax::S2T::Manager<Parser>(*m_source)); - } else if (algorithm == Scope3) { - typedef Syntax::S2T::StandardParserCallback Callback; - typedef Syntax::S2T::Scope3Parser<Callback> Parser; - manager.reset(new Syntax::S2T::Manager<Parser>(*m_source)); - } else { - UTIL_THROW2("ERROR: unhandled S2T parsing algorithm"); - } - } else if (staticData.GetSearchAlgorithm() == SyntaxT2S_SCFG) { - // SCFG-based tree-to-string decoding (ask Phil Williams) - typedef Syntax::F2S::RuleMatcherCallback Callback; - typedef Syntax::T2S::RuleMatcherSCFG<Callback> RuleMatcher; - manager.reset(new Syntax::T2S::Manager<RuleMatcher>(*m_source)); - } else if (staticData.GetSearchAlgorithm() == ChartIncremental) { - // Ken's incremental decoding - manager.reset(new Incremental::Manager(*m_source)); - } else { - // original SCFG manager - manager.reset(new ChartManager(*m_source)); - } + boost::shared_ptr<BaseManager> manager = SetupManager(); VERBOSE(1, "Line " << translationId << ": Initialize search took " << initTime << " seconds total" << endl); manager->Decode(); - OutputCollector* ocoll; + // new: stop here if m_ioWrapper is NULL. This means that the + // owner of the TranslationTask will take care of the output + // oh, and by the way, all the output should be handled by the + // output wrapper along the lines of *m_iwWrapper << *manager; + // Just sayin' ... + if (m_ioWrapper == NULL) return; + // we are done with search, let's look what we got + OutputCollector* ocoll; Timer additionalReportingTime; additionalReportingTime.start(); diff --git a/moses/TranslationTask.h b/moses/TranslationTask.h index acd446abe..0c800d1f2 100644 --- a/moses/TranslationTask.h +++ b/moses/TranslationTask.h @@ -70,7 +70,12 @@ public: // creator functions static boost::shared_ptr<TranslationTask> create(); - static boost::shared_ptr<TranslationTask> + static + boost::shared_ptr<TranslationTask> + create(boost::shared_ptr<Moses::InputType> const& source); + + static + boost::shared_ptr<TranslationTask> create(boost::shared_ptr<Moses::InputType> const& source, boost::shared_ptr<Moses::IOWrapper> const& ioWrapper); @@ -78,8 +83,15 @@ public: /** Translate one sentence * gets called by main function implemented at end of this source file */ virtual void Run(); + + boost::shared_ptr<Moses::InputType> + GetSource() const { return m_source; } + + boost::shared_ptr<BaseManager> + SetupManager(SearchAlgorithm algo = DefaultSearchAlgorithm); -private: + +protected: boost::shared_ptr<Moses::InputType> m_source; boost::shared_ptr<Moses::IOWrapper> m_ioWrapper; diff --git a/moses/server/TranslationRequest.cpp b/moses/server/TranslationRequest.cpp index 1953a711f..02558fa84 100644 --- a/moses/server/TranslationRequest.cpp +++ b/moses/server/TranslationRequest.cpp @@ -252,7 +252,7 @@ namespace MosesServer m_reportAllFactors = check(params, "report-all-factors"); m_nbestDistinct = check(params, "nbest-distinct"); m_withScoreBreakdown = check(params, "add-score-breakdown"); - + m_source.reset(new Sentence(0,m_source_string)); si = params.find("lambda"); if (si != params.end()) { @@ -292,7 +292,7 @@ namespace MosesServer istringstream buf(m_source_string + "\n"); tinput.Read(buf, StaticData::Instance().GetInputFactorOrder()); - Moses::ChartManager manager(tinput); + Moses::ChartManager manager(this->self()); manager.Decode(); const Moses::ChartHypothesis *hypo = manager.GetBestHypothesis(); @@ -356,7 +356,7 @@ namespace MosesServer TranslationRequest:: run_phrase_decoder() { - Manager manager(Sentence(0, m_source_string)); + Manager manager(this->self()); // if (m_bias.size()) manager.SetBias(&m_bias); manager.Decode(); |