cleaning up IOWrapper. Make all functions member of the class

This commit is contained in:
Hieu Hoang 2014-10-13 12:46:58 +01:00
parent 8f00505648
commit 90e4eca0a3
6 changed files with 77 additions and 69 deletions

View File

@ -310,7 +310,7 @@ public:
}
if (addWordAlignInfo) {
stringstream wordAlignment;
OutputAlignment(wordAlignment, hypo);
IOWrapper::OutputAlignment(wordAlignment, hypo);
vector<xmlrpc_c::value> alignments;
string alignmentPair;
while (wordAlignment >> alignmentPair) {
@ -472,7 +472,7 @@ public:
if ((int)edges.size() > 0) {
stringstream wordAlignment;
OutputAlignment(wordAlignment, edges[0]);
IOWrapper::OutputAlignment(wordAlignment, edges[0]);
vector<xmlrpc_c::value> alignments;
string alignmentPair;
while (wordAlignment >> alignmentPair) {
@ -490,7 +490,7 @@ public:
{
// should the score breakdown be reported in a more structured manner?
ostringstream buf;
Moses::OutputAllFeatureScores(path.GetScoreBreakdown(),buf);
IOWrapper::OutputAllFeatureScores(path.GetScoreBreakdown(),buf);
nBestXMLItem["fvals"] = xmlrpc_c::value_string(buf.str());
}

View File

@ -177,7 +177,7 @@ int main(int argc, char* argv[])
const vector<float>& prune_grid = grid.getGrid(lmbr_prune);
const vector<float>& scale_grid = grid.getGrid(lmbr_scale);
while(ioWrapper->ReadInput(*ioWrapper,staticData.GetInputType(),source)) {
while(ioWrapper->ReadInput(staticData.GetInputType(),source)) {
++lineCount;
source->SetTranslationId(lineCount);
@ -200,7 +200,7 @@ int main(int argc, char* argv[])
staticData.SetMBRScale(scale);
cout << lineCount << " ||| " << p << " " << r << " " << prune << " " << scale << " ||| ";
vector<Word> mbrBestHypo = doLatticeMBR(manager,nBestList);
OutputBestHypo(mbrBestHypo, lineCount, staticData.GetReportSegmentation(),
ioWrapper->OutputBestHypo(mbrBestHypo, lineCount, staticData.GetReportSegmentation(),
staticData.GetReportAllFactors(),cout);
}
}

View File

@ -148,7 +148,7 @@ int main(int argc, char** argv)
// main loop over set of input sentences
InputType* source = NULL;
size_t lineCount = staticData.GetStartTranslationId();
while(ioWrapper->ReadInput(*ioWrapper,staticData.GetInputType(),source)) {
while(ioWrapper->ReadInput(staticData.GetInputType(),source)) {
source->SetTranslationId(lineCount);
IFVERBOSE(1) {
ResetUserTime();

View File

@ -242,7 +242,7 @@ void IOWrapper::FixPrecision(std::ostream &stream, size_t size)
stream.precision(size);
}
std::map<size_t, const Factor*> GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor)
std::map<size_t, const Factor*> IOWrapper::GetPlaceholders(const Hypothesis &hypo, FactorType placeholderFactor)
{
const InputPath &inputPath = hypo.GetTranslationOption().GetInputPath();
const Phrase &inputPhrase = inputPath.GetPhrase();
@ -973,7 +973,7 @@ size_t IOWrapper::OutputAlignmentNBest(
/***
* print surface factor only for the given phrase
*/
void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector<FactorType> &outputFactorOrder,
void IOWrapper::OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector<FactorType> &outputFactorOrder,
char reportSegmentation, bool reportAllFactors)
{
UTIL_THROW_IF2(outputFactorOrder.size() == 0,
@ -1046,7 +1046,7 @@ void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector<
}
}
void OutputBestSurface(std::ostream &out, const Hypothesis *hypo, const std::vector<FactorType> &outputFactorOrder,
void IOWrapper::OutputBestSurface(std::ostream &out, const Hypothesis *hypo, const std::vector<FactorType> &outputFactorOrder,
char reportSegmentation, bool reportAllFactors)
{
if (hypo != NULL) {
@ -1056,7 +1056,7 @@ void OutputBestSurface(std::ostream &out, const Hypothesis *hypo, const std::vec
}
}
void OutputAlignment(ostream &out, const AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset)
void IOWrapper::OutputAlignment(ostream &out, const AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset)
{
typedef std::vector< const std::pair<size_t,size_t>* > AlignVec;
AlignVec alignments = ai.GetSortedAlignments();
@ -1069,7 +1069,7 @@ void OutputAlignment(ostream &out, const AlignmentInfo &ai, size_t sourceOffset,
}
void OutputAlignment(ostream &out, const vector<const Hypothesis *> &edges)
void IOWrapper::OutputAlignment(ostream &out, const vector<const Hypothesis *> &edges)
{
size_t targetOffset = 0;
@ -1084,7 +1084,7 @@ void OutputAlignment(ostream &out, const vector<const Hypothesis *> &edges)
}
}
void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo)
void IOWrapper::OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo)
{
std::vector<const Hypothesis *> edges;
const Hypothesis *currentHypo = hypo;
@ -1097,7 +1097,7 @@ void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo)
}
void OutputAlignment(OutputCollector* collector, size_t lineNo , const vector<const Hypothesis *> &edges)
void IOWrapper::OutputAlignment(OutputCollector* collector, size_t lineNo , const vector<const Hypothesis *> &edges)
{
ostringstream out;
OutputAlignment(out, edges);
@ -1105,7 +1105,7 @@ void OutputAlignment(OutputCollector* collector, size_t lineNo , const vector<co
collector->Write(lineNo,out.str());
}
void OutputAlignment(OutputCollector* collector, size_t lineNo , const Hypothesis *hypo)
void IOWrapper::OutputAlignment(OutputCollector* collector, size_t lineNo , const Hypothesis *hypo)
{
if (collector) {
std::vector<const Hypothesis *> edges;
@ -1119,14 +1119,14 @@ void OutputAlignment(OutputCollector* collector, size_t lineNo , const Hypothesi
}
}
void OutputAlignment(OutputCollector* collector, size_t lineNo , const TrellisPath &path)
void IOWrapper::OutputAlignment(OutputCollector* collector, size_t lineNo , const TrellisPath &path)
{
if (collector) {
OutputAlignment(collector,lineNo, path.GetEdges());
}
}
void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/, char reportSegmentation, bool reportAllFactors, std::ostream &out)
void IOWrapper::OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/, char reportSegmentation, bool reportAllFactors, std::ostream &out)
{
const std::vector<const Hypothesis *> &edges = path.GetEdges();
@ -1146,7 +1146,7 @@ void IOWrapper::Backtrack(const Hypothesis *hypo)
}
}
void OutputBestHypo(const std::vector<Word>& mbrBestHypo, long /*translationId*/, char /*reportSegmentation*/, bool /*reportAllFactors*/, ostream& out)
void IOWrapper::OutputBestHypo(const std::vector<Word>& mbrBestHypo, long /*translationId*/, char /*reportSegmentation*/, bool /*reportAllFactors*/, ostream& out)
{
for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) {
@ -1160,7 +1160,7 @@ void OutputBestHypo(const std::vector<Word>& mbrBestHypo, long /*translationId*
}
void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo)
void IOWrapper::OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo)
{
if (hypo->GetPrevHypo()) {
OutputInput(map, hypo->GetPrevHypo());
@ -1168,7 +1168,7 @@ void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo)
}
}
void OutputInput(std::ostream& os, const Hypothesis* hypo)
void IOWrapper::OutputInput(std::ostream& os, const Hypothesis* hypo)
{
size_t len = hypo->GetInput().GetSize();
std::vector<const Phrase*> inp_phrases(len, 0);
@ -1204,21 +1204,21 @@ void IOWrapper::OutputBestHypo(const Hypothesis *hypo, long /*translationId*/, c
}
}
bool IOWrapper::ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputType*& source)
bool IOWrapper::ReadInput(InputTypeEnum inputType, InputType*& source)
{
delete source;
switch(inputType) {
case SentenceInput:
source = ioWrapper.GetInput(new Sentence);
source = GetInput(new Sentence);
break;
case ConfusionNetworkInput:
source = ioWrapper.GetInput(new ConfusionNet);
source = GetInput(new ConfusionNet);
break;
case WordLatticeInput:
source = ioWrapper.GetInput(new WordLattice);
source = GetInput(new WordLattice);
break;
case TreeInputType:
source = ioWrapper.GetInput(new TreeInput);
source = GetInput(new TreeInput);
break;
default:
TRACE_ERR("Unknown input type: " << inputType << "\n");
@ -1226,7 +1226,7 @@ bool IOWrapper::ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputTy
return (source ? true : false);
}
void OutputNBest(std::ostream& out
void IOWrapper::OutputNBest(std::ostream& out
, const Moses::TrellisPathList &nBestList
, const std::vector<Moses::FactorType>& outputFactorOrder
, long translationId
@ -1300,7 +1300,7 @@ void OutputNBest(std::ostream& out
out << std::flush;
}
void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features
void IOWrapper::OutputAllFeatureScores(const Moses::ScoreComponentCollection &features
, std::ostream &out)
{
std::string lastName = "";
@ -1321,7 +1321,7 @@ void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features
}
}
void OutputFeatureScores( std::ostream& out
void IOWrapper::OutputFeatureScores( std::ostream& out
, const ScoreComponentCollection &features
, const FeatureFunction *ff
, std::string &lastName )
@ -1348,7 +1348,7 @@ void OutputFeatureScores( std::ostream& out
}
}
void OutputLatticeMBRNBest(std::ostream& out, const vector<LatticeMBRSolution>& solutions,long translationId)
void IOWrapper::OutputLatticeMBRNBest(std::ostream& out, const vector<LatticeMBRSolution>& solutions,long translationId)
{
for (vector<LatticeMBRSolution>::const_iterator si = solutions.begin(); si != solutions.end(); ++si) {
out << translationId;

View File

@ -32,8 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
// example file on how to use moses library
#ifndef moses_cmd_IOWrapper_h
#define moses_cmd_IOWrapper_h
#pragma once
#include <cassert>
#include <fstream>
@ -125,6 +124,8 @@ protected:
long translationId);
void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector<FactorType> &outputFactorOrder, bool reportAllFactors);
void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector<FactorType> &outputFactorOrder,
char reportSegmentation, bool reportAllFactors);
size_t OutputAlignment(Alignments &retAlign, const Moses::ChartHypothesis *hypo, size_t startTarget);
size_t OutputAlignmentNBest(Alignments &retAlign,
@ -160,7 +161,7 @@ public:
~IOWrapper();
Moses::InputType* GetInput(Moses::InputType *inputType);
bool ReadInput(IOWrapper &ioWrapper, Moses::InputTypeEnum inputType, Moses::InputType*& source);
bool ReadInput(Moses::InputTypeEnum inputType, Moses::InputType*& source);
void OutputBestHypo(const Moses::Hypothesis *hypo, long translationId, char reportSegmentation, bool reportAllFactors);
void OutputLatticeMBRNBestList(const std::vector<LatticeMBRSolution>& solutions,long translationId);
@ -218,35 +219,42 @@ public:
const Moses::Sentence &sentence,
long translationId);
// phrase-based
void OutputBestSurface(std::ostream &out, const Moses::Hypothesis *hypo, const std::vector<Moses::FactorType> &outputFactorOrder, char reportSegmentation, bool reportAllFactors);
void OutputLatticeMBRNBest(std::ostream& out, const std::vector<LatticeMBRSolution>& solutions,long translationId);
void OutputBestHypo(const std::vector<Moses::Word>& mbrBestHypo, long /*translationId*/,
char reportSegmentation, bool reportAllFactors, std::ostream& out);
void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,char reportSegmentation, bool reportAllFactors, std::ostream &out);
void OutputInput(std::ostream& os, const Moses::Hypothesis* hypo);
void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo);
void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::Hypothesis *hypo);
void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::TrellisPath &path);
void OutputAlignment(OutputCollector* collector, size_t lineNo , const std::vector<const Hypothesis *> &edges);
static void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo);
static void OutputAlignment(std::ostream &out, const std::vector<const Hypothesis *> &edges);
static void OutputAlignment(std::ostream &out, const Moses::AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset);
void OutputNBest(std::ostream& out
, const Moses::TrellisPathList &nBestList
, const std::vector<Moses::FactorType>& outputFactorOrder
, long translationId
, char reportSegmentation);
static void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features
, std::ostream &out);
static void OutputFeatureScores( std::ostream& out
, const Moses::ScoreComponentCollection &features
, const Moses::FeatureFunction *ff
, std::string &lastName );
// creates a map of TARGET positions which should be replaced by word using placeholder
std::map<size_t, const Moses::Factor*> GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor);
};
void OutputBestSurface(std::ostream &out, const Moses::Hypothesis *hypo, const std::vector<Moses::FactorType> &outputFactorOrder, char reportSegmentation, bool reportAllFactors);
void OutputLatticeMBRNBest(std::ostream& out, const std::vector<LatticeMBRSolution>& solutions,long translationId);
void OutputBestHypo(const std::vector<Moses::Word>& mbrBestHypo, long /*translationId*/,
char reportSegmentation, bool reportAllFactors, std::ostream& out);
void OutputBestHypo(const Moses::TrellisPath &path, long /*translationId*/,char reportSegmentation, bool reportAllFactors, std::ostream &out);
void OutputInput(std::ostream& os, const Moses::Hypothesis* hypo);
void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::Hypothesis *hypo);
void OutputAlignment(Moses::OutputCollector* collector, size_t lineNo, const Moses::TrellisPath &path);
void OutputAlignment(std::ostream &out, const Moses::Hypothesis *hypo);
void OutputAlignment(std::ostream &out, const Moses::AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset);
void OutputNBest(std::ostream& out
, const Moses::TrellisPathList &nBestList
, const std::vector<Moses::FactorType>& outputFactorOrder
, long translationId
, char reportSegmentation);
void OutputAllFeatureScores(const Moses::ScoreComponentCollection &features
, std::ostream &out);
void OutputFeatureScores( std::ostream& out
, const Moses::ScoreComponentCollection &features
, const Moses::FeatureFunction *ff
, std::string &lastName );
// creates a map of TARGET positions which should be replaced by word using placeholder
std::map<size_t, const Moses::Factor*> GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor);
}
#endif

View File

@ -163,7 +163,7 @@ void TranslationTask::RunPb()
out << bestHypo->GetTotalScore() << ' ';
}
if (staticData.IsPathRecoveryEnabled()) {
OutputInput(out, bestHypo);
m_ioWrapper.OutputInput(out, bestHypo);
out << "||| ";
}
if (staticData.GetParam("print-id").size() && Scan<bool>(staticData.GetParam("print-id")[0]) ) {
@ -173,7 +173,7 @@ void TranslationTask::RunPb()
if (staticData.GetReportSegmentation() == 2) {
manager.GetOutputLanguageModelOrder(out, bestHypo);
}
OutputBestSurface(
m_ioWrapper.OutputBestSurface(
out,
bestHypo,
staticData.GetOutputFactorOrder(),
@ -181,10 +181,10 @@ void TranslationTask::RunPb()
staticData.GetReportAllFactors());
if (staticData.PrintAlignmentInfo()) {
out << "||| ";
OutputAlignment(out, bestHypo);
m_ioWrapper.OutputAlignment(out, bestHypo);
}
OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), bestHypo);
m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), bestHypo);
IFVERBOSE(1) {
debug << "BEST TRANSLATION: " << *bestHypo << endl;
}
@ -218,12 +218,12 @@ void TranslationTask::RunPb()
size_t n = min(nBestSize, staticData.GetNBestSize());
getLatticeMBRNBest(manager,nBestList,solutions,n);
ostringstream out;
OutputLatticeMBRNBest(out, solutions,m_source->GetTranslationId());
m_ioWrapper.OutputLatticeMBRNBest(out, solutions,m_source->GetTranslationId());
m_ioWrapper.GetNBestOutputCollector()->Write(m_source->GetTranslationId(), out.str());
} else {
//Lattice MBR decoding
vector<Word> mbrBestHypo = doLatticeMBR(manager,nBestList);
OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(),
m_ioWrapper.OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(), staticData.GetReportSegmentation(),
staticData.GetReportAllFactors(),out);
IFVERBOSE(2) {
PrintUserTime("finished Lattice MBR decoding");
@ -234,10 +234,10 @@ void TranslationTask::RunPb()
// consensus decoding
else if (staticData.UseConsensusDecoding()) {
const TrellisPath &conBestHypo = doConsensusDecoding(manager,nBestList);
OutputBestHypo(conBestHypo, m_source->GetTranslationId(),
m_ioWrapper.OutputBestHypo(conBestHypo, m_source->GetTranslationId(),
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors(),out);
OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), conBestHypo);
m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), conBestHypo);
IFVERBOSE(2) {
PrintUserTime("finished Consensus decoding");
}
@ -246,10 +246,10 @@ void TranslationTask::RunPb()
// n-best MBR decoding
else {
const TrellisPath &mbrBestHypo = doMBR(nBestList);
OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(),
m_ioWrapper.OutputBestHypo(mbrBestHypo, m_source->GetTranslationId(),
staticData.GetReportSegmentation(),
staticData.GetReportAllFactors(),out);
OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), mbrBestHypo);
m_ioWrapper.OutputAlignment(m_ioWrapper.GetAlignmentInfoCollector(), m_source->GetTranslationId(), mbrBestHypo);
IFVERBOSE(2) {
PrintUserTime("finished MBR decoding");
}
@ -270,7 +270,7 @@ void TranslationTask::RunPb()
TrellisPathList nBestList;
ostringstream out;
manager.CalcNBest(staticData.GetNBestSize(), nBestList,staticData.GetDistinctNBest());
OutputNBest(out, nBestList, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(),
m_ioWrapper.OutputNBest(out, nBestList, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(),
staticData.GetReportSegmentation());
m_ioWrapper.GetNBestOutputCollector()->Write(m_source->GetTranslationId(), out.str());
}
@ -280,7 +280,7 @@ void TranslationTask::RunPb()
TrellisPathList latticeSamples;
ostringstream out;
manager.CalcLatticeSamples(staticData.GetLatticeSamplesSize(), latticeSamples);
OutputNBest(out,latticeSamples, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(),
m_ioWrapper.OutputNBest(out,latticeSamples, staticData.GetOutputFactorOrder(), m_source->GetTranslationId(),
staticData.GetReportSegmentation());
m_ioWrapper.GetLatticeSamplesCollector()->Write(m_source->GetTranslationId(), out.str());
}