mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 19:27:11 +03:00
code clean - move creating wordgraph file to iostream class
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@1581 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
parent
4ee06bbf92
commit
2d95df359e
@ -57,22 +57,11 @@ IOStream::IOStream(
|
||||
,m_inputFile(NULL)
|
||||
,m_inputStream(&std::cin)
|
||||
,m_nBestStream(NULL)
|
||||
,m_outputWordGraphStream(NULL)
|
||||
{
|
||||
m_surpressSingleBestOutput = false;
|
||||
if (nBestSize > 0)
|
||||
{
|
||||
if (nBestFilePath == "-")
|
||||
{
|
||||
m_nBestStream = &std::cout;
|
||||
m_surpressSingleBestOutput = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ofstream *nBestFile = new std::ofstream;
|
||||
m_nBestStream = nBestFile;
|
||||
nBestFile->open(nBestFilePath.c_str());
|
||||
}
|
||||
}
|
||||
Initialization(inputFactorOrder, outputFactorOrder
|
||||
, inputFactorUsed
|
||||
, nBestSize, nBestFilePath);
|
||||
}
|
||||
|
||||
IOStream::IOStream(const std::vector<FactorType> &inputFactorOrder
|
||||
@ -87,24 +76,13 @@ IOStream::IOStream(const std::vector<FactorType> &inputFactorOrder
|
||||
,m_inputFilePath(inputFilePath)
|
||||
,m_inputFile(new InputFileStream(inputFilePath))
|
||||
,m_nBestStream(NULL)
|
||||
,m_outputWordGraphStream(NULL)
|
||||
{
|
||||
m_surpressSingleBestOutput = false;
|
||||
m_inputStream = m_inputFile;
|
||||
Initialization(inputFactorOrder, outputFactorOrder
|
||||
, inputFactorUsed
|
||||
, nBestSize, nBestFilePath);
|
||||
|
||||
if (nBestSize > 0)
|
||||
{
|
||||
if (nBestFilePath == "-")
|
||||
{
|
||||
m_nBestStream = &std::cout;
|
||||
m_surpressSingleBestOutput = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ofstream *nBestFile = new std::ofstream;
|
||||
m_nBestStream = nBestFile;
|
||||
nBestFile->open(nBestFilePath.c_str());
|
||||
}
|
||||
}
|
||||
m_inputStream = m_inputFile;
|
||||
}
|
||||
|
||||
IOStream::~IOStream()
|
||||
@ -115,6 +93,47 @@ IOStream::~IOStream()
|
||||
{ // outputting n-best to file, rather than stdout. need to close file and delete obj
|
||||
delete m_nBestStream;
|
||||
}
|
||||
if (m_outputWordGraphStream != NULL)
|
||||
{
|
||||
delete m_outputWordGraphStream;
|
||||
}
|
||||
}
|
||||
|
||||
void IOStream::Initialization(const std::vector<FactorType> &inputFactorOrder
|
||||
, const std::vector<FactorType> &outputFactorOrder
|
||||
, const FactorMask &inputFactorUsed
|
||||
, size_t nBestSize
|
||||
, const std::string &nBestFilePath)
|
||||
{
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
|
||||
// n-best
|
||||
m_surpressSingleBestOutput = false;
|
||||
if (nBestSize > 0)
|
||||
{
|
||||
if (nBestFilePath == "-")
|
||||
{
|
||||
m_nBestStream = &std::cout;
|
||||
m_surpressSingleBestOutput = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ofstream *file = new std::ofstream;
|
||||
m_nBestStream = file;
|
||||
file->open(nBestFilePath.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// wordgraph output
|
||||
if (staticData.GetOutputWordGraph())
|
||||
{
|
||||
string fileName = staticData.GetParam("output-word-graph")[0];
|
||||
bool outputNBest = Scan<bool>(staticData.GetParam("output-word-graph")[1]);
|
||||
|
||||
std::ofstream *file = new std::ofstream;
|
||||
m_outputWordGraphStream = file;
|
||||
file->open(fileName.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
InputType*IOStream::GetInput(InputType* inputType)
|
||||
|
@ -53,12 +53,19 @@ protected:
|
||||
const std::vector<FactorType> &m_inputFactorOrder;
|
||||
const std::vector<FactorType> &m_outputFactorOrder;
|
||||
const FactorMask &m_inputFactorUsed;
|
||||
std::ostream *m_nBestStream;
|
||||
std::ostream *m_nBestStream
|
||||
,*m_outputWordGraphStream;
|
||||
std::string m_inputFilePath;
|
||||
std::istream *m_inputStream;
|
||||
InputFileStream *m_inputFile;
|
||||
bool m_surpressSingleBestOutput;
|
||||
|
||||
void Initialization(const std::vector<FactorType> &inputFactorOrder
|
||||
, const std::vector<FactorType> &outputFactorOrder
|
||||
, const FactorMask &inputFactorUsed
|
||||
, size_t nBestSize
|
||||
, const std::string &nBestFilePath);
|
||||
|
||||
public:
|
||||
IOStream(const std::vector<FactorType> &inputFactorOrder
|
||||
, const std::vector<FactorType> &outputFactorOrder
|
||||
@ -82,4 +89,8 @@ public:
|
||||
|
||||
void ResetTranslationId() { m_translationId = 0; }
|
||||
|
||||
std::ostream &GetOutputWordGraphStream()
|
||||
{
|
||||
return *m_outputWordGraphStream;
|
||||
}
|
||||
};
|
||||
|
@ -138,7 +138,7 @@ int main(int argc, char* argv[])
|
||||
manager.ProcessSentence();
|
||||
|
||||
if (staticData.GetOutputWordGraph())
|
||||
manager.GetWordGraph(source->GetTranslationId());
|
||||
manager.GetWordGraph(source->GetTranslationId(), ioStream->GetOutputWordGraphStream());
|
||||
|
||||
// pick best translation (maximum a posteriori decoding)
|
||||
if (! staticData.UseMBR()) {
|
||||
|
@ -481,7 +481,7 @@ void Manager::CalcDecoderStatistics() const
|
||||
}
|
||||
}
|
||||
|
||||
void OutputWordGraph(std::ofstream &wordGraphFile, const Hypothesis *hypo, size_t &linkId)
|
||||
void OutputWordGraph(std::ostream &outputWordGraphStream, const Hypothesis *hypo, size_t &linkId)
|
||||
{
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
|
||||
@ -490,7 +490,7 @@ void OutputWordGraph(std::ofstream &wordGraphFile, const Hypothesis *hypo, size_
|
||||
const Phrase &targetPhrase = hypo->GetCurrTargetPhrase();
|
||||
|
||||
|
||||
wordGraphFile << "J=" << linkId++
|
||||
outputWordGraphStream << "J=" << linkId++
|
||||
<< "\tS=" << prevHypo->GetId()
|
||||
<< "\tE=" << hypo->GetId()
|
||||
<< "\ta=";
|
||||
@ -503,16 +503,16 @@ void OutputWordGraph(std::ofstream &wordGraphFile, const Hypothesis *hypo, size_
|
||||
const PhraseDictionary *phraseTable = *iterPhraseTable;
|
||||
vector<float> scores = hypo->GetScoreBreakdown().GetScoresForProducer(phraseTable);
|
||||
|
||||
wordGraphFile << scores[0];
|
||||
outputWordGraphStream << scores[0];
|
||||
vector<float>::const_iterator iterScore;
|
||||
for (iterScore = ++scores.begin() ; iterScore != scores.end() ; ++iterScore)
|
||||
{
|
||||
wordGraphFile << ", " << *iterScore;
|
||||
outputWordGraphStream << ", " << *iterScore;
|
||||
}
|
||||
}
|
||||
|
||||
// language model scores
|
||||
wordGraphFile << "\tl=";
|
||||
outputWordGraphStream << "\tl=";
|
||||
const LMList &lmList = staticData.GetAllLM();
|
||||
LMList::const_iterator iterLM;
|
||||
for (iterLM = lmList.begin() ; iterLM != lmList.end() ; ++iterLM)
|
||||
@ -520,18 +520,18 @@ void OutputWordGraph(std::ofstream &wordGraphFile, const Hypothesis *hypo, size_
|
||||
LanguageModel *lm = *iterLM;
|
||||
vector<float> scores = hypo->GetScoreBreakdown().GetScoresForProducer(lm);
|
||||
|
||||
wordGraphFile << scores[0];
|
||||
outputWordGraphStream << scores[0];
|
||||
vector<float>::const_iterator iterScore;
|
||||
for (iterScore = ++scores.begin() ; iterScore != scores.end() ; ++iterScore)
|
||||
{
|
||||
wordGraphFile << ", " << *iterScore;
|
||||
outputWordGraphStream << ", " << *iterScore;
|
||||
}
|
||||
}
|
||||
|
||||
// re-ordering
|
||||
wordGraphFile << "\tr=";
|
||||
outputWordGraphStream << "\tr=";
|
||||
|
||||
wordGraphFile << hypo->GetScoreBreakdown().GetScoreForProducer(staticData.GetDistortionScoreProducer());
|
||||
outputWordGraphStream << hypo->GetScoreBreakdown().GetScoreForProducer(staticData.GetDistortionScoreProducer());
|
||||
|
||||
// lexicalised re-ordering
|
||||
const std::vector<LexicalReordering*> &lexOrderings = staticData.GetReorderModels();
|
||||
@ -541,30 +541,27 @@ void OutputWordGraph(std::ofstream &wordGraphFile, const Hypothesis *hypo, size_
|
||||
LexicalReordering *lexicalReordering = *iterLexOrdering;
|
||||
vector<float> scores = hypo->GetScoreBreakdown().GetScoresForProducer(lexicalReordering);
|
||||
|
||||
wordGraphFile << scores[0];
|
||||
outputWordGraphStream << scores[0];
|
||||
vector<float>::const_iterator iterScore;
|
||||
for (iterScore = ++scores.begin() ; iterScore != scores.end() ; ++iterScore)
|
||||
{
|
||||
wordGraphFile << ", " << *iterScore;
|
||||
outputWordGraphStream << ", " << *iterScore;
|
||||
}
|
||||
}
|
||||
|
||||
// words !!
|
||||
wordGraphFile << "\tw=" << hypo->GetCurrTargetPhrase();
|
||||
outputWordGraphStream << "\tw=" << hypo->GetCurrTargetPhrase();
|
||||
|
||||
wordGraphFile << endl;
|
||||
outputWordGraphStream << endl;
|
||||
}
|
||||
|
||||
void Manager::GetWordGraph(long translationId) const
|
||||
void Manager::GetWordGraph(long translationId, std::ostream &outputWordGraphStream) const
|
||||
{
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
string fileName = staticData.GetParam("output-word-graph")[0];
|
||||
bool outputNBest = Scan<bool>(staticData.GetParam("output-word-graph")[1]);
|
||||
|
||||
std::ofstream wordGraphFile;
|
||||
wordGraphFile.open(fileName.c_str(), ios::app);
|
||||
|
||||
wordGraphFile << "VERSION=1.0" << endl
|
||||
outputWordGraphStream << "VERSION=1.0" << endl
|
||||
<< "UTTERANCE=" << translationId << endl;
|
||||
|
||||
size_t linkId = 0;
|
||||
@ -578,7 +575,7 @@ void Manager::GetWordGraph(long translationId) const
|
||||
for (iterHypo = stack.begin() ; iterHypo != stack.end() ; ++iterHypo)
|
||||
{
|
||||
const Hypothesis *hypo = *iterHypo;
|
||||
OutputWordGraph(wordGraphFile, hypo, linkId);
|
||||
OutputWordGraph(outputWordGraphStream, hypo, linkId);
|
||||
|
||||
if (outputNBest)
|
||||
{
|
||||
@ -589,13 +586,11 @@ void Manager::GetWordGraph(long translationId) const
|
||||
for (iterArcList = arcList->begin() ; iterArcList != arcList->end() ; ++iterArcList)
|
||||
{
|
||||
const Hypothesis *loserHypo = *iterArcList;
|
||||
OutputWordGraph(wordGraphFile, loserHypo, linkId);
|
||||
OutputWordGraph(outputWordGraphStream, loserHypo, linkId);
|
||||
}
|
||||
}
|
||||
} //if (outputNBest)
|
||||
} //for (iterHypo
|
||||
} // for (iterStack
|
||||
|
||||
wordGraphFile.close();
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ public:
|
||||
const Hypothesis *GetBestHypothesis() const;
|
||||
void CalcNBest(size_t count, TrellisPathList &ret,bool onlyDistinct=0) const;
|
||||
|
||||
void GetWordGraph(long translationId) const;
|
||||
void GetWordGraph(long translationId, std::ostream &outputWordGraphStream) const;
|
||||
|
||||
/***
|
||||
* to be called after processing a sentence (which may consist of more than just calling ProcessSentence() )
|
||||
|
Loading…
Reference in New Issue
Block a user