00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #include <exception>
00027 #include <fstream>
00028 #include <sstream>
00029 #include <vector>
00030
00031 #include "util/random.hh"
00032 #include "util/usage.hh"
00033
00034 #ifdef WIN32
00035
00036
00037 #endif
00038
00039
00040 #include "Hypothesis.h"
00041 #include "Manager.h"
00042 #include "StaticData.h"
00043 #include "TypeDef.h"
00044 #include "Util.h"
00045 #include "Timer.h"
00046 #include "TranslationModel/PhraseDictionary.h"
00047 #include "FF/StatefulFeatureFunction.h"
00048 #include "FF/StatelessFeatureFunction.h"
00049 #include "TranslationTask.h"
00050 #include "ExportInterface.h"
00051
00052 #ifdef HAVE_PROTOBUF
00053 #include "hypergraph.pb.h"
00054 #endif
00055
00056 #ifdef PT_UG
00057 #include <boost/foreach.hpp>
00058 #include "TranslationModel/UG/mmsapt.h"
00059 #include "TranslationModel/UG/generic/program_options/ug_splice_arglist.h"
00060 #endif
00061
00062 #include "ExportInterface.h"
00063
00064 #ifdef HAVE_XMLRPC_C
00065 #include "moses/server/Server.h"
00066 #endif
00067 #include <signal.h>
00068
00069 using namespace std;
00070 using namespace Moses;
00071
00072 namespace Moses
00073 {
00074
00075
00076 void OutputFeatureWeightsForHypergraph(std::ostream &outputSearchGraphStream)
00077 {
00078 outputSearchGraphStream.setf(std::ios::fixed);
00079 outputSearchGraphStream.precision(6);
00080 StaticData::Instance().GetAllWeights().Save(outputSearchGraphStream);
00081 }
00082 }
00083
00084 SimpleTranslationInterface::SimpleTranslationInterface(const string &mosesIni): m_staticData(StaticData::Instance())
00085 {
00086 if (!m_params.LoadParam(mosesIni)) {
00087 cerr << "Error; Cannot load parameters at " << mosesIni<<endl;
00088 exit(1);
00089 }
00090 ResetUserTime();
00091 if (!StaticData::LoadDataStatic(&m_params, mosesIni.c_str())) {
00092 cerr << "Error; Cannot load static data in file " << mosesIni<<endl;
00093 exit(1);
00094 }
00095
00096 util::rand_init();
00097
00098 }
00099
00100 SimpleTranslationInterface::~SimpleTranslationInterface()
00101 {}
00102
00103
00104 string SimpleTranslationInterface::translate(const string &inputString)
00105 {
00106 boost::shared_ptr<Moses::IOWrapper> ioWrapper(new IOWrapper(*StaticData::Instance().options()));
00107
00108 size_t sentEnd = inputString.rfind('\n');
00109 const string &newString = sentEnd != string::npos ? inputString : inputString + '\n';
00110
00111 istringstream inputStream(newString);
00112 ioWrapper->SetInputStreamFromString(inputStream);
00113 ostringstream outputStream;
00114 ioWrapper->SetOutputStream2SingleBestOutputCollector(&outputStream);
00115
00116 boost::shared_ptr<InputType> source = ioWrapper->ReadInput();
00117 if (!source) return "Error: Source==null!!!";
00118 IFVERBOSE(1) {
00119 ResetUserTime();
00120 }
00121
00122
00123 boost::shared_ptr<TranslationTask> task
00124 = TranslationTask::create(source, ioWrapper);
00125 task->Run();
00126
00127 string output = outputStream.str();
00128
00129 const string whitespace = " \t\f\v\n\r";
00130 size_t end = output.find_last_not_of(whitespace);
00131 return output.erase(end + 1);
00132 }
00133
00134 Moses::StaticData& SimpleTranslationInterface::getStaticData()
00135 {
00136 return StaticData::InstanceNonConst();
00137 }
00138 void SimpleTranslationInterface::DestroyFeatureFunctionStatic()
00139 {
00140 FeatureFunction::Destroy();
00141 }
00142
00143
00144 Parameter params;
00145
00146 void
00147 signal_handler(int signum)
00148 {
00149 if (signum == SIGALRM) {
00150 exit(0);
00151 } else if (signum == SIGTERM || signum == SIGKILL) {
00152 exit(0);
00153 } else {
00154 std::cerr << "Unexpected signal " << signum << std::endl;
00155 exit(signum);
00156 }
00157 }
00158
00160 int
00161 run_as_server()
00162 {
00163 #ifdef HAVE_XMLRPC_C
00164 if (params.GetParam("daemon")) {
00165 kill(getppid(),SIGALRM);
00166 }
00167 MosesServer::Server server(params);
00168 return server.run();
00169 #else
00170 UTIL_THROW2("Moses was compiled without xmlrpc-c. "
00171 << "No server functionality available.");
00172 #endif
00173 }
00174
00175 int
00176 batch_run()
00177 {
00178
00179 const StaticData& staticData = StaticData::Instance();
00180
00181
00182 util::rand_init();
00183
00184 IFVERBOSE(1) PrintUserTime("Created input-output object");
00185
00186
00187 boost::shared_ptr<IOWrapper> ioWrapper(new IOWrapper(*staticData.options()));
00188 UTIL_THROW_IF2(ioWrapper == NULL, "Error; Failed to create IO object"
00189 << " [" << HERE << "]");
00190
00191
00192 const ScoreComponentCollection& weights = staticData.GetAllWeights();
00193 IFVERBOSE(2) {
00194 TRACE_ERR("The global weight vector looks like this: ");
00195 TRACE_ERR(weights);
00196 TRACE_ERR("\n");
00197 }
00198
00199 #ifdef WITH_THREADS
00200 ThreadPool pool(staticData.ThreadCount());
00201 #endif
00202
00203
00204
00205 std::string context_string;
00206 params.SetParameter(context_string,"context-string",string(""));
00207
00208
00209 std::string context_weights;
00210 params.SetParameter(context_weights,"context-weights",string(""));
00211
00212
00213 size_t size_t_max = std::numeric_limits<size_t>::max();
00214 bool use_context_window = ioWrapper->GetLookAhead() || ioWrapper->GetLookBack();
00215
00216 bool use_sliding_context_window = (use_context_window
00217 && ioWrapper->GetLookAhead() != size_t_max);
00218
00219 boost::shared_ptr<std::vector<std::string> > context_window;
00220 boost::shared_ptr<std::vector<std::string> >* cw;
00221 cw = use_context_window ? &context_window : NULL;
00222 if (!cw && context_string.size())
00223 context_window.reset(new std::vector<std::string>(1,context_string));
00224
00225
00226 boost::shared_ptr<ContextScope> gscope;
00227 if (!use_sliding_context_window)
00228 gscope.reset(new ContextScope);
00229
00230
00231 boost::shared_ptr<InputType> source;
00232 while ((source = ioWrapper->ReadInput(cw)) != NULL) {
00233 IFVERBOSE(1) ResetUserTime();
00234
00235
00236 boost::shared_ptr<ContextScope> lscope;
00237 if (gscope) lscope = gscope;
00238 else lscope.reset(new ContextScope);
00239
00240 boost::shared_ptr<TranslationTask> task;
00241 task = TranslationTask::create(source, ioWrapper, lscope);
00242
00243 if (cw) {
00244 if (context_string.size())
00245 context_window->push_back(context_string);
00246 if(!use_sliding_context_window)
00247 cw = NULL;
00248 }
00249
00250 if (context_window)
00251 task->SetContextWindow(context_window);
00252
00253 if (context_weights != "" && !task->GetScope()->GetContextWeights())
00254 task->GetScope()->SetContextWeights(context_weights);
00255
00256
00257
00258
00259 FeatureFunction::SetupAll(*task);
00260
00261
00262 #ifdef WITH_THREADS
00263 #ifdef PT_UG
00264
00265
00266 bool spe = params.isParamSpecified("spe-src");
00267 if (spe) {
00268
00269 task->Run();
00270 string src,trg,aln;
00271 UTIL_THROW_IF2(!getline(*ioWrapper->spe_src,src), "[" << HERE << "] "
00272 << "missing update data for simulated post-editing.");
00273 UTIL_THROW_IF2(!getline(*ioWrapper->spe_trg,trg), "[" << HERE << "] "
00274 << "missing update data for simulated post-editing.");
00275 UTIL_THROW_IF2(!getline(*ioWrapper->spe_aln,aln), "[" << HERE << "] "
00276 << "missing update data for simulated post-editing.");
00277 BOOST_FOREACH (PhraseDictionary* pd, PhraseDictionary::GetColl()) {
00278 Mmsapt* sapt = dynamic_cast<Mmsapt*>(pd);
00279 if (sapt) sapt->add(src,trg,aln);
00280 VERBOSE(1,"[" << HERE << " added src] " << src << endl);
00281 VERBOSE(1,"[" << HERE << " added trg] " << trg << endl);
00282 VERBOSE(1,"[" << HERE << " added aln] " << aln << endl);
00283 }
00284 } else pool.Submit(task);
00285 #else
00286 pool.Submit(task);
00287
00288 #endif
00289 #else
00290 task->Run();
00291 #endif
00292 }
00293
00294
00295 #ifdef WITH_THREADS
00296 pool.Stop(true);
00297 #endif
00298
00299
00300
00301 FeatureFunction::Destroy();
00302
00303 IFVERBOSE(0) util::PrintUsage(std::cerr);
00304
00305 #ifndef EXIT_RETURN
00306
00307 exit(EXIT_SUCCESS);
00308 #else
00309 return EXIT_SUCCESS;
00310 #endif
00311 }
00312
00314 int decoder_main(int argc, char const** argv)
00315 {
00316 #ifdef NDEBUG
00317 try
00318 #endif
00319 {
00320 #ifdef HAVE_PROTOBUF
00321 GOOGLE_PROTOBUF_VERIFY_VERSION;
00322 #endif
00323
00324
00325 IFVERBOSE(1) {
00326 TRACE_ERR("command: ");
00327 for(int i=0; i<argc; ++i) TRACE_ERR(argv[i]<<" ");
00328 TRACE_ERR(endl);
00329 }
00330
00331
00332 FixPrecision(cout);
00333 FixPrecision(cerr);
00334
00335
00336
00337 if (!params.LoadParam(argc,argv))
00338 exit(1);
00339
00340
00341
00342 ResetUserTime();
00343 if (!StaticData::LoadDataStatic(¶ms, argv[0]))
00344 exit(1);
00345
00346
00347 #if 1
00348 pid_t pid;
00349 if (params.GetParam("daemon")) {
00350 pid = fork();
00351 if (pid) {
00352 pause();
00353 exit(0);
00354 }
00355 }
00356 #endif
00357
00358 if (params.isParamSpecified("show-weights")) {
00359 ShowWeights();
00360 exit(0);
00361 }
00362
00363 if (params.GetParam("server")) {
00364 std::cerr << "RUN SERVER at pid " << pid << std::endl;
00365 return run_as_server();
00366 } else
00367 return batch_run();
00368 }
00369 #ifdef NDEBUG
00370 catch (const std::exception &e) {
00371 std::cerr << "Exception: " << e.what() << std::endl;
00372 return EXIT_FAILURE;
00373 }
00374 #endif
00375 }
00376