2010-07-18 03:23:09 +04:00
|
|
|
// $Id$
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (c) 2006 University of Edinburgh
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the University of Edinburgh nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software
|
|
|
|
without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
|
|
|
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
|
|
|
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
// example file on how to use moses library
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "IOWrapper.h"
|
2012-11-13 00:21:32 +04:00
|
|
|
#include "moses/TypeDef.h"
|
|
|
|
#include "moses/Util.h"
|
|
|
|
#include "moses/WordsRange.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/InputFileStream.h"
|
2012-11-16 18:46:10 +04:00
|
|
|
#include "moses/Incremental.h"
|
2012-11-27 19:08:31 +04:00
|
|
|
#include "moses/TranslationModel/PhraseDictionary.h"
|
2012-11-13 00:21:32 +04:00
|
|
|
#include "moses/ChartTrellisPathList.h"
|
|
|
|
#include "moses/ChartTrellisPath.h"
|
2012-11-15 20:42:19 +04:00
|
|
|
#include "moses/ChartTrellisNode.h"
|
2012-11-13 00:21:32 +04:00
|
|
|
#include "moses/ChartTranslationOptions.h"
|
|
|
|
#include "moses/ChartHypothesis.h"
|
|
|
|
#include "moses/FeatureVector.h"
|
2011-06-27 19:13:15 +04:00
|
|
|
|
2012-01-20 19:35:55 +04:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Moses;
|
|
|
|
|
2012-07-02 20:05:11 +04:00
|
|
|
namespace MosesChartCmd
|
|
|
|
{
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
IOWrapper::IOWrapper(const std::vector<FactorType> &inputFactorOrder
|
2011-02-24 15:40:21 +03:00
|
|
|
, const std::vector<FactorType> &outputFactorOrder
|
|
|
|
, const FactorMask &inputFactorUsed
|
|
|
|
, size_t nBestSize
|
|
|
|
, const std::string &nBestFilePath
|
|
|
|
, const std::string &inputFilePath)
|
|
|
|
:m_inputFactorOrder(inputFactorOrder)
|
|
|
|
,m_outputFactorOrder(outputFactorOrder)
|
|
|
|
,m_inputFactorUsed(inputFactorUsed)
|
|
|
|
,m_outputSearchGraphStream(NULL)
|
|
|
|
,m_detailedTranslationReportingStream(NULL)
|
2012-11-15 19:52:35 +04:00
|
|
|
,m_alignmentInfoStream(NULL)
|
2011-02-24 15:40:21 +03:00
|
|
|
,m_inputFilePath(inputFilePath)
|
|
|
|
,m_detailOutputCollector(NULL)
|
|
|
|
,m_nBestOutputCollector(NULL)
|
|
|
|
,m_searchGraphOutputCollector(NULL)
|
|
|
|
,m_singleBestOutputCollector(NULL)
|
2012-11-15 19:52:35 +04:00
|
|
|
,m_alignmentInfoCollector(NULL)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:40:21 +03:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
if (m_inputFilePath.empty()) {
|
2010-05-07 22:43:34 +04:00
|
|
|
m_inputStream = &std::cin;
|
2011-02-24 15:40:21 +03:00
|
|
|
} else {
|
2010-05-07 22:43:34 +04:00
|
|
|
m_inputStream = new InputFileStream(inputFilePath);
|
|
|
|
}
|
|
|
|
|
2012-09-21 17:46:43 +04:00
|
|
|
bool suppressSingleBestOutput = false;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
if (nBestSize > 0) {
|
|
|
|
if (nBestFilePath == "-") {
|
2012-09-21 17:46:43 +04:00
|
|
|
m_nBestOutputCollector = new Moses::OutputCollector(&std::cout);
|
|
|
|
suppressSingleBestOutput = true;
|
2011-02-24 15:40:21 +03:00
|
|
|
} else {
|
2012-09-21 17:46:43 +04:00
|
|
|
m_nBestOutputCollector = new Moses::OutputCollector(new std::ofstream(nBestFilePath.c_str()));
|
|
|
|
m_nBestOutputCollector->HoldOutputStream();
|
2011-02-24 15:40:21 +03:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2012-09-21 17:46:43 +04:00
|
|
|
if (!suppressSingleBestOutput) {
|
2011-08-18 01:13:21 +04:00
|
|
|
m_singleBestOutputCollector = new Moses::OutputCollector(&std::cout);
|
|
|
|
}
|
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
// search graph output
|
|
|
|
if (staticData.GetOutputSearchGraph()) {
|
|
|
|
string fileName = staticData.GetParam("output-search-graph")[0];
|
|
|
|
std::ofstream *file = new std::ofstream;
|
|
|
|
m_outputSearchGraphStream = file;
|
|
|
|
file->open(fileName.c_str());
|
2011-08-18 01:13:21 +04:00
|
|
|
m_searchGraphOutputCollector = new Moses::OutputCollector(m_outputSearchGraphStream);
|
2011-02-24 15:40:21 +03:00
|
|
|
}
|
2010-05-08 19:51:59 +04:00
|
|
|
|
|
|
|
// detailed translation reporting
|
2011-02-24 15:40:21 +03:00
|
|
|
if (staticData.IsDetailedTranslationReportingEnabled()) {
|
2010-05-08 19:51:59 +04:00
|
|
|
const std::string &path = staticData.GetDetailedTranslationReportingFilePath();
|
|
|
|
m_detailedTranslationReportingStream = new std::ofstream(path.c_str());
|
2011-08-18 01:13:21 +04:00
|
|
|
m_detailOutputCollector = new Moses::OutputCollector(m_detailedTranslationReportingStream);
|
2010-05-08 19:51:59 +04:00
|
|
|
}
|
2012-11-15 19:52:35 +04:00
|
|
|
|
|
|
|
if (!staticData.GetAlignmentOutputFile().empty()) {
|
|
|
|
m_alignmentInfoStream = new std::ofstream(staticData.GetAlignmentOutputFile().c_str());
|
|
|
|
m_alignmentInfoCollector = new Moses::OutputCollector(m_alignmentInfoStream);
|
|
|
|
CHECK(m_alignmentInfoStream->good());
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IOWrapper::~IOWrapper()
|
|
|
|
{
|
2011-02-24 15:40:21 +03:00
|
|
|
if (!m_inputFilePath.empty()) {
|
2010-05-07 22:43:34 +04:00
|
|
|
delete m_inputStream;
|
|
|
|
}
|
2011-02-24 15:40:21 +03:00
|
|
|
delete m_outputSearchGraphStream;
|
2010-05-08 19:51:59 +04:00
|
|
|
delete m_detailedTranslationReportingStream;
|
2012-11-15 19:52:35 +04:00
|
|
|
delete m_alignmentInfoStream;
|
2011-08-18 01:13:21 +04:00
|
|
|
delete m_detailOutputCollector;
|
|
|
|
delete m_nBestOutputCollector;
|
|
|
|
delete m_searchGraphOutputCollector;
|
|
|
|
delete m_singleBestOutputCollector;
|
2012-11-15 19:52:35 +04:00
|
|
|
delete m_alignmentInfoCollector;
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void IOWrapper::ResetTranslationId()
|
|
|
|
{
|
2011-11-13 21:14:40 +04:00
|
|
|
m_translationId = StaticData::Instance().GetStartTranslationId();
|
|
|
|
}
|
2010-09-13 20:02:00 +04:00
|
|
|
|
2011-08-18 01:13:21 +04:00
|
|
|
InputType*IOWrapper::GetInput(InputType* inputType)
|
2010-09-13 20:02:00 +04:00
|
|
|
{
|
2011-08-18 01:13:21 +04:00
|
|
|
if(inputType->Read(*m_inputStream, m_inputFactorOrder)) {
|
|
|
|
if (long x = inputType->GetTranslationId()) {
|
|
|
|
if (x>=m_translationId) m_translationId = x+1;
|
|
|
|
} else inputType->SetTranslationId(m_translationId++);
|
|
|
|
|
|
|
|
return inputType;
|
|
|
|
} else {
|
|
|
|
delete inputType;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 01:13:21 +04:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
/***
|
|
|
|
* print surface factor only for the given phrase
|
|
|
|
*/
|
|
|
|
void OutputSurface(std::ostream &out, const Phrase &phrase, const std::vector<FactorType> &outputFactorOrder, bool reportAllFactors)
|
|
|
|
{
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(outputFactorOrder.size() > 0);
|
2011-02-24 15:40:21 +03:00
|
|
|
if (reportAllFactors == true) {
|
|
|
|
out << phrase;
|
|
|
|
} else {
|
|
|
|
size_t size = phrase.GetSize();
|
|
|
|
for (size_t pos = 0 ; pos < size ; pos++) {
|
|
|
|
const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[0]);
|
|
|
|
out << *factor;
|
2012-05-17 19:25:53 +04:00
|
|
|
CHECK(factor);
|
2011-02-24 15:40:21 +03:00
|
|
|
|
|
|
|
for (size_t i = 1 ; i < outputFactorOrder.size() ; i++) {
|
|
|
|
const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[i]);
|
2013-05-29 21:16:15 +04:00
|
|
|
CHECK(factor);
|
2012-05-17 19:25:53 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
out << "|" << *factor;
|
|
|
|
}
|
|
|
|
out << " ";
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
void OutputSurface(std::ostream &out, const ChartHypothesis *hypo, const std::vector<FactorType> &outputFactorOrder
|
2011-02-24 15:40:21 +03:00
|
|
|
,bool reportSegmentation, bool reportAllFactors)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:40:21 +03:00
|
|
|
if ( hypo != NULL) {
|
|
|
|
//OutputSurface(out, hypo->GetCurrTargetPhrase(), outputFactorOrder, reportAllFactors);
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
const vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
vector<const ChartHypothesis*>::const_iterator iter;
|
2011-02-24 15:40:21 +03:00
|
|
|
for (iter = prevHypos.begin(); iter != prevHypos.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *prevHypo = *iter;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
OutputSurface(out, prevHypo, outputFactorOrder, reportSegmentation, reportAllFactors);
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
void IOWrapper::Backtrack(const ChartHypothesis *hypo)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-03-11 16:08:43 +03:00
|
|
|
const vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
vector<const ChartHypothesis*>::const_iterator iter;
|
2011-02-24 15:40:21 +03:00
|
|
|
for (iter = prevHypos.begin(); iter != prevHypos.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *prevHypo = *iter;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
VERBOSE(3,prevHypo->GetId() << " <= ");
|
|
|
|
Backtrack(prevHypo);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:11:42 +04:00
|
|
|
void IOWrapper::OutputBestHypo(const std::vector<const Factor*>& mbrBestHypo, long /*translationId*/)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:40:21 +03:00
|
|
|
for (size_t i = 0 ; i < mbrBestHypo.size() ; i++) {
|
|
|
|
const Factor *factor = mbrBestHypo[i];
|
2012-05-17 19:25:53 +04:00
|
|
|
CHECK(factor);
|
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
cout << *factor << " ";
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
/*
|
2011-03-11 16:08:43 +03:00
|
|
|
void OutputInput(std::vector<const Phrase*>& map, const ChartHypothesis* hypo)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
if (hypo->GetPrevHypos())
|
|
|
|
{
|
|
|
|
OutputInput(map, hypo->GetPrevHypos());
|
|
|
|
map[hypo->GetCurrSourceWordsRange().GetStartPos()] = hypo->GetSourcePhrase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
void OutputInput(std::ostream& os, const ChartHypothesis* hypo)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
size_t len = StaticData::Instance().GetInput()->GetSize();
|
|
|
|
std::vector<const Phrase*> inp_phrases(len, 0);
|
|
|
|
OutputInput(inp_phrases, hypo);
|
|
|
|
for (size_t i=0; i<len; ++i)
|
|
|
|
if (inp_phrases[i]) os << *inp_phrases[i];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:26:28 +04:00
|
|
|
// Given a hypothesis and sentence, reconstructs the 'application context' --
|
|
|
|
// the source RHS symbols of the SCFG rule that was applied, plus their spans.
|
2012-12-03 18:57:33 +04:00
|
|
|
void IOWrapper::ReconstructApplicationContext(const ChartHypothesis &hypo,
|
2013-05-29 21:16:15 +04:00
|
|
|
const Sentence &sentence,
|
|
|
|
ApplicationContext &context)
|
2011-02-24 15:40:21 +03:00
|
|
|
{
|
2012-01-25 16:26:28 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2012-12-03 18:57:33 +04:00
|
|
|
void IOWrapper::WriteApplicationContext(std::ostream &out,
|
2013-05-29 21:16:15 +04:00
|
|
|
const ApplicationContext &context)
|
2011-02-24 15:40:21 +03:00
|
|
|
{
|
2012-01-25 16:26:28 +04:00
|
|
|
assert(!context.empty());
|
|
|
|
ApplicationContext::const_reverse_iterator p = context.rbegin();
|
|
|
|
while (true) {
|
|
|
|
out << p->second << "=" << p->first << " ";
|
|
|
|
if (++p == context.rend()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 18:57:33 +04:00
|
|
|
void IOWrapper::OutputTranslationOptions(std::ostream &out, ApplicationContext &applicationContext, const ChartHypothesis *hypo, const Sentence &sentence, long translationId)
|
2012-01-25 16:26:28 +04:00
|
|
|
{
|
2011-02-24 15:40:21 +03:00
|
|
|
// recursive
|
|
|
|
if (hypo != NULL) {
|
2012-01-25 16:26:28 +04:00
|
|
|
ReconstructApplicationContext(*hypo, sentence, applicationContext);
|
2011-06-27 19:13:15 +04:00
|
|
|
out << "Trans Opt " << translationId
|
|
|
|
<< " " << hypo->GetCurrSourceRange()
|
2012-01-25 16:26:28 +04:00
|
|
|
<< ": ";
|
|
|
|
WriteApplicationContext(out, applicationContext);
|
|
|
|
out << ": " << hypo->GetCurrTargetPhrase().GetTargetLHS()
|
2011-06-27 19:13:15 +04:00
|
|
|
<< "->" << hypo->GetCurrTargetPhrase()
|
2011-02-24 15:40:21 +03:00
|
|
|
<< " " << hypo->GetTotalScore() << hypo->GetScoreBreakdown()
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
const std::vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
|
|
|
std::vector<const ChartHypothesis*>::const_iterator iter;
|
2011-02-24 15:40:21 +03:00
|
|
|
for (iter = prevHypos.begin(); iter != prevHypos.end(); ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *prevHypo = *iter;
|
2012-12-03 18:57:33 +04:00
|
|
|
OutputTranslationOptions(out, applicationContext, prevHypo, sentence, translationId);
|
2011-02-24 15:40:21 +03:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 01:13:21 +04:00
|
|
|
void IOWrapper::OutputDetailedTranslationReport(
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *hypo,
|
2012-01-25 16:26:28 +04:00
|
|
|
const Sentence &sentence,
|
2011-02-24 15:40:21 +03:00
|
|
|
long translationId)
|
2011-08-18 01:13:21 +04:00
|
|
|
{
|
2011-02-24 15:40:21 +03:00
|
|
|
if (hypo == NULL) {
|
2011-08-18 01:13:21 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::ostringstream out;
|
2012-12-03 18:57:33 +04:00
|
|
|
ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
OutputTranslationOptions(out, applicationContext, hypo, sentence, translationId);
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(m_detailOutputCollector);
|
2011-08-18 01:13:21 +04:00
|
|
|
m_detailOutputCollector->Write(translationId, out.str());
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-08-18 01:13:21 +04:00
|
|
|
|
2012-10-11 20:11:42 +04:00
|
|
|
void IOWrapper::OutputBestHypo(const ChartHypothesis *hypo, long translationId)
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2012-09-21 17:46:43 +04:00
|
|
|
if (!m_singleBestOutputCollector)
|
|
|
|
return;
|
2011-08-18 01:13:21 +04:00
|
|
|
std::ostringstream out;
|
|
|
|
IOWrapper::FixPrecision(out);
|
2011-02-24 15:40:21 +03:00
|
|
|
if (hypo != NULL) {
|
|
|
|
VERBOSE(1,"BEST TRANSLATION: " << *hypo << endl);
|
|
|
|
VERBOSE(3,"Best path: ");
|
|
|
|
Backtrack(hypo);
|
|
|
|
VERBOSE(3,"0" << std::endl);
|
|
|
|
|
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
2011-09-24 18:48:52 +04:00
|
|
|
out << hypo->GetTotalScore() << " ";
|
2011-02-24 15:40:21 +03:00
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-09-21 17:46:43 +04:00
|
|
|
if (StaticData::Instance().IsPathRecoveryEnabled()) {
|
|
|
|
out << "||| ";
|
2011-02-24 15:40:21 +03:00
|
|
|
}
|
2012-09-21 17:46:43 +04:00
|
|
|
Phrase outPhrase(ARRAY_SIZE_INCR);
|
|
|
|
hypo->CreateOutputPhrase(outPhrase);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-09-21 17:46:43 +04:00
|
|
|
// delete 1st & last
|
|
|
|
CHECK(outPhrase.GetSize() >= 2);
|
|
|
|
outPhrase.RemoveWord(0);
|
|
|
|
outPhrase.RemoveWord(outPhrase.GetSize() - 1);
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-09-21 17:46:43 +04:00
|
|
|
const std::vector<FactorType> outputFactorOrder = StaticData::Instance().GetOutputFactorOrder();
|
|
|
|
string output = outPhrase.GetStringRep(outputFactorOrder);
|
|
|
|
out << output << endl;
|
2011-02-24 15:40:21 +03:00
|
|
|
} else {
|
|
|
|
VERBOSE(1, "NO BEST TRANSLATION" << endl);
|
2011-08-18 01:13:21 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
|
|
|
out << "0 ";
|
|
|
|
}
|
|
|
|
|
|
|
|
out << endl;
|
|
|
|
}
|
2012-09-21 17:46:43 +04:00
|
|
|
m_singleBestOutputCollector->Write(translationId, out.str());
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void IOWrapper::OutputBestHypo(search::Applied applied, long translationId)
|
|
|
|
{
|
2012-11-16 18:46:10 +04:00
|
|
|
if (!m_singleBestOutputCollector) return;
|
|
|
|
std::ostringstream out;
|
|
|
|
IOWrapper::FixPrecision(out);
|
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
|
|
|
out << applied.GetScore() << ' ';
|
|
|
|
}
|
|
|
|
Phrase outPhrase;
|
|
|
|
Incremental::ToPhrase(applied, outPhrase);
|
|
|
|
// delete 1st & last
|
|
|
|
CHECK(outPhrase.GetSize() >= 2);
|
|
|
|
outPhrase.RemoveWord(0);
|
|
|
|
outPhrase.RemoveWord(outPhrase.GetSize() - 1);
|
|
|
|
out << outPhrase.GetStringRep(StaticData::Instance().GetOutputFactorOrder());
|
|
|
|
out << '\n';
|
|
|
|
m_singleBestOutputCollector->Write(translationId, out.str());
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void IOWrapper::OutputBestNone(long translationId)
|
|
|
|
{
|
2012-11-16 18:46:10 +04:00
|
|
|
if (!m_singleBestOutputCollector) return;
|
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
|
|
|
m_singleBestOutputCollector->Write(translationId, "0 \n");
|
|
|
|
} else {
|
|
|
|
m_singleBestOutputCollector->Write(translationId, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 17:13:26 +04:00
|
|
|
void IOWrapper::OutputAllFeatureScores(const ScoreComponentCollection &features, std::ostream &out)
|
2012-12-07 17:28:11 +04:00
|
|
|
{
|
|
|
|
std::string lastName = "";
|
2012-12-31 04:57:21 +04:00
|
|
|
const vector<const StatefulFeatureFunction*>& sff = StatefulFeatureFunction::GetStatefulFeatureFunctions();
|
2012-12-16 22:29:53 +04:00
|
|
|
for( size_t i=0; i<sff.size(); i++ ) {
|
|
|
|
const StatefulFeatureFunction *ff = sff[i];
|
|
|
|
if (ff->GetScoreProducerDescription() != "BleuScoreFeature"
|
|
|
|
&& ff->IsTuneable()) {
|
|
|
|
OutputFeatureScores( out, features, ff, lastName );
|
|
|
|
}
|
|
|
|
}
|
2012-12-31 04:57:21 +04:00
|
|
|
const vector<const StatelessFeatureFunction*>& slf = StatelessFeatureFunction::GetStatelessFeatureFunctions();
|
2012-12-16 22:29:53 +04:00
|
|
|
for( size_t i=0; i<slf.size(); i++ ) {
|
|
|
|
const StatelessFeatureFunction *ff = slf[i];
|
|
|
|
if (ff->IsTuneable()) {
|
|
|
|
OutputFeatureScores( out, features, ff, lastName );
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 17:28:11 +04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void IOWrapper::OutputFeatureScores( std::ostream& out, const ScoreComponentCollection &features, const FeatureFunction *ff, std::string &lastName )
|
|
|
|
{
|
2012-11-16 18:46:10 +04:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
bool labeledOutput = staticData.IsLabeledNBestList();
|
|
|
|
|
2012-12-07 17:28:11 +04:00
|
|
|
// regular features (not sparse)
|
2013-05-08 18:34:56 +04:00
|
|
|
if (ff->GetNumScoreComponents() != 0) {
|
2012-12-07 17:28:11 +04:00
|
|
|
if( labeledOutput && lastName != ff->GetScoreProducerDescription() ) {
|
|
|
|
lastName = ff->GetScoreProducerDescription();
|
2013-02-19 13:50:52 +04:00
|
|
|
out << " " << lastName << "=";
|
2012-11-16 18:46:10 +04:00
|
|
|
}
|
2012-12-07 17:28:11 +04:00
|
|
|
vector<float> scores = features.GetScoresForProducer( ff );
|
|
|
|
for (size_t j = 0; j<scores.size(); ++j) {
|
|
|
|
out << " " << scores[j];
|
2012-11-16 18:46:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 17:28:11 +04:00
|
|
|
// sparse features
|
2013-06-09 21:48:09 +04:00
|
|
|
const FVector scores = features.GetVectorForProducer( ff );
|
|
|
|
for(FVector::FNVmap::const_iterator i = scores.cbegin(); i != scores.cend(); i++) {
|
|
|
|
out << " " << i->first << "= " << i->second;
|
2012-11-16 18:46:10 +04:00
|
|
|
}
|
2012-12-07 17:28:11 +04:00
|
|
|
}
|
2012-11-16 18:46:10 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void IOWrapper::OutputNBestList(const ChartTrellisPathList &nBestList, long translationId)
|
|
|
|
{
|
2011-08-18 01:13:21 +04:00
|
|
|
std::ostringstream out;
|
|
|
|
|
|
|
|
// Check if we're writing to std::cout.
|
2012-09-21 17:46:43 +04:00
|
|
|
if (m_nBestOutputCollector->OutputIsCout()) {
|
2011-08-18 01:13:21 +04:00
|
|
|
// Set precision only if we're writing the n-best list to cout. This is to
|
|
|
|
// preserve existing behaviour, but should probably be done either way.
|
|
|
|
IOWrapper::FixPrecision(out);
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
// Used to check StaticData's GetOutputHypoScore(), but it makes no sense with nbest output.
|
2011-08-18 01:13:21 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
//bool includeAlignment = StaticData::Instance().NBestIncludesAlignment();
|
2012-11-15 20:42:19 +04:00
|
|
|
bool includeWordAlignment = StaticData::Instance().PrintAlignmentInfoInNbest();
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
ChartTrellisPathList::const_iterator iter;
|
2011-02-24 15:40:21 +03:00
|
|
|
for (iter = nBestList.begin() ; iter != nBestList.end() ; ++iter) {
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartTrellisPath &path = **iter;
|
2011-02-24 15:40:21 +03:00
|
|
|
//cerr << path << endl << endl;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
Moses::Phrase outputPhrase = path.GetOutputPhrase();
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
// delete 1st & last
|
2011-11-18 16:07:41 +04:00
|
|
|
CHECK(outputPhrase.GetSize() >= 2);
|
2011-02-24 15:40:21 +03:00
|
|
|
outputPhrase.RemoveWord(0);
|
|
|
|
outputPhrase.RemoveWord(outputPhrase.GetSize() - 1);
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
// print the surface factor of the translation
|
|
|
|
out << translationId << " ||| ";
|
|
|
|
OutputSurface(out, outputPhrase, m_outputFactorOrder, false);
|
2012-01-20 19:35:55 +04:00
|
|
|
out << " ||| ";
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
// print the scores in a hardwired order
|
2010-04-08 21:16:10 +04:00
|
|
|
// before each model type, the corresponding command-line-like name must be emitted
|
|
|
|
// MERT script relies on this
|
|
|
|
|
2013-05-11 17:13:26 +04:00
|
|
|
OutputAllFeatureScores(path.GetScoreBreakdown(), out);
|
2012-01-20 19:35:55 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
// total
|
2012-01-20 19:35:55 +04:00
|
|
|
out << " ||| " << path.GetTotalScore();
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
/*
|
2010-04-08 21:16:10 +04:00
|
|
|
if (includeAlignment) {
|
2011-02-24 15:40:21 +03:00
|
|
|
*m_nBestStream << " |||";
|
|
|
|
for (int currEdge = (int)edges.size() - 2 ; currEdge >= 0 ; currEdge--)
|
|
|
|
{
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis &edge = *edges[currEdge];
|
2011-02-24 15:40:21 +03:00
|
|
|
WordsRange sourceRange = edge.GetCurrSourceWordsRange();
|
|
|
|
WordsRange targetRange = edge.GetCurrTargetWordsRange();
|
|
|
|
*m_nBestStream << " " << sourceRange.GetStartPos();
|
|
|
|
if (sourceRange.GetStartPos() < sourceRange.GetEndPos()) {
|
|
|
|
*m_nBestStream << "-" << sourceRange.GetEndPos();
|
|
|
|
}
|
|
|
|
*m_nBestStream << "=" << targetRange.GetStartPos();
|
|
|
|
if (targetRange.GetStartPos() < targetRange.GetEndPos()) {
|
|
|
|
*m_nBestStream << "-" << targetRange.GetEndPos();
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 15:40:21 +03:00
|
|
|
*/
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2012-11-15 20:42:19 +04:00
|
|
|
if (includeWordAlignment) {
|
|
|
|
out << " ||| ";
|
2012-11-16 20:44:29 +04:00
|
|
|
|
|
|
|
Alignments retAlign;
|
|
|
|
|
2012-11-16 22:25:17 +04:00
|
|
|
const ChartTrellisNode &node = path.GetFinalNode();
|
|
|
|
OutputAlignmentNBest(retAlign, node, 0);
|
|
|
|
|
|
|
|
Alignments::const_iterator iter;
|
|
|
|
for (iter = retAlign.begin(); iter != retAlign.end(); ++iter) {
|
|
|
|
const pair<size_t, size_t> &alignPoint = *iter;
|
|
|
|
out << alignPoint.first << "-" << alignPoint.second << " ";
|
|
|
|
}
|
2012-11-15 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 01:13:21 +04:00
|
|
|
out << endl;
|
2011-02-24 15:40:21 +03:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-02-24 15:40:21 +03:00
|
|
|
out <<std::flush;
|
2011-08-18 01:13:21 +04:00
|
|
|
|
2012-11-16 18:46:10 +04:00
|
|
|
assert(m_nBestOutputCollector);
|
2011-08-18 01:13:21 +04:00
|
|
|
m_nBestOutputCollector->Write(translationId, out.str());
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void IOWrapper::OutputNBestList(const std::vector<search::Applied> &nbest, long translationId)
|
|
|
|
{
|
2012-11-16 18:46:10 +04:00
|
|
|
std::ostringstream out;
|
|
|
|
// wtf? copied from the original OutputNBestList
|
|
|
|
if (m_nBestOutputCollector->OutputIsCout()) {
|
|
|
|
IOWrapper::FixPrecision(out);
|
2012-01-20 19:35:55 +04:00
|
|
|
}
|
2012-11-16 18:46:10 +04:00
|
|
|
Phrase outputPhrase;
|
|
|
|
ScoreComponentCollection features;
|
|
|
|
for (std::vector<search::Applied>::const_iterator i = nbest.begin(); i != nbest.end(); ++i) {
|
2013-05-11 17:13:26 +04:00
|
|
|
Incremental::PhraseAndFeatures(*i, outputPhrase, features);
|
2012-11-16 18:46:10 +04:00
|
|
|
// <s> and </s>
|
|
|
|
CHECK(outputPhrase.GetSize() >= 2);
|
|
|
|
outputPhrase.RemoveWord(0);
|
|
|
|
outputPhrase.RemoveWord(outputPhrase.GetSize() - 1);
|
|
|
|
out << translationId << " ||| ";
|
|
|
|
OutputSurface(out, outputPhrase, m_outputFactorOrder, false);
|
|
|
|
out << " ||| ";
|
2013-05-11 17:13:26 +04:00
|
|
|
OutputAllFeatureScores(features, out);
|
2012-11-16 18:46:10 +04:00
|
|
|
out << " ||| " << i->GetScore() << '\n';
|
2012-01-20 19:35:55 +04:00
|
|
|
}
|
2012-11-16 18:46:10 +04:00
|
|
|
out << std::flush;
|
|
|
|
assert(m_nBestOutputCollector);
|
|
|
|
m_nBestOutputCollector->Write(translationId, out.str());
|
2012-01-20 19:35:55 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 01:13:21 +04:00
|
|
|
void IOWrapper::FixPrecision(std::ostream &stream, size_t size)
|
|
|
|
{
|
|
|
|
stream.setf(std::ios::fixed);
|
|
|
|
stream.precision(size);
|
|
|
|
}
|
2012-07-02 20:05:11 +04:00
|
|
|
|
2012-11-16 22:25:17 +04:00
|
|
|
template <class T>
|
|
|
|
void ShiftOffsets(vector<T> &offsets, T shift)
|
2012-11-15 20:42:19 +04:00
|
|
|
{
|
2013-02-26 22:27:55 +04:00
|
|
|
T currPos = shift;
|
2012-11-16 22:25:17 +04:00
|
|
|
for (size_t i = 0; i < offsets.size(); ++i) {
|
2013-02-26 22:27:55 +04:00
|
|
|
if (offsets[i] == 0) {
|
2013-05-29 21:16:15 +04:00
|
|
|
offsets[i] = currPos;
|
|
|
|
++currPos;
|
|
|
|
} else {
|
|
|
|
currPos += offsets[i];
|
|
|
|
}
|
2012-11-16 22:25:17 +04:00
|
|
|
}
|
2012-11-15 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
size_t CalcSourceSize(const Moses::ChartHypothesis *hypo)
|
|
|
|
{
|
|
|
|
size_t ret = hypo->GetCurrSourceRange().GetNumWordsCovered();
|
|
|
|
const std::vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
|
|
|
for (size_t i = 0; i < prevHypos.size(); ++i) {
|
|
|
|
size_t childSize = prevHypos[i]->GetCurrSourceRange().GetNumWordsCovered();
|
|
|
|
ret -= (childSize - 1);
|
2012-11-16 22:25:17 +04:00
|
|
|
}
|
2013-03-02 21:58:33 +04:00
|
|
|
return ret;
|
2012-11-15 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2012-11-16 22:25:17 +04:00
|
|
|
size_t IOWrapper::OutputAlignmentNBest(Alignments &retAlign, const Moses::ChartTrellisNode &node, size_t startTarget)
|
2012-11-15 20:42:19 +04:00
|
|
|
{
|
2012-11-16 22:25:17 +04:00
|
|
|
const ChartHypothesis *hypo = &node.GetHypothesis();
|
|
|
|
|
|
|
|
size_t totalTargetSize = 0;
|
|
|
|
size_t startSource = hypo->GetCurrSourceRange().GetStartPos();
|
|
|
|
|
|
|
|
const TargetPhrase &tp = hypo->GetCurrTargetPhrase();
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
size_t thisSourceSize = CalcSourceSize(hypo);
|
|
|
|
|
|
|
|
// position of each terminal word in translation rule, irrespective of alignment
|
|
|
|
// if non-term, number is undefined
|
|
|
|
vector<size_t> sourceOffsets(thisSourceSize, 0);
|
2012-11-16 22:25:17 +04:00
|
|
|
vector<size_t> targetOffsets(tp.GetSize(), 0);
|
2012-11-15 20:42:19 +04:00
|
|
|
|
2012-11-16 22:25:17 +04:00
|
|
|
const ChartTrellisNode::NodeChildren &prevNodes = node.GetChildren();
|
|
|
|
|
|
|
|
const AlignmentInfo &aiNonTerm = hypo->GetCurrTargetPhrase().GetAlignNonTerm();
|
|
|
|
vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap();
|
|
|
|
const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap();
|
|
|
|
|
|
|
|
CHECK(sourceInd2pos.size() == prevNodes.size());
|
|
|
|
|
|
|
|
size_t targetInd = 0;
|
|
|
|
for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
|
|
|
|
if (tp.GetWord(targetPos).IsNonTerminal()) {
|
|
|
|
CHECK(targetPos < targetPos2SourceInd.size());
|
|
|
|
size_t sourceInd = targetPos2SourceInd[targetPos];
|
|
|
|
size_t sourcePos = sourceInd2pos[sourceInd];
|
|
|
|
|
|
|
|
const ChartTrellisNode &prevNode = *prevNodes[sourceInd];
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
// calc source size
|
2012-11-16 22:25:17 +04:00
|
|
|
size_t sourceSize = prevNode.GetHypothesis().GetCurrSourceRange().GetNumWordsCovered();
|
|
|
|
sourceOffsets[sourcePos] = sourceSize;
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
// calc target size.
|
|
|
|
// Recursively look thru child hypos
|
2012-11-16 22:25:17 +04:00
|
|
|
size_t currStartTarget = startTarget + totalTargetSize;
|
|
|
|
size_t targetSize = OutputAlignmentNBest(retAlign, prevNode, currStartTarget);
|
|
|
|
targetOffsets[targetPos] = targetSize;
|
|
|
|
|
|
|
|
totalTargetSize += targetSize;
|
|
|
|
++targetInd;
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2012-11-16 22:25:17 +04:00
|
|
|
++totalTargetSize;
|
|
|
|
}
|
2012-11-15 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
// convert position within translation rule to absolute position within
|
|
|
|
// source sentence / output sentence
|
2012-11-16 22:25:17 +04:00
|
|
|
ShiftOffsets(sourceOffsets, startSource);
|
|
|
|
ShiftOffsets(targetOffsets, startTarget);
|
|
|
|
|
|
|
|
// get alignments from this hypo
|
|
|
|
const AlignmentInfo &aiTerm = hypo->GetCurrTargetPhrase().GetAlignTerm();
|
2012-11-16 21:31:24 +04:00
|
|
|
|
2012-11-16 22:25:17 +04:00
|
|
|
// add to output arg, offsetting by source & target
|
2013-03-02 21:58:33 +04:00
|
|
|
AlignmentInfo::const_iterator iter;
|
|
|
|
for (iter = aiTerm.begin(); iter != aiTerm.end(); ++iter) {
|
|
|
|
const std::pair<size_t,size_t> &align = *iter;
|
|
|
|
size_t relSource = align.first;
|
|
|
|
size_t relTarget = align.second;
|
|
|
|
size_t absSource = sourceOffsets[relSource];
|
|
|
|
size_t absTarget = targetOffsets[relTarget];
|
2012-11-16 22:25:17 +04:00
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
pair<size_t, size_t> alignPoint(absSource, absTarget);
|
|
|
|
pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
|
|
|
|
CHECK(ret.second);
|
2012-11-16 22:25:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return totalTargetSize;
|
2012-11-15 20:42:19 +04:00
|
|
|
}
|
|
|
|
|
2012-11-15 19:52:35 +04:00
|
|
|
void IOWrapper::OutputAlignment(size_t translationId , const Moses::ChartHypothesis *hypo)
|
|
|
|
{
|
|
|
|
ostringstream out;
|
|
|
|
|
2013-03-04 14:19:50 +04:00
|
|
|
if (hypo) {
|
2013-05-29 21:16:15 +04:00
|
|
|
Alignments retAlign;
|
|
|
|
OutputAlignment(retAlign, hypo, 0);
|
|
|
|
|
|
|
|
// output alignments
|
|
|
|
Alignments::const_iterator iter;
|
|
|
|
for (iter = retAlign.begin(); iter != retAlign.end(); ++iter) {
|
|
|
|
const pair<size_t, size_t> &alignPoint = *iter;
|
|
|
|
out << alignPoint.first << "-" << alignPoint.second << " ";
|
|
|
|
}
|
2012-11-16 20:44:29 +04:00
|
|
|
}
|
2012-11-15 19:52:35 +04:00
|
|
|
out << endl;
|
|
|
|
|
|
|
|
m_alignmentInfoCollector->Write(translationId, out.str());
|
|
|
|
}
|
|
|
|
|
2012-11-16 20:44:29 +04:00
|
|
|
size_t IOWrapper::OutputAlignment(Alignments &retAlign, const Moses::ChartHypothesis *hypo, size_t startTarget)
|
|
|
|
{
|
|
|
|
size_t totalTargetSize = 0;
|
|
|
|
size_t startSource = hypo->GetCurrSourceRange().GetStartPos();
|
|
|
|
|
|
|
|
const TargetPhrase &tp = hypo->GetCurrTargetPhrase();
|
2012-11-15 19:52:35 +04:00
|
|
|
|
2013-02-26 22:27:55 +04:00
|
|
|
size_t thisSourceSize = CalcSourceSize(hypo);
|
2013-03-02 21:58:33 +04:00
|
|
|
|
|
|
|
// position of each terminal word in translation rule, irrespective of alignment
|
|
|
|
// if non-term, number is undefined
|
2013-02-26 22:27:55 +04:00
|
|
|
vector<size_t> sourceOffsets(thisSourceSize, 0);
|
2012-11-16 20:44:29 +04:00
|
|
|
vector<size_t> targetOffsets(tp.GetSize(), 0);
|
|
|
|
|
|
|
|
const vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
|
|
|
|
|
|
|
const AlignmentInfo &aiNonTerm = hypo->GetCurrTargetPhrase().GetAlignNonTerm();
|
|
|
|
vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap();
|
|
|
|
const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap();
|
|
|
|
|
|
|
|
CHECK(sourceInd2pos.size() == prevHypos.size());
|
|
|
|
|
|
|
|
size_t targetInd = 0;
|
|
|
|
for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
|
|
|
|
if (tp.GetWord(targetPos).IsNonTerminal()) {
|
|
|
|
CHECK(targetPos < targetPos2SourceInd.size());
|
|
|
|
size_t sourceInd = targetPos2SourceInd[targetPos];
|
|
|
|
size_t sourcePos = sourceInd2pos[sourceInd];
|
|
|
|
|
|
|
|
const ChartHypothesis *prevHypo = prevHypos[sourceInd];
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
// calc source size
|
2012-11-16 20:44:29 +04:00
|
|
|
size_t sourceSize = prevHypo->GetCurrSourceRange().GetNumWordsCovered();
|
|
|
|
sourceOffsets[sourcePos] = sourceSize;
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
// calc target size.
|
|
|
|
// Recursively look thru child hypos
|
2012-11-16 20:44:29 +04:00
|
|
|
size_t currStartTarget = startTarget + totalTargetSize;
|
|
|
|
size_t targetSize = OutputAlignment(retAlign, prevHypo, currStartTarget);
|
|
|
|
targetOffsets[targetPos] = targetSize;
|
|
|
|
|
|
|
|
totalTargetSize += targetSize;
|
|
|
|
++targetInd;
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2012-11-16 20:44:29 +04:00
|
|
|
++totalTargetSize;
|
2012-11-15 19:52:35 +04:00
|
|
|
}
|
2012-11-16 20:44:29 +04:00
|
|
|
}
|
|
|
|
|
2013-03-02 21:58:33 +04:00
|
|
|
// convert position within translation rule to absolute position within
|
|
|
|
// source sentence / output sentence
|
2012-11-16 20:44:29 +04:00
|
|
|
ShiftOffsets(sourceOffsets, startSource);
|
|
|
|
ShiftOffsets(targetOffsets, startTarget);
|
|
|
|
|
|
|
|
// get alignments from this hypo
|
|
|
|
const AlignmentInfo &aiTerm = hypo->GetCurrTargetPhrase().GetAlignTerm();
|
|
|
|
|
|
|
|
// add to output arg, offsetting by source & target
|
2013-02-26 22:27:55 +04:00
|
|
|
AlignmentInfo::const_iterator iter;
|
|
|
|
for (iter = aiTerm.begin(); iter != aiTerm.end(); ++iter) {
|
|
|
|
const std::pair<size_t,size_t> &align = *iter;
|
|
|
|
size_t relSource = align.first;
|
|
|
|
size_t relTarget = align.second;
|
|
|
|
size_t absSource = sourceOffsets[relSource];
|
|
|
|
size_t absTarget = targetOffsets[relTarget];
|
|
|
|
|
|
|
|
pair<size_t, size_t> alignPoint(absSource, absTarget);
|
|
|
|
pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
|
|
|
|
CHECK(ret.second);
|
2012-11-15 19:52:35 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-16 20:44:29 +04:00
|
|
|
return totalTargetSize;
|
2012-11-15 19:52:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-16 20:44:29 +04:00
|
|
|
void IOWrapper::OutputAlignment(vector< set<size_t> > &retAlignmentsS2T, const AlignmentInfo &ai)
|
2012-11-15 19:52:35 +04:00
|
|
|
{
|
|
|
|
typedef std::vector< const std::pair<size_t,size_t>* > AlignVec;
|
|
|
|
AlignVec alignments = ai.GetSortedAlignments();
|
|
|
|
|
|
|
|
AlignVec::const_iterator it;
|
|
|
|
for (it = alignments.begin(); it != alignments.end(); ++it) {
|
2012-11-16 20:44:29 +04:00
|
|
|
const std::pair<size_t,size_t> &alignPoint = **it;
|
2012-11-15 19:52:35 +04:00
|
|
|
|
2012-11-16 21:04:50 +04:00
|
|
|
CHECK(alignPoint.first < retAlignmentsS2T.size());
|
2012-11-16 20:44:29 +04:00
|
|
|
pair<set<size_t>::iterator, bool> ret = retAlignmentsS2T[alignPoint.first].insert(alignPoint.second);
|
2012-11-16 21:04:50 +04:00
|
|
|
CHECK(ret.second);
|
2012-11-16 20:44:29 +04:00
|
|
|
}
|
2012-11-15 19:52:35 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 20:05:11 +04:00
|
|
|
}
|
|
|
|
|