mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-12-24 20:32:59 +03:00
gut IOWrapper
This commit is contained in:
parent
cec03c949e
commit
b526efea23
@ -97,8 +97,6 @@ IOWrapper::IOWrapper()
|
||||
const StaticData &staticData = StaticData::Instance();
|
||||
|
||||
m_inputFactorOrder = &staticData.GetInputFactorOrder();
|
||||
m_outputFactorOrder = &staticData.GetOutputFactorOrder();
|
||||
m_inputFactorUsed = FactorMask(*m_inputFactorOrder);
|
||||
|
||||
size_t nBestSize = staticData.GetNBestSize();
|
||||
string nBestFilePath = staticData.GetNBestFilePath();
|
||||
@ -258,148 +256,6 @@ GetInput(InputType* inputType)
|
||||
}
|
||||
}
|
||||
|
||||
void IOWrapper::OutputTranslationOptions(std::ostream &out, ApplicationContext &applicationContext, const ChartHypothesis *hypo, const Sentence &sentence, long translationId)
|
||||
{
|
||||
if (hypo != NULL) {
|
||||
OutputTranslationOption(out, applicationContext, hypo, sentence, translationId);
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
// recursive
|
||||
const std::vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
||||
std::vector<const ChartHypothesis*>::const_iterator iter;
|
||||
for (iter = prevHypos.begin(); iter != prevHypos.end(); ++iter) {
|
||||
const ChartHypothesis *prevHypo = *iter;
|
||||
OutputTranslationOptions(out, applicationContext, prevHypo, sentence, translationId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void IOWrapper::OutputTranslationOptions(std::ostream &out, ApplicationContext &applicationContext, const search::Applied *applied, const Sentence &sentence, long translationId)
|
||||
{
|
||||
if (applied != NULL) {
|
||||
OutputTranslationOption(out, applicationContext, applied, sentence, translationId);
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
// recursive
|
||||
const search::Applied *child = applied->Children();
|
||||
for (size_t i = 0; i < applied->GetArity(); i++) {
|
||||
OutputTranslationOptions(out, applicationContext, child++, sentence, translationId);
|
||||
}
|
||||
}
|
||||
|
||||
void IOWrapper::OutputTranslationOption(std::ostream &out, ApplicationContext &applicationContext, const ChartHypothesis *hypo, const Sentence &sentence, long translationId)
|
||||
{
|
||||
ReconstructApplicationContext(*hypo, sentence, applicationContext);
|
||||
out << "Trans Opt " << translationId
|
||||
<< " " << hypo->GetCurrSourceRange()
|
||||
<< ": ";
|
||||
WriteApplicationContext(out, applicationContext);
|
||||
out << ": " << hypo->GetCurrTargetPhrase().GetTargetLHS()
|
||||
<< "->" << hypo->GetCurrTargetPhrase()
|
||||
<< " " << hypo->GetTotalScore() << hypo->GetScoreBreakdown();
|
||||
}
|
||||
|
||||
void IOWrapper::OutputTranslationOption(std::ostream &out, ApplicationContext &applicationContext, const search::Applied *applied, const Sentence &sentence, long translationId)
|
||||
{
|
||||
ReconstructApplicationContext(applied, sentence, applicationContext);
|
||||
const TargetPhrase &phrase = *static_cast<const TargetPhrase*>(applied->GetNote().vp);
|
||||
out << "Trans Opt " << translationId
|
||||
<< " " << applied->GetRange()
|
||||
<< ": ";
|
||||
WriteApplicationContext(out, applicationContext);
|
||||
out << ": " << phrase.GetTargetLHS()
|
||||
<< "->" << phrase
|
||||
<< " " << applied->GetScore(); // << hypo->GetScoreBreakdown() TODO: missing in incremental search hypothesis
|
||||
}
|
||||
|
||||
// Given a hypothesis and sentence, reconstructs the 'application context' --
|
||||
// the source RHS symbols of the SCFG rule that was applied, plus their spans.
|
||||
void IOWrapper::ReconstructApplicationContext(const ChartHypothesis &hypo,
|
||||
const Sentence &sentence,
|
||||
ApplicationContext &context)
|
||||
{
|
||||
context.clear();
|
||||
const std::vector<const ChartHypothesis*> &prevHypos = hypo.GetPrevHypos();
|
||||
std::vector<const ChartHypothesis*>::const_iterator p = prevHypos.begin();
|
||||
std::vector<const ChartHypothesis*>::const_iterator end = prevHypos.end();
|
||||
const WordsRange &span = hypo.GetCurrSourceRange();
|
||||
size_t i = span.GetStartPos();
|
||||
while (i <= span.GetEndPos()) {
|
||||
if (p == end || i < (*p)->GetCurrSourceRange().GetStartPos()) {
|
||||
// Symbol is a terminal.
|
||||
const Word &symbol = sentence.GetWord(i);
|
||||
context.push_back(std::make_pair(symbol, WordsRange(i, i)));
|
||||
++i;
|
||||
} else {
|
||||
// Symbol is a non-terminal.
|
||||
const Word &symbol = (*p)->GetTargetLHS();
|
||||
const WordsRange &range = (*p)->GetCurrSourceRange();
|
||||
context.push_back(std::make_pair(symbol, range));
|
||||
i = range.GetEndPos()+1;
|
||||
++p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Given a hypothesis and sentence, reconstructs the 'application context' --
|
||||
// the source RHS symbols of the SCFG rule that was applied, plus their spans.
|
||||
void IOWrapper::ReconstructApplicationContext(const search::Applied *applied,
|
||||
const Sentence &sentence,
|
||||
ApplicationContext &context)
|
||||
{
|
||||
context.clear();
|
||||
const WordsRange &span = applied->GetRange();
|
||||
const search::Applied *child = applied->Children();
|
||||
size_t i = span.GetStartPos();
|
||||
size_t j = 0;
|
||||
|
||||
while (i <= span.GetEndPos()) {
|
||||
if (j == applied->GetArity() || i < child->GetRange().GetStartPos()) {
|
||||
// Symbol is a terminal.
|
||||
const Word &symbol = sentence.GetWord(i);
|
||||
context.push_back(std::make_pair(symbol, WordsRange(i, i)));
|
||||
++i;
|
||||
} else {
|
||||
// Symbol is a non-terminal.
|
||||
const Word &symbol = static_cast<const TargetPhrase*>(child->GetNote().vp)->GetTargetLHS();
|
||||
const WordsRange &range = child->GetRange();
|
||||
context.push_back(std::make_pair(symbol, range));
|
||||
i = range.GetEndPos()+1;
|
||||
++child;
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Emulates the old operator<<(ostream &, const DottedRule &) function. The
|
||||
// output format is a bit odd (reverse order and double spacing between symbols)
|
||||
// but there are scripts and tools that expect the output of -T to look like
|
||||
// that.
|
||||
void IOWrapper::WriteApplicationContext(std::ostream &out,
|
||||
const ApplicationContext &context)
|
||||
{
|
||||
assert(!context.empty());
|
||||
ApplicationContext::const_reverse_iterator p = context.rbegin();
|
||||
while (true) {
|
||||
out << p->second << "=" << p->first << " ";
|
||||
if (++p == context.rend()) {
|
||||
break;
|
||||
}
|
||||
out << " ";
|
||||
}
|
||||
}
|
||||
|
||||
void IOWrapper::Backtrack(const Hypothesis *hypo)
|
||||
{
|
||||
|
||||
if (hypo->GetPrevHypo() != NULL) {
|
||||
VERBOSE(3,hypo->GetId() << " <= ");
|
||||
Backtrack(hypo->GetPrevHypo());
|
||||
}
|
||||
}
|
||||
|
||||
bool IOWrapper::ReadInput(InputTypeEnum inputType, InputType*& source)
|
||||
{
|
||||
delete source;
|
||||
|
@ -74,8 +74,6 @@ class IOWrapper
|
||||
protected:
|
||||
|
||||
const std::vector<Moses::FactorType> *m_inputFactorOrder;
|
||||
const std::vector<Moses::FactorType> *m_outputFactorOrder;
|
||||
Moses::FactorMask m_inputFactorUsed;
|
||||
std::string m_inputFilePath;
|
||||
Moses::InputFileStream *m_inputFile;
|
||||
std::istream *m_inputStream;
|
||||
@ -100,22 +98,6 @@ protected:
|
||||
|
||||
bool m_surpressSingleBestOutput;
|
||||
|
||||
// CHART
|
||||
typedef std::vector<std::pair<Moses::Word, Moses::WordsRange> > ApplicationContext;
|
||||
|
||||
void OutputTranslationOptions(std::ostream &out, ApplicationContext &applicationContext, const Moses::ChartHypothesis *hypo, const Moses::Sentence &sentence, long translationId);
|
||||
void OutputTranslationOptions(std::ostream &out, ApplicationContext &applicationContext, const search::Applied *applied, const Moses::Sentence &sentence, long translationId);
|
||||
void OutputTranslationOption(std::ostream &out, ApplicationContext &applicationContext, const Moses::ChartHypothesis *hypo, const Moses::Sentence &sentence, long translationId);
|
||||
void OutputTranslationOption(std::ostream &out, ApplicationContext &applicationContext, const search::Applied *applied, const Moses::Sentence &sentence, long translationId);
|
||||
|
||||
void ReconstructApplicationContext(const Moses::ChartHypothesis &hypo,
|
||||
const Moses::Sentence &sentence,
|
||||
ApplicationContext &context);
|
||||
void ReconstructApplicationContext(const search::Applied *applied,
|
||||
const Moses::Sentence &sentence,
|
||||
ApplicationContext &context);
|
||||
void WriteApplicationContext(std::ostream &out,
|
||||
const ApplicationContext &context);
|
||||
|
||||
public:
|
||||
IOWrapper();
|
||||
@ -124,8 +106,6 @@ public:
|
||||
Moses::InputType* GetInput(Moses::InputType *inputType);
|
||||
bool ReadInput(Moses::InputTypeEnum inputType, Moses::InputType*& source);
|
||||
|
||||
void Backtrack(const Moses::Hypothesis *hypo);
|
||||
|
||||
Moses::OutputCollector *GetSingleBestOutputCollector() {
|
||||
return m_singleBestOutputCollector;
|
||||
}
|
||||
@ -162,11 +142,6 @@ public:
|
||||
return m_detailTreeFragmentsOutputCollector;
|
||||
}
|
||||
|
||||
|
||||
// CHART
|
||||
|
||||
// phrase-based
|
||||
|
||||
// post editing
|
||||
std::ifstream *spe_src, *spe_trg, *spe_aln;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user