2010-07-18 03:23:09 +04:00
|
|
|
// $Id$
|
2010-04-12 14:15:49 +04:00
|
|
|
// vim:tabstop=2
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2010 Hieu Hoang
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2010-04-12 14:15:49 +04:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2015-03-28 16:09:03 +03:00
|
|
|
#include <cstdio>
|
2010-04-08 21:16:10 +04:00
|
|
|
#include "ChartManager.h"
|
|
|
|
#include "ChartCell.h"
|
|
|
|
#include "ChartHypothesis.h"
|
2014-04-21 13:19:00 +04:00
|
|
|
#include "ChartKBestExtractor.h"
|
2012-09-27 01:49:33 +04:00
|
|
|
#include "ChartTranslationOptions.h"
|
2014-08-08 00:20:10 +04:00
|
|
|
#include "HypergraphOutput.h"
|
2011-03-11 16:08:43 +03:00
|
|
|
#include "StaticData.h"
|
|
|
|
#include "DecodeStep.h"
|
2012-03-28 07:29:24 +04:00
|
|
|
#include "TreeInput.h"
|
2014-12-04 21:35:19 +03:00
|
|
|
#include "moses/FF/StatefulFeatureFunction.h"
|
2013-05-25 02:57:06 +04:00
|
|
|
#include "moses/FF/WordPenaltyProducer.h"
|
2014-12-02 20:40:53 +03:00
|
|
|
#include "moses/OutputCollector.h"
|
|
|
|
#include "moses/ChartKBestExtractor.h"
|
2014-12-06 00:33:59 +03:00
|
|
|
#include "moses/HypergraphOutput.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2015-03-26 21:25:54 +03:00
|
|
|
|
2013-12-11 23:04:42 +04:00
|
|
|
extern bool g_mosesDebug;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
/* constructor. Initialize everything prior to decoding a particular sentence.
|
|
|
|
* \param source the sentence to be decoded
|
|
|
|
* \param system which particular set of models to use.
|
|
|
|
*/
|
2015-03-26 21:25:54 +03:00
|
|
|
ChartManager::ChartManager(ttasksptr const& ttask)
|
|
|
|
: BaseManager(ttask)
|
|
|
|
, m_hypoStackColl(m_source, *this)
|
|
|
|
, m_start(clock())
|
|
|
|
, m_hypothesisId(0)
|
|
|
|
, m_parser(ttask, m_hypoStackColl)
|
|
|
|
, m_translationOptionList(StaticData::Instance().GetRuleLimit(), m_source)
|
|
|
|
{ }
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
ChartManager::~ChartManager()
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
clock_t end = clock();
|
|
|
|
float et = (end - m_start);
|
|
|
|
et /= (float)CLOCKS_PER_SEC;
|
|
|
|
VERBOSE(1, "Translation took " << et << " seconds" << endl);
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
//! decode the sentence. This contains the main laps. Basically, the CKY++ algorithm
|
2014-12-05 20:59:53 +03:00
|
|
|
void ChartManager::Decode()
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2015-04-30 08:05:11 +03:00
|
|
|
|
2011-02-24 15:36:50 +03:00
|
|
|
VERBOSE(1,"Translating: " << m_source << endl);
|
|
|
|
|
|
|
|
ResetSentenceStats(m_source);
|
|
|
|
|
|
|
|
VERBOSE(2,"Decoding: " << endl);
|
2011-03-11 16:08:43 +03:00
|
|
|
//ChartHypothesis::ResetHypoCount();
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2012-03-28 07:29:24 +04:00
|
|
|
AddXmlChartOptions();
|
|
|
|
|
2011-02-24 15:36:50 +03:00
|
|
|
// MAIN LOOP
|
|
|
|
size_t size = m_source.GetSize();
|
2014-03-21 14:53:15 +04:00
|
|
|
for (int startPos = size-1; startPos >= 0; --startPos) {
|
|
|
|
for (size_t width = 1; width <= size-startPos; ++width) {
|
2011-02-24 15:36:50 +03:00
|
|
|
size_t endPos = startPos + width - 1;
|
|
|
|
WordsRange range(startPos, endPos);
|
|
|
|
|
|
|
|
// create trans opt
|
2012-10-11 16:26:47 +04:00
|
|
|
m_translationOptionList.Clear();
|
2012-10-11 15:29:02 +04:00
|
|
|
m_parser.Create(range, m_translationOptionList);
|
2012-10-11 16:26:47 +04:00
|
|
|
m_translationOptionList.ApplyThreshold();
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2013-09-13 11:27:48 +04:00
|
|
|
const InputPath &inputPath = m_parser.GetInputPath(range);
|
2014-08-08 21:11:30 +04:00
|
|
|
m_translationOptionList.EvaluateWithSourceContext(m_source, inputPath);
|
2013-09-13 11:27:48 +04:00
|
|
|
|
2011-02-24 15:36:50 +03:00
|
|
|
// decode
|
|
|
|
ChartCell &cell = m_hypoStackColl.Get(range);
|
2014-12-26 10:41:52 +03:00
|
|
|
cell.Decode(m_translationOptionList, m_hypoStackColl);
|
2013-12-12 00:03:30 +04:00
|
|
|
|
2012-09-25 20:08:38 +04:00
|
|
|
m_translationOptionList.Clear();
|
2011-02-24 15:36:50 +03:00
|
|
|
cell.PruneToSize();
|
|
|
|
cell.CleanupArcList();
|
|
|
|
cell.SortHypotheses();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IFVERBOSE(1) {
|
|
|
|
|
|
|
|
for (size_t startPos = 0; startPos < size; ++startPos) {
|
|
|
|
cerr.width(3);
|
|
|
|
cerr << startPos << " ";
|
|
|
|
}
|
|
|
|
cerr << endl;
|
|
|
|
for (size_t width = 1; width <= size; width++) {
|
|
|
|
for( size_t space = 0; space < width-1; space++ ) {
|
|
|
|
cerr << " ";
|
|
|
|
}
|
|
|
|
for (size_t startPos = 0; startPos <= size-width; ++startPos) {
|
|
|
|
WordsRange range(startPos, startPos+width-1);
|
|
|
|
cerr.width(3);
|
|
|
|
cerr << m_hypoStackColl.Get(range).GetSize() << " ";
|
|
|
|
}
|
|
|
|
cerr << endl;
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
/** add specific translation options and hypotheses according to the XML override translation scheme.
|
|
|
|
* Doesn't seem to do anything about walls and zones.
|
|
|
|
* @todo check walls & zones. Check that the implementation doesn't leak, xml options sometimes does if you're not careful
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
void ChartManager::AddXmlChartOptions()
|
|
|
|
{
|
2014-06-05 04:39:44 +04:00
|
|
|
// const StaticData &staticData = StaticData::Instance();
|
2013-08-31 04:37:15 +04:00
|
|
|
|
2012-09-25 20:34:43 +04:00
|
|
|
const std::vector <ChartTranslationOptions*> xmlChartOptionsList = m_source.GetXmlChartTranslationOptions();
|
2013-05-29 21:16:15 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
cerr << "AddXmlChartOptions " << xmlChartOptionsList.size() << endl;
|
|
|
|
}
|
2012-03-28 07:29:24 +04:00
|
|
|
if (xmlChartOptionsList.size() == 0) return;
|
|
|
|
|
2012-09-25 20:34:43 +04:00
|
|
|
for(std::vector<ChartTranslationOptions*>::const_iterator i = xmlChartOptionsList.begin();
|
2012-03-28 07:29:24 +04:00
|
|
|
i != xmlChartOptionsList.end(); ++i) {
|
2012-09-25 20:34:43 +04:00
|
|
|
ChartTranslationOptions* opt = *i;
|
2012-03-28 07:29:24 +04:00
|
|
|
|
|
|
|
const WordsRange &range = opt->GetSourceWordsRange();
|
2013-07-24 18:55:17 +04:00
|
|
|
|
2012-03-28 07:29:24 +04:00
|
|
|
RuleCubeItem* item = new RuleCubeItem( *opt, m_hypoStackColl );
|
|
|
|
ChartHypothesis* hypo = new ChartHypothesis(*opt, *item, *this);
|
2014-08-08 19:41:16 +04:00
|
|
|
hypo->EvaluateWhenApplied();
|
2013-07-24 18:55:17 +04:00
|
|
|
|
|
|
|
|
2012-03-28 07:29:24 +04:00
|
|
|
ChartCell &cell = m_hypoStackColl.Get(range);
|
|
|
|
cell.AddHypothesis(hypo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
//! get best complete translation from the top chart cell.
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *ChartManager::GetBestHypothesis() const
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-02-24 15:36:50 +03:00
|
|
|
size_t size = m_source.GetSize();
|
|
|
|
|
|
|
|
if (size == 0) // empty source
|
|
|
|
return NULL;
|
|
|
|
else {
|
|
|
|
WordsRange range(0, size-1);
|
|
|
|
const ChartCell &lastCell = m_hypoStackColl.Get(range);
|
|
|
|
return lastCell.GetBestHypothesis();
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 13:19:00 +04:00
|
|
|
/** Calculate the n-best paths through the output hypergraph.
|
|
|
|
* Return the list of paths with the variable ret
|
|
|
|
* \param n how may paths to return
|
|
|
|
* \param ret return argument
|
|
|
|
* \param onlyDistinct whether to check for distinct output sentence or not (default - don't check, just return top n-paths)
|
|
|
|
*/
|
|
|
|
void ChartManager::CalcNBest(
|
2014-05-19 17:34:27 +04:00
|
|
|
std::size_t n,
|
|
|
|
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > &nBestList,
|
|
|
|
bool onlyDistinct) const
|
2014-04-21 13:19:00 +04:00
|
|
|
{
|
|
|
|
nBestList.clear();
|
|
|
|
if (n == 0 || m_source.GetSize() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of top-level hypotheses, sorted by score.
|
|
|
|
WordsRange range(0, m_source.GetSize()-1);
|
|
|
|
const ChartCell &lastCell = m_hypoStackColl.Get(range);
|
|
|
|
boost::scoped_ptr<const std::vector<const ChartHypothesis*> > topLevelHypos(
|
2014-05-19 17:34:27 +04:00
|
|
|
lastCell.GetAllSortedHypotheses());
|
2014-04-21 13:19:00 +04:00
|
|
|
if (!topLevelHypos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartKBestExtractor extractor;
|
|
|
|
|
|
|
|
if (!onlyDistinct) {
|
|
|
|
// Return the n-best list as is, including duplicate translations.
|
|
|
|
extractor.Extract(*topLevelHypos, n, nBestList);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine how many derivations to extract. If the n-best list is
|
|
|
|
// restricted to distinct translations then this limit should be bigger
|
|
|
|
// than n. The n-best factor determines how much bigger the limit should be,
|
|
|
|
// with 0 being 'unlimited.' This actually sets a large-ish limit in case
|
|
|
|
// too many translations are identical.
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
const std::size_t nBestFactor = staticData.GetNBestFactor();
|
|
|
|
std::size_t numDerivations = (nBestFactor == 0) ? n*1000 : n*nBestFactor;
|
|
|
|
|
|
|
|
// Extract the derivations.
|
|
|
|
ChartKBestExtractor::KBestVec bigList;
|
|
|
|
bigList.reserve(numDerivations);
|
|
|
|
extractor.Extract(*topLevelHypos, numDerivations, bigList);
|
|
|
|
|
|
|
|
// Copy derivations into nBestList, skipping ones with repeated translations.
|
|
|
|
std::set<Phrase> distinct;
|
|
|
|
for (ChartKBestExtractor::KBestVec::const_iterator p = bigList.begin();
|
2014-04-21 17:46:22 +04:00
|
|
|
nBestList.size() < n && p != bigList.end(); ++p) {
|
2014-04-21 13:19:00 +04:00
|
|
|
boost::shared_ptr<ChartKBestExtractor::Derivation> derivation = *p;
|
|
|
|
Phrase translation = ChartKBestExtractor::GetOutputPhrase(*derivation);
|
|
|
|
if (distinct.insert(translation).second) {
|
|
|
|
nBestList.push_back(derivation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 00:20:10 +04:00
|
|
|
void ChartManager::WriteSearchGraph(const ChartSearchGraphWriter& writer) const
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2014-08-08 00:20:10 +04:00
|
|
|
|
2011-02-24 15:36:50 +03:00
|
|
|
size_t size = m_source.GetSize();
|
2011-06-27 04:38:43 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
// which hypotheses are reachable?
|
|
|
|
std::map<unsigned,bool> reachable;
|
|
|
|
WordsRange fullRange(0, size-1);
|
|
|
|
const ChartCell &lastCell = m_hypoStackColl.Get(fullRange);
|
2011-06-27 04:38:43 +04:00
|
|
|
const ChartHypothesis *hypo = lastCell.GetBestHypothesis();
|
|
|
|
|
|
|
|
if (hypo == NULL) {
|
|
|
|
// no hypothesis
|
|
|
|
return;
|
|
|
|
}
|
2014-08-08 00:20:10 +04:00
|
|
|
size_t winners = 0;
|
|
|
|
size_t losers = 0;
|
|
|
|
|
|
|
|
FindReachableHypotheses( hypo, reachable, &winners, &losers);
|
|
|
|
writer.WriteHeader(winners, losers);
|
2011-06-27 04:38:43 +04:00
|
|
|
|
2011-02-24 15:36:50 +03:00
|
|
|
for (size_t width = 1; width <= size; ++width) {
|
|
|
|
for (size_t startPos = 0; startPos <= size-width; ++startPos) {
|
|
|
|
size_t endPos = startPos + width - 1;
|
|
|
|
WordsRange range(startPos, endPos);
|
|
|
|
TRACE_ERR(" " << range << "=");
|
|
|
|
|
|
|
|
const ChartCell &cell = m_hypoStackColl.Get(range);
|
2014-08-08 00:20:10 +04:00
|
|
|
cell.WriteSearchGraph(writer, reachable);
|
2011-02-24 15:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
2011-06-27 04:38:43 +04:00
|
|
|
}
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2014-08-08 00:20:10 +04:00
|
|
|
void ChartManager::FindReachableHypotheses(
|
|
|
|
const ChartHypothesis *hypo, std::map<unsigned,bool> &reachable, size_t* winners, size_t* losers) const
|
2011-06-27 04:38:43 +04:00
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
// do not recurse, if already visited
|
|
|
|
if (reachable.find(hypo->GetId()) != reachable.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse
|
|
|
|
reachable[ hypo->GetId() ] = true;
|
2014-08-08 00:20:10 +04:00
|
|
|
if (hypo->GetWinningHypothesis() == hypo) {
|
|
|
|
(*winners)++;
|
|
|
|
} else {
|
|
|
|
(*losers)++;
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
const std::vector<const ChartHypothesis*> &previous = hypo->GetPrevHypos();
|
|
|
|
for(std::vector<const ChartHypothesis*>::const_iterator i = previous.begin(); i != previous.end(); ++i) {
|
2014-08-08 00:20:10 +04:00
|
|
|
FindReachableHypotheses( *i, reachable, winners, losers );
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// also loop over recombined hypotheses (arcs)
|
|
|
|
const ChartArcList *arcList = hypo->GetArcList();
|
|
|
|
if (arcList) {
|
|
|
|
ChartArcList::const_iterator iterArc;
|
|
|
|
for (iterArc = arcList->begin(); iterArc != arcList->end(); ++iterArc) {
|
|
|
|
const ChartHypothesis &arc = **iterArc;
|
2014-08-08 00:20:10 +04:00
|
|
|
FindReachableHypotheses( &arc, reachable, winners, losers );
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2015-05-13 13:05:43 +03:00
|
|
|
void
|
2015-05-12 04:28:58 +03:00
|
|
|
ChartManager::
|
|
|
|
OutputSearchGraphAsHypergraph(std::ostream& out) const
|
2015-01-14 14:07:42 +03:00
|
|
|
{
|
2015-05-12 04:28:58 +03:00
|
|
|
ChartSearchGraphWriterHypergraph writer(&out);
|
2014-08-08 00:20:10 +04:00
|
|
|
WriteSearchGraph(writer);
|
|
|
|
}
|
2014-08-06 19:37:09 +04:00
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
void ChartManager::OutputSearchGraphMoses(std::ostream &outputSearchGraphStream) const
|
|
|
|
{
|
2014-10-08 22:23:26 +04:00
|
|
|
ChartSearchGraphWriterMoses writer(&outputSearchGraphStream, m_source.GetTranslationId());
|
2014-08-08 00:20:10 +04:00
|
|
|
WriteSearchGraph(writer);
|
2014-08-06 19:37:09 +04:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:47:23 +03:00
|
|
|
void ChartManager::OutputBest(OutputCollector *collector) const
|
|
|
|
{
|
|
|
|
const ChartHypothesis *bestHypo = GetBestHypothesis();
|
|
|
|
if (collector && bestHypo) {
|
2015-01-14 14:07:42 +03:00
|
|
|
const size_t translationId = m_source.GetTranslationId();
|
|
|
|
const ChartHypothesis *bestHypo = GetBestHypothesis();
|
|
|
|
OutputBestHypo(collector, bestHypo, translationId);
|
2014-12-10 15:47:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:09:10 +03:00
|
|
|
void ChartManager::OutputNBest(OutputCollector *collector) const
|
2014-12-02 20:40:53 +03:00
|
|
|
{
|
2015-01-14 14:07:42 +03:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
size_t nBestSize = staticData.GetNBestSize();
|
|
|
|
if (nBestSize > 0) {
|
|
|
|
const size_t translationId = m_source.GetTranslationId();
|
|
|
|
|
|
|
|
VERBOSE(2,"WRITING " << nBestSize << " TRANSLATION ALTERNATIVES TO " << staticData.GetNBestFilePath() << endl);
|
|
|
|
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > nBestList;
|
|
|
|
CalcNBest(nBestSize, nBestList,staticData.GetDistinctNBest());
|
|
|
|
OutputNBestList(collector, nBestList, translationId);
|
|
|
|
IFVERBOSE(2) {
|
|
|
|
PrintUserTime("N-Best Hypotheses Generation Time:");
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 20:40:53 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartManager::OutputNBestList(OutputCollector *collector,
|
2015-01-14 14:07:42 +03:00
|
|
|
const ChartKBestExtractor::KBestVec &nBestList,
|
|
|
|
long translationId) const
|
2014-12-02 20:40:53 +03:00
|
|
|
{
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
|
|
|
const std::vector<Moses::FactorType> &outputFactorOrder = staticData.GetOutputFactorOrder();
|
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
|
|
|
|
if (collector->OutputIsCout()) {
|
|
|
|
// 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.
|
|
|
|
FixPrecision(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool includeWordAlignment =
|
2015-01-14 14:07:42 +03:00
|
|
|
StaticData::Instance().PrintAlignmentInfoInNbest();
|
2014-12-02 20:40:53 +03:00
|
|
|
|
|
|
|
bool PrintNBestTrees = StaticData::Instance().PrintNBestTrees();
|
|
|
|
|
|
|
|
for (ChartKBestExtractor::KBestVec::const_iterator p = nBestList.begin();
|
|
|
|
p != nBestList.end(); ++p) {
|
|
|
|
const ChartKBestExtractor::Derivation &derivation = **p;
|
|
|
|
|
|
|
|
// get the derivation's target-side yield
|
|
|
|
Phrase outputPhrase = ChartKBestExtractor::GetOutputPhrase(derivation);
|
|
|
|
|
|
|
|
// delete <s> and </s>
|
|
|
|
UTIL_THROW_IF2(outputPhrase.GetSize() < 2,
|
2015-01-14 14:07:42 +03:00
|
|
|
"Output phrase should have contained at least 2 words (beginning and end-of-sentence)");
|
2014-12-02 20:40:53 +03:00
|
|
|
outputPhrase.RemoveWord(0);
|
|
|
|
outputPhrase.RemoveWord(outputPhrase.GetSize() - 1);
|
|
|
|
|
|
|
|
// print the translation ID, surface factors, and scores
|
|
|
|
out << translationId << " ||| ";
|
|
|
|
OutputSurface(out, outputPhrase, outputFactorOrder, false);
|
|
|
|
out << " ||| ";
|
2015-01-07 17:25:43 +03:00
|
|
|
boost::shared_ptr<ScoreComponentCollection> scoreBreakdown = ChartKBestExtractor::GetOutputScoreBreakdown(derivation);
|
|
|
|
scoreBreakdown->OutputAllFeatureScores(out);
|
2014-12-02 20:40:53 +03:00
|
|
|
out << " ||| " << derivation.score;
|
|
|
|
|
|
|
|
// optionally, print word alignments
|
|
|
|
if (includeWordAlignment) {
|
|
|
|
out << " ||| ";
|
|
|
|
Alignments align;
|
|
|
|
OutputAlignmentNBest(align, derivation, 0);
|
|
|
|
for (Alignments::const_iterator q = align.begin(); q != align.end();
|
|
|
|
++q) {
|
|
|
|
out << q->first << "-" << q->second << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// optionally, print tree
|
|
|
|
if (PrintNBestTrees) {
|
|
|
|
TreePointer tree = ChartKBestExtractor::GetOutputTree(derivation);
|
|
|
|
out << " ||| " << tree->GetString();
|
|
|
|
}
|
|
|
|
|
|
|
|
out << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(collector);
|
|
|
|
collector->Write(translationId, out.str());
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:09:10 +03:00
|
|
|
size_t ChartManager::CalcSourceSize(const Moses::ChartHypothesis *hypo) const
|
2014-12-02 20:40:53 +03:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ChartManager::OutputAlignmentNBest(
|
2015-01-14 14:07:42 +03:00
|
|
|
Alignments &retAlign,
|
|
|
|
const Moses::ChartKBestExtractor::Derivation &derivation,
|
|
|
|
size_t startTarget) const
|
2014-12-02 20:40:53 +03:00
|
|
|
{
|
|
|
|
const ChartHypothesis &hypo = derivation.edge.head->hypothesis;
|
|
|
|
|
|
|
|
size_t totalTargetSize = 0;
|
|
|
|
size_t startSource = hypo.GetCurrSourceRange().GetStartPos();
|
|
|
|
|
|
|
|
const TargetPhrase &tp = hypo.GetCurrTargetPhrase();
|
|
|
|
|
|
|
|
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);
|
|
|
|
vector<size_t> targetOffsets(tp.GetSize(), 0);
|
|
|
|
|
|
|
|
const AlignmentInfo &aiNonTerm = hypo.GetCurrTargetPhrase().GetAlignNonTerm();
|
|
|
|
vector<size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap();
|
|
|
|
const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd = aiNonTerm.GetNonTermIndexMap();
|
|
|
|
|
|
|
|
UTIL_THROW_IF2(sourceInd2pos.size() != derivation.subderivations.size(),
|
|
|
|
"Error");
|
|
|
|
|
|
|
|
size_t targetInd = 0;
|
|
|
|
for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
|
|
|
|
if (tp.GetWord(targetPos).IsNonTerminal()) {
|
|
|
|
UTIL_THROW_IF2(targetPos >= targetPos2SourceInd.size(), "Error");
|
|
|
|
size_t sourceInd = targetPos2SourceInd[targetPos];
|
|
|
|
size_t sourcePos = sourceInd2pos[sourceInd];
|
|
|
|
|
|
|
|
const Moses::ChartKBestExtractor::Derivation &subderivation =
|
|
|
|
*derivation.subderivations[sourceInd];
|
|
|
|
|
|
|
|
// calc source size
|
|
|
|
size_t sourceSize = subderivation.edge.head->hypothesis.GetCurrSourceRange().GetNumWordsCovered();
|
|
|
|
sourceOffsets[sourcePos] = sourceSize;
|
|
|
|
|
|
|
|
// calc target size.
|
|
|
|
// Recursively look thru child hypos
|
|
|
|
size_t currStartTarget = startTarget + totalTargetSize;
|
|
|
|
size_t targetSize = OutputAlignmentNBest(retAlign, subderivation,
|
2015-01-14 14:07:42 +03:00
|
|
|
currStartTarget);
|
2014-12-02 20:40:53 +03:00
|
|
|
targetOffsets[targetPos] = targetSize;
|
|
|
|
|
|
|
|
totalTargetSize += targetSize;
|
|
|
|
++targetInd;
|
|
|
|
} else {
|
|
|
|
++totalTargetSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert position within translation rule to absolute position within
|
|
|
|
// source sentence / output sentence
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
UTIL_THROW_IF2(!ret.second, "Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalTargetSize;
|
|
|
|
}
|
|
|
|
|
2014-12-03 15:05:35 +03:00
|
|
|
void ChartManager::OutputAlignment(OutputCollector *collector) const
|
|
|
|
{
|
|
|
|
if (collector == NULL) {
|
2015-01-14 14:07:42 +03:00
|
|
|
return;
|
2014-12-03 15:05:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ostringstream out;
|
|
|
|
|
|
|
|
const ChartHypothesis *hypo = GetBestHypothesis();
|
|
|
|
if (hypo) {
|
2015-01-14 14:07:42 +03: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 << " ";
|
|
|
|
}
|
2014-12-03 15:05:35 +03:00
|
|
|
}
|
|
|
|
out << endl;
|
|
|
|
|
|
|
|
collector->Write(m_source.GetTranslationId(), out.str());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ChartManager::OutputAlignment(Alignments &retAlign,
|
2015-01-14 14:07:42 +03:00
|
|
|
const Moses::ChartHypothesis *hypo,
|
|
|
|
size_t startTarget) const
|
2014-12-03 15:05:35 +03:00
|
|
|
{
|
|
|
|
size_t totalTargetSize = 0;
|
|
|
|
size_t startSource = hypo->GetCurrSourceRange().GetStartPos();
|
|
|
|
|
|
|
|
const TargetPhrase &tp = hypo->GetCurrTargetPhrase();
|
|
|
|
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
|
|
|
|
UTIL_THROW_IF2(sourceInd2pos.size() != prevHypos.size(), "Error");
|
|
|
|
|
|
|
|
size_t targetInd = 0;
|
|
|
|
for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
|
|
|
|
if (tp.GetWord(targetPos).IsNonTerminal()) {
|
2015-01-14 14:07:42 +03:00
|
|
|
UTIL_THROW_IF2(targetPos >= targetPos2SourceInd.size(), "Error");
|
2014-12-03 15:05:35 +03:00
|
|
|
size_t sourceInd = targetPos2SourceInd[targetPos];
|
|
|
|
size_t sourcePos = sourceInd2pos[sourceInd];
|
|
|
|
|
|
|
|
const ChartHypothesis *prevHypo = prevHypos[sourceInd];
|
|
|
|
|
|
|
|
// calc source size
|
|
|
|
size_t sourceSize = prevHypo->GetCurrSourceRange().GetNumWordsCovered();
|
|
|
|
sourceOffsets[sourcePos] = sourceSize;
|
|
|
|
|
|
|
|
// calc target size.
|
|
|
|
// Recursively look thru child hypos
|
|
|
|
size_t currStartTarget = startTarget + totalTargetSize;
|
|
|
|
size_t targetSize = OutputAlignment(retAlign, prevHypo, currStartTarget);
|
|
|
|
targetOffsets[targetPos] = targetSize;
|
|
|
|
|
|
|
|
totalTargetSize += targetSize;
|
|
|
|
++targetInd;
|
|
|
|
} else {
|
|
|
|
++totalTargetSize;
|
2013-05-29 21:16:15 +04:00
|
|
|
}
|
|
|
|
}
|
2014-12-03 15:05:35 +03:00
|
|
|
|
|
|
|
// convert position within translation rule to absolute position within
|
|
|
|
// source sentence / output sentence
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
UTIL_THROW_IF2(!ret.second, "Error");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalTargetSize;
|
|
|
|
}
|
|
|
|
|
2014-12-03 20:04:10 +03:00
|
|
|
void ChartManager::OutputDetailedTranslationReport(OutputCollector *collector) const
|
|
|
|
{
|
2015-01-14 14:07:42 +03:00
|
|
|
if (collector) {
|
|
|
|
OutputDetailedTranslationReport(collector,
|
|
|
|
GetBestHypothesis(),
|
|
|
|
static_cast<const Sentence&>(m_source),
|
|
|
|
m_source.GetTranslationId());
|
|
|
|
}
|
2014-12-03 20:04:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChartManager::OutputDetailedTranslationReport(
|
2015-01-14 14:07:42 +03:00
|
|
|
OutputCollector *collector,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const
|
2014-12-03 20:04:10 +03:00
|
|
|
{
|
|
|
|
if (hypo == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::ostringstream out;
|
|
|
|
ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
OutputTranslationOptions(out, applicationContext, hypo, sentence, translationId);
|
|
|
|
collector->Write(translationId, out.str());
|
2014-12-05 20:59:53 +03:00
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
//DIMw
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2014-12-05 20:59:53 +03:00
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
if (staticData.IsDetailedAllTranslationReportingEnabled()) {
|
|
|
|
const Sentence &sentence = dynamic_cast<const Sentence &>(m_source);
|
|
|
|
size_t nBestSize = staticData.GetNBestSize();
|
|
|
|
std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > nBestList;
|
|
|
|
CalcNBest(nBestSize, nBestList, staticData.GetDistinctNBest());
|
|
|
|
OutputDetailedAllTranslationReport(collector, nBestList, sentence, translationId);
|
|
|
|
}
|
2014-12-05 20:59:53 +03:00
|
|
|
|
2014-12-03 20:04:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChartManager::OutputTranslationOptions(std::ostream &out,
|
2015-01-14 14:07:42 +03:00
|
|
|
ApplicationContext &applicationContext,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const
|
2014-12-03 20:04:10 +03:00
|
|
|
{
|
|
|
|
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 ChartManager::OutputTranslationOption(std::ostream &out,
|
2015-01-14 14:07:42 +03:00
|
|
|
ApplicationContext &applicationContext,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const
|
2014-12-03 20:04:10 +03:00
|
|
|
{
|
|
|
|
ReconstructApplicationContext(*hypo, sentence, applicationContext);
|
|
|
|
out << "Trans Opt " << translationId
|
|
|
|
<< " " << hypo->GetCurrSourceRange()
|
|
|
|
<< ": ";
|
|
|
|
WriteApplicationContext(out, applicationContext);
|
|
|
|
out << ": " << hypo->GetCurrTargetPhrase().GetTargetLHS()
|
|
|
|
<< "->" << hypo->GetCurrTargetPhrase()
|
|
|
|
<< " " << hypo->GetTotalScore() << hypo->GetScoreBreakdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a hypothesis and sentence, reconstructs the 'application context' --
|
|
|
|
// the source RHS symbols of the SCFG rule that was applied, plus their spans.
|
|
|
|
void ChartManager::ReconstructApplicationContext(const ChartHypothesis &hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
ApplicationContext &context) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 19:25:19 +03:00
|
|
|
void ChartManager::OutputUnknowns(OutputCollector *collector) const
|
|
|
|
{
|
|
|
|
if (collector) {
|
2015-01-14 14:07:42 +03:00
|
|
|
long translationId = m_source.GetTranslationId();
|
|
|
|
const std::vector<Phrase*> &oovs = GetParser().GetUnknownSources();
|
2014-12-04 19:25:19 +03:00
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
std::ostringstream out;
|
|
|
|
for (std::vector<Phrase*>::const_iterator p = oovs.begin();
|
|
|
|
p != oovs.end(); ++p) {
|
2015-02-18 19:22:13 +03:00
|
|
|
out << **p;
|
2015-01-14 14:07:42 +03:00
|
|
|
}
|
|
|
|
out << std::endl;
|
|
|
|
collector->Write(translationId, out.str());
|
2014-12-04 19:25:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-03 20:04:10 +03:00
|
|
|
|
2014-12-04 21:35:19 +03:00
|
|
|
void ChartManager::OutputDetailedTreeFragmentsTranslationReport(OutputCollector *collector) const
|
|
|
|
{
|
|
|
|
const ChartHypothesis *hypo = GetBestHypothesis();
|
|
|
|
if (collector == NULL || hypo == NULL) {
|
2015-01-14 14:07:42 +03:00
|
|
|
return;
|
2014-12-04 21:35:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
const Sentence &sentence = dynamic_cast<const Sentence &>(m_source);
|
|
|
|
const size_t translationId = m_source.GetTranslationId();
|
|
|
|
|
|
|
|
OutputTreeFragmentsTranslationOptions(out, applicationContext, hypo, sentence, translationId);
|
|
|
|
|
|
|
|
//Tree of full sentence
|
|
|
|
const StatefulFeatureFunction* treeStructure = StaticData::Instance().GetTreeStructure();
|
|
|
|
if (treeStructure != NULL) {
|
2015-01-14 14:07:42 +03:00
|
|
|
const vector<const StatefulFeatureFunction*>& sff = StatefulFeatureFunction::GetStatefulFeatureFunctions();
|
|
|
|
for( size_t i=0; i<sff.size(); i++ ) {
|
|
|
|
if (sff[i] == treeStructure) {
|
|
|
|
const TreeState* tree = dynamic_cast<const TreeState*>(hypo->GetFFState(i));
|
|
|
|
out << "Full Tree " << translationId << ": " << tree->GetTree()->GetString() << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:35:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
collector->Write(translationId, out.str());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartManager::OutputTreeFragmentsTranslationOptions(std::ostream &out,
|
2015-01-14 14:07:42 +03:00
|
|
|
ApplicationContext &applicationContext,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const
|
2014-12-04 21:35:19 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (hypo != NULL) {
|
|
|
|
OutputTranslationOption(out, applicationContext, hypo, sentence, translationId);
|
|
|
|
|
|
|
|
const TargetPhrase &currTarPhr = hypo->GetCurrTargetPhrase();
|
|
|
|
|
|
|
|
out << " ||| ";
|
|
|
|
if (const PhraseProperty *property = currTarPhr.GetProperty("Tree")) {
|
|
|
|
out << " " << *property->GetValueString();
|
|
|
|
} else {
|
|
|
|
out << " " << "noTreeInfo";
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
OutputTreeFragmentsTranslationOptions(out, applicationContext, prevHypo, sentence, translationId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 20:23:08 +03:00
|
|
|
void ChartManager::OutputSearchGraph(OutputCollector *collector) const
|
|
|
|
{
|
2015-01-14 14:07:42 +03:00
|
|
|
if (collector) {
|
|
|
|
long translationId = m_source.GetTranslationId();
|
|
|
|
std::ostringstream out;
|
|
|
|
OutputSearchGraphMoses( out);
|
|
|
|
collector->Write(translationId, out.str());
|
|
|
|
}
|
2014-12-05 20:23:08 +03:00
|
|
|
}
|
2014-12-04 21:35:19 +03:00
|
|
|
|
2014-12-05 20:59:53 +03:00
|
|
|
//DIMw
|
|
|
|
void ChartManager::OutputDetailedAllTranslationReport(
|
2015-01-14 14:07:42 +03:00
|
|
|
OutputCollector *collector,
|
|
|
|
const std::vector<boost::shared_ptr<Moses::ChartKBestExtractor::Derivation> > &nBestList,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const
|
2014-12-05 20:59:53 +03:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
const ChartCellCollection& cells = GetChartCellCollection();
|
|
|
|
size_t size = GetSource().GetSize();
|
|
|
|
for (size_t width = 1; width <= size; ++width) {
|
|
|
|
for (size_t startPos = 0; startPos <= size-width; ++startPos) {
|
|
|
|
size_t endPos = startPos + width - 1;
|
|
|
|
WordsRange range(startPos, endPos);
|
|
|
|
const ChartCell& cell = cells.Get(range);
|
|
|
|
const HypoList* hyps = cell.GetAllSortedHypotheses();
|
|
|
|
out << "Chart Cell [" << startPos << ".." << endPos << "]" << endl;
|
|
|
|
HypoList::const_iterator iter;
|
|
|
|
size_t c = 1;
|
|
|
|
for (iter = hyps->begin(); iter != hyps->end(); ++iter) {
|
|
|
|
out << "----------------Item " << c++ << " ---------------------"
|
|
|
|
<< endl;
|
|
|
|
OutputTranslationOptions(out, applicationContext, *iter,
|
|
|
|
sentence, translationId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collector->Write(translationId, out.str());
|
|
|
|
}
|
|
|
|
|
2015-05-12 04:28:58 +03:00
|
|
|
// void ChartManager::OutputSearchGraphHypergraph() const
|
|
|
|
// {
|
|
|
|
// const StaticData &staticData = StaticData::Instance();
|
|
|
|
// if (staticData.GetOutputSearchGraphHypergraph()) {
|
|
|
|
// HypergraphOutput<ChartManager> hypergraphOutputChart(PRECISION);
|
|
|
|
// hypergraphOutputChart.Write(*this);
|
|
|
|
// }
|
|
|
|
// }
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2014-12-10 15:47:23 +03:00
|
|
|
void ChartManager::OutputBestHypo(OutputCollector *collector, const ChartHypothesis *hypo, long translationId) const
|
|
|
|
{
|
|
|
|
if (!collector)
|
|
|
|
return;
|
|
|
|
std::ostringstream out;
|
|
|
|
FixPrecision(out);
|
|
|
|
if (hypo != NULL) {
|
|
|
|
VERBOSE(1,"BEST TRANSLATION: " << *hypo << endl);
|
|
|
|
VERBOSE(3,"Best path: ");
|
|
|
|
Backtrack(hypo);
|
|
|
|
VERBOSE(3,"0" << std::endl);
|
|
|
|
|
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
|
|
|
out << hypo->GetTotalScore() << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StaticData::Instance().IsPathRecoveryEnabled()) {
|
|
|
|
out << "||| ";
|
|
|
|
}
|
|
|
|
Phrase outPhrase(ARRAY_SIZE_INCR);
|
|
|
|
hypo->GetOutputPhrase(outPhrase);
|
|
|
|
|
|
|
|
// delete 1st & last
|
|
|
|
UTIL_THROW_IF2(outPhrase.GetSize() < 2,
|
2015-01-14 14:07:42 +03:00
|
|
|
"Output phrase should have contained at least 2 words (beginning and end-of-sentence)");
|
2014-12-10 15:47:23 +03:00
|
|
|
|
|
|
|
outPhrase.RemoveWord(0);
|
|
|
|
outPhrase.RemoveWord(outPhrase.GetSize() - 1);
|
|
|
|
|
|
|
|
const std::vector<FactorType> outputFactorOrder = StaticData::Instance().GetOutputFactorOrder();
|
|
|
|
string output = outPhrase.GetStringRep(outputFactorOrder);
|
|
|
|
out << output << endl;
|
|
|
|
} else {
|
|
|
|
VERBOSE(1, "NO BEST TRANSLATION" << endl);
|
|
|
|
|
|
|
|
if (StaticData::Instance().GetOutputHypoScore()) {
|
|
|
|
out << "0 ";
|
|
|
|
}
|
|
|
|
|
|
|
|
out << endl;
|
|
|
|
}
|
|
|
|
collector->Write(translationId, out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartManager::Backtrack(const ChartHypothesis *hypo) const
|
|
|
|
{
|
|
|
|
const vector<const ChartHypothesis*> &prevHypos = hypo->GetPrevHypos();
|
|
|
|
|
|
|
|
vector<const ChartHypothesis*>::const_iterator iter;
|
|
|
|
for (iter = prevHypos.begin(); iter != prevHypos.end(); ++iter) {
|
|
|
|
const ChartHypothesis *prevHypo = *iter;
|
|
|
|
|
|
|
|
VERBOSE(3,prevHypo->GetId() << " <= ");
|
|
|
|
Backtrack(prevHypo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 15:28:02 +04:00
|
|
|
} // namespace Moses
|