From 3ffc5e7a8f24103011fcf60bf94071cf2436054c Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Mon, 18 Nov 2013 18:27:01 +0000 Subject: [PATCH] replace CHECK with UTIL_THROW_IF in moses-cmd --- moses-cmd/IOWrapper.cpp | 29 ++++++++++++++++++++++------- moses-cmd/LatticeMBRGrid.cpp | 5 ++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/moses-cmd/IOWrapper.cpp b/moses-cmd/IOWrapper.cpp index 3e983e3a5..b0a97fd85 100644 --- a/moses-cmd/IOWrapper.cpp +++ b/moses-cmd/IOWrapper.cpp @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "moses/InputFileStream.h" #include "moses/FF/StatefulFeatureFunction.h" #include "moses/FF/StatelessFeatureFunction.h" +#include "util/exception.hh" #include "IOWrapper.h" @@ -166,13 +167,17 @@ void IOWrapper::Initialization(const std::vector &/*inputFactorOrder if (staticData.IsDetailedTranslationReportingEnabled()) { const std::string &path = staticData.GetDetailedTranslationReportingFilePath(); m_detailedTranslationReportingStream = new std::ofstream(path.c_str()); - CHECK(m_detailedTranslationReportingStream->good()); + UTIL_THROW_IF(!m_detailedTranslationReportingStream->good(), + util::FileOpenException, + "File for output of detailed translation report could not be open"); } // sentence alignment output if (! staticData.GetAlignmentOutputFile().empty()) { m_alignmentOutputStream = new ofstream(staticData.GetAlignmentOutputFile().c_str()); - CHECK(m_alignmentOutputStream->good()); + UTIL_THROW_IF(!m_alignmentOutputStream->good(), + util::FileOpenException, + "File for output of word alignment could not be open"); } } @@ -202,7 +207,9 @@ std::map GetPlaceholders(const Hypothesis &hypo, FactorTy const Factor *factor = inputPhrase.GetFactor(sourcePos, placeholderFactor); if (factor) { std::set targetPos = hypo.GetTranslationOption().GetTargetPhrase().GetAlignTerm().GetAlignmentsForSource(sourcePos); - CHECK(targetPos.size() == 1); + UTIL_THROW_IF(targetPos.size() != 1, + util::Exception, + "Placeholder should be aligned to 1, and only 1, word"); ret[*targetPos.begin()] = factor; } } @@ -216,7 +223,9 @@ std::map GetPlaceholders(const Hypothesis &hypo, FactorTy void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector &outputFactorOrder, char reportSegmentation, bool reportAllFactors) { - CHECK(outputFactorOrder.size() > 0); + UTIL_THROW_IF(outputFactorOrder.size() == 0, + util::Exception, + "Must specific at least 1 output factor"); const TargetPhrase& phrase = edge.GetCurrTargetPhrase(); bool markUnknown = StaticData::Instance().GetMarkUnknown(); if (reportAllFactors == true) { @@ -242,7 +251,9 @@ void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector< } } - CHECK(factor); + UTIL_THROW_IF(factor == NULL, + util::Exception, + "No factor 0 at position " << pos); //preface surface form with UNK if marking unknowns const Word &word = phrase.GetWord(pos); @@ -254,7 +265,9 @@ void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector< for (size_t i = 1 ; i < outputFactorOrder.size() ; i++) { const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[i]); - CHECK(factor); + UTIL_THROW_IF(factor == NULL, + util::Exception, + "No factor " << i << " at position " << pos); out << "|" << *factor; } @@ -389,7 +402,9 @@ void OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId* for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) { const Factor *factor = mbrBestHypo[i].GetFactor(StaticData::Instance().GetOutputFactorOrder()[0]); - CHECK(factor); + UTIL_THROW_IF(factor == NULL, + util::Exception, + "No factor 0 at position " << i); if (i>0) out << " " << *factor; else out << *factor; } diff --git a/moses-cmd/LatticeMBRGrid.cpp b/moses-cmd/LatticeMBRGrid.cpp index c6de2ae74..41a1377ad 100644 --- a/moses-cmd/LatticeMBRGrid.cpp +++ b/moses-cmd/LatticeMBRGrid.cpp @@ -50,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "LatticeMBR.h" #include "moses/Manager.h" #include "moses/StaticData.h" +#include "util/exception.hh" using namespace std; @@ -68,7 +69,9 @@ public: /** Add a parameter with key, command line argument, and default value */ void addParam(gridkey key, const string& arg, float defaultValue) { m_args[arg] = key; - CHECK(m_grid.find(key) == m_grid.end()); + UTIL_THROW_IF(m_grid.find(key) != m_grid.end(), + util::Exception, + "Couldn't find value for key " << (int) key); m_grid[key].push_back(defaultValue); }