00001 #include "TranslationTask.h"
00002 #include "moses/StaticData.h"
00003 #include "moses/Sentence.h"
00004 #include "moses/IOWrapper.h"
00005 #include "moses/TranslationAnalysis.h"
00006 #include "moses/TypeDef.h"
00007 #include "moses/Util.h"
00008 #include "moses/Timer.h"
00009 #include "moses/InputType.h"
00010 #include "moses/OutputCollector.h"
00011 #include "moses/Incremental.h"
00012 #include "mbr.h"
00013
00014 #include "moses/Syntax/F2S/RuleMatcherCallback.h"
00015 #include "moses/Syntax/F2S/RuleMatcherHyperTree.h"
00016 #include "moses/Syntax/S2T/Parsers/RecursiveCYKPlusParser/RecursiveCYKPlusParser.h"
00017 #include "moses/Syntax/S2T/Parsers/Scope3Parser/Parser.h"
00018 #include "moses/Syntax/T2S/RuleMatcherSCFG.h"
00019
00020 #include "util/exception.hh"
00021
00022 using namespace std;
00023
00024 namespace Moses
00025 {
00026
00027 boost::shared_ptr<std::vector<std::string> >
00028 TranslationTask::
00029 GetContextWindow() const
00030 {
00031 return m_context;
00032 }
00033
00034 void
00035 TranslationTask::
00036 SetContextWindow(boost::shared_ptr<std::vector<std::string> > const& cw)
00037 {
00038 m_context = cw;
00039 }
00040
00041 boost::shared_ptr<TranslationTask>
00042 TranslationTask
00043 ::create(boost::shared_ptr<InputType> const& source)
00044 {
00045 boost::shared_ptr<IOWrapper> nix;
00046 boost::shared_ptr<TranslationTask> ret(new TranslationTask(source, nix));
00047 ret->m_self = ret;
00048 ret->m_scope.reset(new ContextScope);
00049 return ret;
00050 }
00051
00052 boost::shared_ptr<TranslationTask>
00053 TranslationTask
00054 ::create(boost::shared_ptr<InputType> const& source,
00055 boost::shared_ptr<IOWrapper> const& ioWrapper)
00056 {
00057 boost::shared_ptr<TranslationTask> ret(new TranslationTask(source, ioWrapper));
00058 ret->m_self = ret;
00059 ret->m_scope.reset(new ContextScope);
00060 return ret;
00061 }
00062
00063 boost::shared_ptr<TranslationTask>
00064 TranslationTask
00065 ::create(boost::shared_ptr<InputType> const& source,
00066 boost::shared_ptr<IOWrapper> const& ioWrapper,
00067 boost::shared_ptr<ContextScope> const& scope)
00068 {
00069 boost::shared_ptr<TranslationTask> ret(new TranslationTask(source, ioWrapper));
00070 ret->m_self = ret;
00071 ret->m_scope = scope;
00072 return ret;
00073 }
00074
00075 TranslationTask
00076 ::TranslationTask(boost::shared_ptr<InputType> const& source,
00077 boost::shared_ptr<IOWrapper> const& ioWrapper)
00078 : m_source(source) , m_ioWrapper(ioWrapper)
00079 {
00080 m_options = source->options();
00081 }
00082
00083 TranslationTask::~TranslationTask()
00084 { }
00085
00086
00087 boost::shared_ptr<BaseManager>
00088 TranslationTask
00089 ::SetupManager(SearchAlgorithm algo)
00090 {
00091 boost::shared_ptr<BaseManager> manager;
00092
00093
00094
00095 if (!is_syntax(algo))
00096 manager.reset(new Manager(this->self()));
00097
00098 else if (algo == SyntaxF2S || algo == SyntaxT2S) {
00099
00100 typedef Syntax::F2S::RuleMatcherCallback Callback;
00101 typedef Syntax::F2S::RuleMatcherHyperTree<Callback> RuleMatcher;
00102 manager.reset(new Syntax::F2S::Manager<RuleMatcher>(this->self()));
00103 }
00104
00105 else if (algo == SyntaxS2T) {
00106
00107 S2TParsingAlgorithm algorithm = m_options->syntax.s2t_parsing_algo;
00108 if (algorithm == RecursiveCYKPlus) {
00109 typedef Syntax::S2T::EagerParserCallback Callback;
00110 typedef Syntax::S2T::RecursiveCYKPlusParser<Callback> Parser;
00111 manager.reset(new Syntax::S2T::Manager<Parser>(this->self()));
00112 } else if (algorithm == Scope3) {
00113 typedef Syntax::S2T::StandardParserCallback Callback;
00114 typedef Syntax::S2T::Scope3Parser<Callback> Parser;
00115 manager.reset(new Syntax::S2T::Manager<Parser>(this->self()));
00116 } else UTIL_THROW2("ERROR: unhandled S2T parsing algorithm");
00117 }
00118
00119 else if (algo == SyntaxT2S_SCFG) {
00120
00121 typedef Syntax::F2S::RuleMatcherCallback Callback;
00122 typedef Syntax::T2S::RuleMatcherSCFG<Callback> RuleMatcher;
00123 manager.reset(new Syntax::T2S::Manager<RuleMatcher>(this->self()));
00124 }
00125
00126 else if (algo == ChartIncremental)
00127 manager.reset(new Incremental::Manager(this->self()));
00128
00129 else
00130 manager.reset(new ChartManager(this->self()));
00131
00132 return manager;
00133 }
00134
00135 AllOptions::ptr const&
00136 TranslationTask::
00137 options() const
00138 {
00139 return m_options;
00140 }
00141
00143 void
00144 TranslationTask::
00145 interpret_dlt()
00146 {
00147 if (m_source->GetType() != SentenceInput) return;
00148 Sentence const& snt = static_cast<Sentence const&>(*m_source);
00149 typedef std::map<std::string,std::string> dltmap_t;
00150 BOOST_FOREACH(dltmap_t const& M, snt.GetDltMeta()) {
00151 dltmap_t::const_iterator i = M.find("type");
00152 if (i == M.end() || i->second != "adaptive-lm") continue;
00153 dltmap_t::const_iterator j = M.find("context-weights");
00154 if (j == M.end()) continue;
00155 m_scope->SetContextWeights(j->second);
00156 }
00157 }
00158
00159
00160 void TranslationTask::Run()
00161 {
00162 UTIL_THROW_IF2(!m_source || !m_ioWrapper,
00163 "Base Instances of TranslationTask must be initialized with"
00164 << " input and iowrapper.");
00165
00166 const size_t translationId = m_source->GetTranslationId();
00167
00168
00169
00170 Timer translationTime;
00171 translationTime.start();
00172
00173 interpret_dlt();
00174
00175
00176 #if defined(WITH_THREADS) && defined(BOOST_HAS_PTHREADS)
00177 VERBOSE(2, "Translating line " << translationId << " in thread id "
00178 << pthread_self() << endl);
00179 #endif
00180
00181
00182
00183
00184
00185 Timer initTime;
00186 initTime.start();
00187
00188 boost::shared_ptr<BaseManager> manager = SetupManager(m_options->search.algo);
00189
00190 VERBOSE(1, "Line " << translationId << ": Initialize search took "
00191 << initTime << " seconds total" << endl);
00192
00193 manager->Decode();
00194
00195
00196
00197
00198
00199
00200 if (m_ioWrapper == NULL) return;
00201
00202
00203 OutputCollector* ocoll;
00204 Timer additionalReportingTime;
00205 additionalReportingTime.start();
00206 boost::shared_ptr<IOWrapper> const& io = m_ioWrapper;
00207
00208 manager->OutputBest(io->GetSingleBestOutputCollector());
00209
00210
00211 manager->OutputWordGraph(io->GetWordGraphCollector());
00212
00213
00214 manager->OutputSearchGraph(io->GetSearchGraphOutputCollector());
00215
00216
00217 manager->OutputSearchGraphSLF();
00218
00219
00220
00221 if (m_options->output.SearchGraphHG.size()) {
00222 size_t transId = manager->GetSource().GetTranslationId();
00223 string fname = io->GetHypergraphOutputFileName(transId);
00224 manager->OutputSearchGraphAsHypergraph(fname, PRECISION);
00225 }
00226
00227 additionalReportingTime.stop();
00228
00229 additionalReportingTime.start();
00230
00231
00232 manager->OutputNBest(io->GetNBestOutputCollector());
00233
00234
00235 manager->OutputLatticeSamples(io->GetLatticeSamplesCollector());
00236
00237
00238 ocoll = io->GetDetailedTranslationCollector();
00239 manager->OutputDetailedTranslationReport(ocoll);
00240
00241 ocoll = io->GetDetailTreeFragmentsOutputCollector();
00242 manager->OutputDetailedTreeFragmentsTranslationReport(ocoll);
00243
00244
00245 manager->OutputUnknowns(io->GetUnknownsCollector());
00246
00247 manager->OutputAlignment(io->GetAlignmentInfoCollector());
00248
00249
00250 manager->CalcDecoderStatistics();
00251 VERBOSE(1, "Line " << translationId << ": Additional reporting took "
00252 << additionalReportingTime << " seconds total" << endl);
00253 VERBOSE(1, "Line " << translationId << ": Translation took "
00254 << translationTime << " seconds total" << endl);
00255 IFVERBOSE(2) {
00256 PrintUserTime("Sentence Decoding Time:");
00257 }
00258 }
00259
00260 }