replace CHECK with UTIL_THROW_IF in moses-chart-cmd

This commit is contained in:
Hieu Hoang 2013-11-18 19:03:30 +00:00
parent 3ffc5e7a8f
commit d424e6985b
4 changed files with 52 additions and 26 deletions

View File

@ -50,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "moses/FeatureVector.h" #include "moses/FeatureVector.h"
#include "moses/FF/StatefulFeatureFunction.h" #include "moses/FF/StatefulFeatureFunction.h"
#include "moses/FF/StatelessFeatureFunction.h" #include "moses/FF/StatelessFeatureFunction.h"
#include "util/exception.hh"
using namespace std; using namespace std;
using namespace Moses; using namespace Moses;
@ -127,7 +128,9 @@ IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
if (!staticData.GetAlignmentOutputFile().empty()) { if (!staticData.GetAlignmentOutputFile().empty()) {
m_alignmentInfoStream = new std::ofstream(staticData.GetAlignmentOutputFile().c_str()); m_alignmentInfoStream = new std::ofstream(staticData.GetAlignmentOutputFile().c_str());
m_alignmentInfoCollector = new Moses::OutputCollector(m_alignmentInfoStream); m_alignmentInfoCollector = new Moses::OutputCollector(m_alignmentInfoStream);
CHECK(m_alignmentInfoStream->good()); UTIL_THROW_IF(!m_alignmentInfoStream->good(),
util::Exception,
"File for alignment output could not be opened: " << staticData.GetAlignmentOutputFile());
} }
} }
@ -172,7 +175,9 @@ InputType*IOWrapper::GetInput(InputType* inputType)
*/ */
void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector<FactorType> &outputFactorOrder, bool reportAllFactors) void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector<FactorType> &outputFactorOrder, bool reportAllFactors)
{ {
CHECK(outputFactorOrder.size() > 0); UTIL_THROW_IF(outputFactorOrder.size() == 0,
util::Exception,
"Cannot be empty phrase");
if (reportAllFactors == true) { if (reportAllFactors == true) {
out << phrase; out << phrase;
} else { } else {
@ -180,11 +185,15 @@ void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector<Fa
for (size_t pos = 0 ; pos < size ; pos++) { for (size_t pos = 0 ; pos < size ; pos++) {
const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[0]); const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[0]);
out << *factor; out << *factor;
CHECK(factor); UTIL_THROW_IF(factor == NULL,
util::Exception,
"Empty factor 0 at position " << pos);
for (size_t i = 1 ; i < outputFactorOrder.size() ; i++) { for (size_t i = 1 ; i < outputFactorOrder.size() ; i++) {
const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[i]); const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[i]);
CHECK(factor); UTIL_THROW_IF(factor == NULL,
util::Exception,
"Empty factor " << i << " at position " << pos);
out << "|" << *factor; out << "|" << *factor;
} }
@ -227,7 +236,8 @@ void IOWrapper::OutputBestHypo(const std::vector<const Factor*>& mbrBestHypo, l
{ {
for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) { for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) {
const Factor *factor = mbrBestHypo[i]; const Factor *factor = mbrBestHypo[i];
CHECK(factor); UTIL_THROW_IF(factor == NULL, util::Exception,
"No factor at position " << i );
cout << *factor << " "; cout << *factor << " ";
} }
@ -368,7 +378,9 @@ void IOWrapper::OutputDetailedTranslationReport(
ApplicationContext applicationContext; ApplicationContext applicationContext;
OutputTranslationOptions(out, applicationContext, hypo, sentence, translationId); OutputTranslationOptions(out, applicationContext, hypo, sentence, translationId);
CHECK(m_detailOutputCollector); UTIL_THROW_IF(m_detailOutputCollector == NULL,
util::Exception,
"No ouput file for detailed reports specified");
m_detailOutputCollector->Write(translationId, out.str()); m_detailOutputCollector->Write(translationId, out.str());
} }
@ -384,7 +396,9 @@ void IOWrapper::OutputDetailedTreeFragmentsTranslationReport(
ApplicationContext applicationContext; ApplicationContext applicationContext;
OutputTreeFragmentsTranslationOptions(out, applicationContext, hypo, sentence, translationId); OutputTreeFragmentsTranslationOptions(out, applicationContext, hypo, sentence, translationId);
CHECK(m_detailTreeFragmentsOutputCollector); UTIL_THROW_IF(m_detailTreeFragmentsOutputCollector == NULL,
util::Exception,
"No output file for tree fragments specified");
m_detailTreeFragmentsOutputCollector->Write(translationId, out.str()); m_detailTreeFragmentsOutputCollector->Write(translationId, out.str());
} }
@ -417,7 +431,9 @@ void IOWrapper::OutputDetailedAllTranslationReport(
} }
} }
} }
CHECK(m_detailAllOutputCollector); UTIL_THROW_IF(m_detailAllOutputCollector == NULL,
util::Exception,
"No output file for details specified");
m_detailAllOutputCollector->Write(translationId, out.str()); m_detailAllOutputCollector->Write(translationId, out.str());
} }
@ -444,7 +460,9 @@ void IOWrapper::OutputBestHypo(const ChartHypothesis *hypo, long translationId)
hypo->GetOutputPhrase(outPhrase); hypo->GetOutputPhrase(outPhrase);
// delete 1st & last // delete 1st & last
CHECK(outPhrase.GetSize() >= 2); UTIL_THROW_IF(outPhrase.GetSize() < 2, util::Exception,
"Output phrase should have contained at least 2 words (beginning and end-of-sentence)");
outPhrase.RemoveWord(0); outPhrase.RemoveWord(0);
outPhrase.RemoveWord(outPhrase.GetSize() - 1); outPhrase.RemoveWord(outPhrase.GetSize() - 1);
@ -474,7 +492,8 @@ void IOWrapper::OutputBestHypo(search::Applied applied, long translationId)
Phrase outPhrase; Phrase outPhrase;
Incremental::ToPhrase(applied, outPhrase); Incremental::ToPhrase(applied, outPhrase);
// delete 1st & last // delete 1st & last
CHECK(outPhrase.GetSize() >= 2); UTIL_THROW_IF(outPhrase.GetSize() < 2, util::Exception,
"Output phrase should have contained at least 2 words (beginning and end-of-sentence)");
outPhrase.RemoveWord(0); outPhrase.RemoveWord(0);
outPhrase.RemoveWord(outPhrase.GetSize() - 1); outPhrase.RemoveWord(outPhrase.GetSize() - 1);
out << outPhrase.GetStringRep(StaticData::Instance().GetOutputFactorOrder()); out << outPhrase.GetStringRep(StaticData::Instance().GetOutputFactorOrder());
@ -562,7 +581,9 @@ void IOWrapper::OutputNBestList(const ChartTrellisPathList &nBestList, long tran
Moses::Phrase outputPhrase = path.GetOutputPhrase(); Moses::Phrase outputPhrase = path.GetOutputPhrase();
// delete 1st & last // delete 1st & last
CHECK(outputPhrase.GetSize() >= 2); UTIL_THROW_IF(outputPhrase.GetSize() < 2, util::Exception,
"Output phrase should have contained at least 2 words (beginning and end-of-sentence)");
outputPhrase.RemoveWord(0); outputPhrase.RemoveWord(0);
outputPhrase.RemoveWord(outputPhrase.GetSize() - 1); outputPhrase.RemoveWord(outputPhrase.GetSize() - 1);
@ -636,7 +657,9 @@ void IOWrapper::OutputNBestList(const std::vector<search::Applied> &nbest, long
for (std::vector<search::Applied>::const_iterator i = nbest.begin(); i != nbest.end(); ++i) { for (std::vector<search::Applied>::const_iterator i = nbest.begin(); i != nbest.end(); ++i) {
Incremental::PhraseAndFeatures(*i, outputPhrase, features); Incremental::PhraseAndFeatures(*i, outputPhrase, features);
// <s> and </s> // <s> and </s>
CHECK(outputPhrase.GetSize() >= 2); UTIL_THROW_IF(outputPhrase.GetSize() < 2, util::Exception,
"Output phrase should have contained at least 2 words (beginning and end-of-sentence)");
outputPhrase.RemoveWord(0); outputPhrase.RemoveWord(0);
outputPhrase.RemoveWord(outputPhrase.GetSize() - 1); outputPhrase.RemoveWord(outputPhrase.GetSize() - 1);
out << translationId << " ||| "; out << translationId << " ||| ";
@ -703,12 +726,12 @@ size_t IOWrapper::OutputAlignmentNBest(Alignments &retAlign, const Moses::ChartT
vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap(); vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap();
const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap(); const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap();
CHECK(sourceInd2pos.size() == prevNodes.size()); UTIL_THROW_IF(sourceInd2pos.size() != prevNodes.size(), util::Exception, "Error");
size_t targetInd = 0; size_t targetInd = 0;
for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) { for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
if (tp.GetWord(targetPos).IsNonTerminal()) { if (tp.GetWord(targetPos).IsNonTerminal()) {
CHECK(targetPos < targetPos2SourceInd.size()); UTIL_THROW_IF(targetPos >= targetPos2SourceInd.size(), util::Exception, "Error");
size_t sourceInd = targetPos2SourceInd[targetPos]; size_t sourceInd = targetPos2SourceInd[targetPos];
size_t sourcePos = sourceInd2pos[sourceInd]; size_t sourcePos = sourceInd2pos[sourceInd];
@ -750,7 +773,7 @@ size_t IOWrapper::OutputAlignmentNBest(Alignments &retAlign, const Moses::ChartT
pair<size_t, size_t> alignPoint(absSource, absTarget); pair<size_t, size_t> alignPoint(absSource, absTarget);
pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint); pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
CHECK(ret.second); UTIL_THROW_IF(!ret.second, util::Exception, "Error");
} }
return totalTargetSize; return totalTargetSize;
@ -796,12 +819,12 @@ size_t IOWrapper::OutputAlignment(Alignments &retAlign, const Moses::ChartHypoth
vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap(); vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap();
const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap(); const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap();
CHECK(sourceInd2pos.size() == prevHypos.size()); UTIL_THROW_IF(sourceInd2pos.size() != prevHypos.size(), util::Exception, "Error");
size_t targetInd = 0; size_t targetInd = 0;
for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) { for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
if (tp.GetWord(targetPos).IsNonTerminal()) { if (tp.GetWord(targetPos).IsNonTerminal()) {
CHECK(targetPos < targetPos2SourceInd.size()); UTIL_THROW_IF(targetPos >= targetPos2SourceInd.size(), util::Exception, "Error");
size_t sourceInd = targetPos2SourceInd[targetPos]; size_t sourceInd = targetPos2SourceInd[targetPos];
size_t sourcePos = sourceInd2pos[sourceInd]; size_t sourcePos = sourceInd2pos[sourceInd];
@ -843,7 +866,7 @@ size_t IOWrapper::OutputAlignment(Alignments &retAlign, const Moses::ChartHypoth
pair<size_t, size_t> alignPoint(absSource, absTarget); pair<size_t, size_t> alignPoint(absSource, absTarget);
pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint); pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
CHECK(ret.second); UTIL_THROW_IF(!ret.second, util::Exception, "Error");
} }
@ -859,9 +882,9 @@ void IOWrapper::OutputAlignment(vector< set<size_t> > &retAlignmentsS2T, const A
for (it = alignments.begin(); it != alignments.end(); ++it) { for (it = alignments.begin(); it != alignments.end(); ++it) {
const std::pair<size_t,size_t> &alignPoint = **it; const std::pair<size_t,size_t> &alignPoint = **it;
CHECK(alignPoint.first < retAlignmentsS2T.size()); UTIL_THROW_IF(alignPoint.first >= retAlignmentsS2T.size(), util::Exception, "Error");
pair<set<size_t>::iterator, bool> ret = retAlignmentsS2T[alignPoint.first].insert(alignPoint.second); pair<set<size_t>::iterator, bool> ret = retAlignmentsS2T[alignPoint.first].insert(alignPoint.second);
CHECK(ret.second); UTIL_THROW_IF(!ret.second, util::Exception, "Error");
} }
} }

View File

@ -63,6 +63,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "moses/FF/StatelessFeatureFunction.h" #include "moses/FF/StatelessFeatureFunction.h"
#include "util/usage.hh" #include "util/usage.hh"
#include "util/exception.hh"
using namespace std; using namespace std;
using namespace Moses; using namespace Moses;
@ -112,7 +113,7 @@ public:
ChartManager manager(*m_source); ChartManager manager(*m_source);
manager.ProcessSentence(); manager.ProcessSentence();
CHECK(!staticData.UseMBR()); UTIL_THROW_IF(staticData.UseMBR(), util::Exception, "Cannot use MBR");
// 1-best // 1-best
const ChartHypothesis *bestHypo = manager.GetBestHypothesis(); const ChartHypothesis *bestHypo = manager.GetBestHypothesis();
@ -159,7 +160,7 @@ public:
std::ostringstream out; std::ostringstream out;
manager.GetSearchGraph(translationId, out); manager.GetSearchGraph(translationId, out);
OutputCollector *oc = m_ioWrapper.GetSearchGraphOutputCollector(); OutputCollector *oc = m_ioWrapper.GetSearchGraphOutputCollector();
CHECK(oc); UTIL_THROW_IF(oc == NULL, util::Exception, "File for search graph output not specified");
oc->Write(translationId, out.str()); oc->Write(translationId, out.str());
} }
@ -260,7 +261,7 @@ int main(int argc, char* argv[])
exit(0); exit(0);
} }
CHECK(staticData.IsChart()); UTIL_THROW_IF(!staticData.IsChart(), util::Exception, "Must be SCFG model");
// set up read/writing class // set up read/writing class
IOWrapper *ioWrapper = GetIOWrapper(staticData); IOWrapper *ioWrapper = GetIOWrapper(staticData);

View File

@ -18,10 +18,10 @@
***********************************************************************/ ***********************************************************************/
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include "util/check.hh"
#include "AlignmentInfo.h" #include "AlignmentInfo.h"
#include "TypeDef.h" #include "TypeDef.h"
#include "StaticData.h" #include "StaticData.h"
#include "util/exception.hh"
namespace Moses namespace Moses
{ {
@ -112,7 +112,7 @@ std::vector< const std::pair<size_t,size_t>* > AlignmentInfo::GetSortedAlignment
break; break;
default: default:
CHECK(false); UTIL_THROW(util::Exception, "Unknown alignment sort option: " << wordAlignmentSort);
} }
return ret; return ret;

View File

@ -30,6 +30,7 @@
#include "ChartTranslationOptions.h" #include "ChartTranslationOptions.h"
#include "ChartTranslationOptionList.h" #include "ChartTranslationOptionList.h"
#include "ChartManager.h" #include "ChartManager.h"
#include "util/exception.hh"
using namespace std; using namespace std;
@ -108,7 +109,8 @@ void ChartCell::ProcessSentence(const ChartTranslationOptionList &transOptList
//! call SortHypotheses() in each hypo collection in this cell //! call SortHypotheses() in each hypo collection in this cell
void ChartCell::SortHypotheses() void ChartCell::SortHypotheses()
{ {
CHECK(m_targetLabelSet.Empty()); UTIL_THROW_IF(!m_targetLabelSet.Empty(), util::Exception, "Already sorted");
MapType::iterator iter; MapType::iterator iter;
for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) { for (iter = m_hypoColl.begin(); iter != m_hypoColl.end(); ++iter) {
ChartHypothesisCollection &coll = iter->second; ChartHypothesisCollection &coll = iter->second;