mosesdecoder/moses-cmd/Main.cpp

191 lines
5.1 KiB
C++
Raw Normal View History

// $Id: MainMT.cpp 3045 2010-04-05 13:07:29Z hieuhoang1972 $
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2009 University of Edinburgh
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************/
/**
* Moses main, for single-threaded and multi-threaded.
**/
2012-01-13 19:20:42 +04:00
#include <exception>
#include <fstream>
#include <sstream>
#include <vector>
2012-10-05 20:49:52 +04:00
#include "util/usage.hh"
#ifdef WIN32
// Include Visual Leak Detector
//#include <vld.h>
#endif
2014-09-29 20:58:20 +04:00
#include "moses/IOWrapper.h"
2012-11-13 00:21:32 +04:00
#include "moses/Hypothesis.h"
2014-08-06 18:29:39 +04:00
#include "moses/HypergraphOutput.h"
2012-11-13 00:21:32 +04:00
#include "moses/Manager.h"
#include "moses/StaticData.h"
#include "moses/TypeDef.h"
2012-11-13 00:21:32 +04:00
#include "moses/Util.h"
#include "moses/Timer.h"
#include "moses/TranslationModel/PhraseDictionary.h"
#include "moses/FF/StatefulFeatureFunction.h"
#include "moses/FF/StatelessFeatureFunction.h"
2014-09-30 15:59:31 +04:00
#include "moses/TranslationTask.h"
#ifdef HAVE_PROTOBUF
#include "hypergraph.pb.h"
#endif
using namespace std;
using namespace Moses;
2012-07-02 20:05:11 +04:00
using namespace MosesCmd;
2012-07-02 20:05:11 +04:00
namespace MosesCmd
{
void OutputFeatureWeightsForHypergraph(std::ostream &outputSearchGraphStream)
{
outputSearchGraphStream.setf(std::ios::fixed);
outputSearchGraphStream.precision(6);
StaticData::Instance().GetAllWeights().Save(outputSearchGraphStream);
}
2012-07-02 20:05:11 +04:00
} //namespace
/** main function of the command line version of the decoder **/
int main(int argc, char** argv)
{
2012-01-13 19:20:42 +04:00
try {
2013-05-29 21:16:15 +04:00
#ifdef HAVE_PROTOBUF
2012-01-13 19:20:42 +04:00
GOOGLE_PROTOBUF_VERIFY_VERSION;
#endif
2012-01-13 19:20:42 +04:00
// echo command line, if verbose
IFVERBOSE(1) {
TRACE_ERR("command: ");
for(int i=0; i<argc; ++i) TRACE_ERR(argv[i]<<" ");
TRACE_ERR(endl);
}
2012-01-13 19:20:42 +04:00
// set number of significant decimals in output
fix(cout,PRECISION);
fix(cerr,PRECISION);
2012-01-13 19:20:42 +04:00
// load all the settings into the Parameter class
// (stores them as strings, or array of strings)
2013-03-15 16:30:39 +04:00
Parameter params;
if (!params.LoadParam(argc,argv)) {
2012-01-13 19:20:42 +04:00
exit(1);
}
2012-01-13 19:20:42 +04:00
// initialize all "global" variables, which are stored in StaticData
// note: this also loads models such as the language model, etc.
2013-03-15 16:30:39 +04:00
if (!StaticData::LoadDataStatic(&params, argv[0])) {
2012-01-13 19:20:42 +04:00
exit(1);
}
2012-01-13 19:20:42 +04:00
// setting "-show-weights" -> just dump out weights and exit
2013-03-15 16:30:39 +04:00
if (params.isParamSpecified("show-weights")) {
2012-01-13 19:20:42 +04:00
ShowWeights();
exit(0);
}
2012-01-13 19:20:42 +04:00
// shorthand for accessing information in StaticData
const StaticData& staticData = StaticData::Instance();
2012-01-13 19:20:42 +04:00
//initialise random numbers
srand(time(NULL));
2012-01-13 19:20:42 +04:00
// set up read/writing class
2014-10-03 19:08:15 +04:00
IOWrapper* ioWrapper = IOWrapper::GetIOWrapper(staticData);
2012-01-13 19:20:42 +04:00
if (!ioWrapper) {
cerr << "Error; Failed to create IO object" << endl;
exit(1);
}
2012-01-13 19:20:42 +04:00
// check on weights
const ScoreComponentCollection& weights = staticData.GetAllWeights();
2012-01-13 19:20:42 +04:00
IFVERBOSE(2) {
TRACE_ERR("The global weight vector looks like this: ");
TRACE_ERR(weights);
2012-01-13 19:20:42 +04:00
TRACE_ERR("\n");
}
boost::shared_ptr<HypergraphOutput<Manager> > hypergraphOutput;
if (staticData.GetOutputSearchGraphHypergraph()) {
hypergraphOutput.reset(new HypergraphOutput<Manager>(PRECISION));
}
#ifdef WITH_THREADS
2012-01-13 19:20:42 +04:00
ThreadPool pool(staticData.ThreadCount());
#endif
2013-05-29 21:16:15 +04:00
2012-01-13 19:20:42 +04:00
// main loop over set of input sentences
InputType* source = NULL;
size_t lineCount = staticData.GetStartTranslationId();
2012-01-13 19:20:42 +04:00
while(ReadInput(*ioWrapper,staticData.GetInputType(),source)) {
source->SetTranslationId(lineCount);
2012-01-13 19:20:42 +04:00
IFVERBOSE(1) {
ResetUserTime();
}
FeatureFunction::CallChangeSource(source);
2012-01-13 19:20:42 +04:00
// set up task of translating one sentence
TranslationTask* task =
new TranslationTask(source, *ioWrapper,
2013-05-29 21:16:15 +04:00
staticData.GetOutputSearchGraphSLF(),
2014-08-06 18:29:39 +04:00
hypergraphOutput);
2012-01-13 19:20:42 +04:00
// execute task
#ifdef WITH_THREADS
2013-05-29 21:16:15 +04:00
pool.Submit(task);
#else
2012-01-13 19:20:42 +04:00
task->Run();
delete task;
#endif
2013-05-29 21:16:15 +04:00
2012-01-13 19:20:42 +04:00
source = NULL; //make sure it doesn't get deleted
++lineCount;
}
2013-05-29 21:16:15 +04:00
// we are done, finishing up
#ifdef WITH_THREADS
2012-01-13 19:20:42 +04:00
pool.Stop(true); //flush remaining jobs
#endif
2013-03-15 20:11:15 +04:00
delete ioWrapper;
FeatureFunction::Destroy();
2013-03-15 20:11:15 +04:00
2012-01-13 19:20:42 +04:00
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
2012-10-05 20:49:52 +04:00
IFVERBOSE(1) util::PrintUsage(std::cerr);
#ifndef EXIT_RETURN
//This avoids that destructors are called (it can take a long time)
exit(EXIT_SUCCESS);
#else
return EXIT_SUCCESS;
#endif
}