From 8b61f396a7558bf628c2e94a9583023b9ae34a8c Mon Sep 17 00:00:00 2001 From: James Zhang Date: Thu, 5 Feb 2015 14:35:49 -0500 Subject: [PATCH] Simple structure change to make moses main() function available in libmoses.a --- moses-cmd/Main.cpp | 190 +---------------- moses/ExportInterface.cpp | 215 ++++++++++++++++++++ moses-cmd/Main.h => moses/ExportInterface.h | 2 +- 3 files changed, 220 insertions(+), 187 deletions(-) create mode 100644 moses/ExportInterface.cpp rename moses-cmd/Main.h => moses/ExportInterface.h (97%) diff --git a/moses-cmd/Main.cpp b/moses-cmd/Main.cpp index f88f186b5..7de3206fd 100644 --- a/moses-cmd/Main.cpp +++ b/moses-cmd/Main.cpp @@ -20,195 +20,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ***********************************************************************/ /** - * Moses main, for single-threaded and multi-threaded. + * Moses main wrapper for executable for single-threaded and multi-threaded, simply calling decoder_main. **/ -#include -#include -#include -#include - -#include "util/usage.hh" - -#ifdef WIN32 -// Include Visual Leak Detector -//#include -#endif - -#include "moses/IOWrapper.h" -#include "moses/Hypothesis.h" -#include "moses/Manager.h" -#include "moses/StaticData.h" -#include "moses/TypeDef.h" -#include "moses/Util.h" -#include "moses/Timer.h" -#include "moses/TranslationModel/PhraseDictionary.h" -#include "moses/FF/StatefulFeatureFunction.h" -#include "moses/FF/StatelessFeatureFunction.h" -#include "moses/TranslationTask.h" - -#ifdef HAVE_PROTOBUF -#include "hypergraph.pb.h" -#endif - -#ifdef PT_UG -#include -#include "moses/TranslationModel/UG/mmsapt.h" -#include "moses/TranslationModel/UG/generic/program_options/ug_splice_arglist.h" -#endif - -using namespace std; -using namespace Moses; - -namespace Moses -{ - -void OutputFeatureWeightsForHypergraph(std::ostream &outputSearchGraphStream) -{ - outputSearchGraphStream.setf(std::ios::fixed); - outputSearchGraphStream.precision(6); - StaticData::Instance().GetAllWeights().Save(outputSearchGraphStream); -} - - -} //namespace +#include "moses/ExportInterface.h" /** main function of the command line version of the decoder **/ int main(int argc, char** argv) { - try { - -#ifdef HAVE_PROTOBUF - GOOGLE_PROTOBUF_VERIFY_VERSION; -#endif - - // echo command line, if verbose - IFVERBOSE(1) { - TRACE_ERR("command: "); - for(int i=0; i just dump out weights and exit - if (params.isParamSpecified("show-weights")) { - ShowWeights(); - exit(0); - } - - // shorthand for accessing information in StaticData - const StaticData& staticData = StaticData::Instance(); - - - //initialise random numbers - srand(time(NULL)); - - // set up read/writing class - IFVERBOSE(1) { - PrintUserTime("Created input-output object"); - } - - IOWrapper* ioWrapper = new IOWrapper(); - if (ioWrapper == NULL) { - cerr << "Error; Failed to create IO object" << endl; - exit(1); - } - - // check on weights - const ScoreComponentCollection& weights = staticData.GetAllWeights(); - IFVERBOSE(2) { - TRACE_ERR("The global weight vector looks like this: "); - TRACE_ERR(weights); - TRACE_ERR("\n"); - } - -#ifdef WITH_THREADS - ThreadPool pool(staticData.ThreadCount()); -#endif - - // main loop over set of input sentences - InputType* source = NULL; - size_t lineCount = staticData.GetStartTranslationId(); - while(ioWrapper->ReadInput(staticData.GetInputType(),source)) { - source->SetTranslationId(lineCount); - IFVERBOSE(1) { - ResetUserTime(); - } - - FeatureFunction::CallChangeSource(source); - - // set up task of translating one sentence - TranslationTask* task = new TranslationTask(source, *ioWrapper); - - // execute task -#ifdef WITH_THREADS -#ifdef PT_UG - bool spe = params.isParamSpecified("spe-src"); - if (spe) { - // simulated post-editing: always run single-threaded! - task->Run(); - delete task; - string src,trg,aln; - UTIL_THROW_IF2(!getline(*ioWrapper->spe_src,src), "[" << HERE << "] " - << "missing update data for simulated post-editing."); - UTIL_THROW_IF2(!getline(*ioWrapper->spe_trg,trg), "[" << HERE << "] " - << "missing update data for simulated post-editing."); - UTIL_THROW_IF2(!getline(*ioWrapper->spe_aln,aln), "[" << HERE << "] " - << "missing update data for simulated post-editing."); - BOOST_FOREACH (PhraseDictionary* pd, PhraseDictionary::GetColl()) { - Mmsapt* sapt = dynamic_cast(pd); - if (sapt) sapt->add(src,trg,aln); - VERBOSE(1,"[" << HERE << " added src] " << src << endl); - VERBOSE(1,"[" << HERE << " added trg] " << trg << endl); - VERBOSE(1,"[" << HERE << " added aln] " << aln << endl); - } - } else -#endif - pool.Submit(task); -#else - task->Run(); - delete task; -#endif - - source = NULL; //make sure it doesn't get deleted - ++lineCount; - } - - // we are done, finishing up -#ifdef WITH_THREADS - pool.Stop(true); //flush remaining jobs -#endif - - delete ioWrapper; - FeatureFunction::Destroy(); - - } catch (const std::exception &e) { - std::cerr << "Exception: " << e.what() << std::endl; - return EXIT_FAILURE; - } - - 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 + return decoder_main(argc, argv); } + diff --git a/moses/ExportInterface.cpp b/moses/ExportInterface.cpp new file mode 100644 index 000000000..87affdbed --- /dev/null +++ b/moses/ExportInterface.cpp @@ -0,0 +1,215 @@ +// $Id: ExportInterface.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 interface for main function, for single-threaded and multi-threaded. + **/ +#include +#include +#include +#include + +#include "util/usage.hh" + +#ifdef WIN32 +// Include Visual Leak Detector +//#include +#endif + +#include "IOWrapper.h" +#include "Hypothesis.h" +#include "Manager.h" +#include "StaticData.h" +#include "TypeDef.h" +#include "Util.h" +#include "Timer.h" +#include "TranslationModel/PhraseDictionary.h" +#include "FF/StatefulFeatureFunction.h" +#include "FF/StatelessFeatureFunction.h" +#include "TranslationTask.h" + +#ifdef HAVE_PROTOBUF +#include "hypergraph.pb.h" +#endif + +#ifdef PT_UG +#include +#include "TranslationModel/UG/mmsapt.h" +#include "TranslationModel/UG/generic/program_options/ug_splice_arglist.h" +#endif + +using namespace std; +using namespace Moses; + +namespace Moses +{ + +void OutputFeatureWeightsForHypergraph(std::ostream &outputSearchGraphStream) +{ + outputSearchGraphStream.setf(std::ios::fixed); + outputSearchGraphStream.precision(6); + StaticData::Instance().GetAllWeights().Save(outputSearchGraphStream); +} + + +} //namespace + +/** Called by main function of the command line version of the decoder **/ +int decoder_main(int argc, char** argv) +{ + try { + +#ifdef HAVE_PROTOBUF + GOOGLE_PROTOBUF_VERIFY_VERSION; +#endif + + // echo command line, if verbose + IFVERBOSE(1) { + TRACE_ERR("command: "); + for(int i=0; i just dump out weights and exit + if (params.isParamSpecified("show-weights")) { + ShowWeights(); + exit(0); + } + + // shorthand for accessing information in StaticData + const StaticData& staticData = StaticData::Instance(); + + + //initialise random numbers + srand(time(NULL)); + + // set up read/writing class + IFVERBOSE(1) { + PrintUserTime("Created input-output object"); + } + + IOWrapper* ioWrapper = new IOWrapper(); + if (ioWrapper == NULL) { + cerr << "Error; Failed to create IO object" << endl; + exit(1); + } + + // check on weights + const ScoreComponentCollection& weights = staticData.GetAllWeights(); + IFVERBOSE(2) { + TRACE_ERR("The global weight vector looks like this: "); + TRACE_ERR(weights); + TRACE_ERR("\n"); + } + +#ifdef WITH_THREADS + ThreadPool pool(staticData.ThreadCount()); +#endif + + // main loop over set of input sentences + InputType* source = NULL; + size_t lineCount = staticData.GetStartTranslationId(); + while(ioWrapper->ReadInput(staticData.GetInputType(),source)) { + source->SetTranslationId(lineCount); + IFVERBOSE(1) { + ResetUserTime(); + } + + FeatureFunction::CallChangeSource(source); + + // set up task of translating one sentence + TranslationTask* task = new TranslationTask(source, *ioWrapper); + + // execute task +#ifdef WITH_THREADS +#ifdef PT_UG + bool spe = params.isParamSpecified("spe-src"); + if (spe) { + // simulated post-editing: always run single-threaded! + task->Run(); + delete task; + string src,trg,aln; + UTIL_THROW_IF2(!getline(*ioWrapper->spe_src,src), "[" << HERE << "] " + << "missing update data for simulated post-editing."); + UTIL_THROW_IF2(!getline(*ioWrapper->spe_trg,trg), "[" << HERE << "] " + << "missing update data for simulated post-editing."); + UTIL_THROW_IF2(!getline(*ioWrapper->spe_aln,aln), "[" << HERE << "] " + << "missing update data for simulated post-editing."); + BOOST_FOREACH (PhraseDictionary* pd, PhraseDictionary::GetColl()) { + Mmsapt* sapt = dynamic_cast(pd); + if (sapt) sapt->add(src,trg,aln); + VERBOSE(1,"[" << HERE << " added src] " << src << endl); + VERBOSE(1,"[" << HERE << " added trg] " << trg << endl); + VERBOSE(1,"[" << HERE << " added aln] " << aln << endl); + } + } else +#endif + pool.Submit(task); +#else + task->Run(); + delete task; +#endif + + source = NULL; //make sure it doesn't get deleted + ++lineCount; + } + + // we are done, finishing up +#ifdef WITH_THREADS + pool.Stop(true); //flush remaining jobs +#endif + + delete ioWrapper; + FeatureFunction::Destroy(); + + } catch (const std::exception &e) { + std::cerr << "Exception: " << e.what() << std::endl; + return EXIT_FAILURE; + } + + 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 +} + diff --git a/moses-cmd/Main.h b/moses/ExportInterface.h similarity index 97% rename from moses-cmd/Main.h rename to moses/ExportInterface.h index 49fee0219..8f5b3b7f6 100644 --- a/moses-cmd/Main.h +++ b/moses/ExportInterface.h @@ -38,5 +38,5 @@ POSSIBILITY OF SUCH DAMAGE. class IOWrapper; -int main(int argc, char* argv[]); +int decoder_main(int argc, char* argv[]);