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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
2012-09-27 01:49:33 +04:00
|
|
|
#include <boost/unordered_map.hpp>
|
2010-04-08 21:16:10 +04:00
|
|
|
#include "ChartCell.h"
|
|
|
|
#include "ChartCellCollection.h"
|
2011-03-11 16:08:43 +03:00
|
|
|
#include "InputType.h"
|
|
|
|
#include "WordsRange.h"
|
|
|
|
#include "SentenceStats.h"
|
2012-09-25 20:08:38 +04:00
|
|
|
#include "ChartTranslationOptionList.h"
|
2012-10-11 16:45:36 +04:00
|
|
|
#include "ChartParser.h"
|
2014-04-21 13:19:00 +04:00
|
|
|
#include "ChartKBestExtractor.h"
|
2014-12-02 17:38:03 +03:00
|
|
|
#include "BaseManager.h"
|
2014-12-02 20:40:53 +03:00
|
|
|
#include "moses/Syntax/KBestExtractor.h"
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2011-11-08 15:28:02 +04:00
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
namespace Moses
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
|
|
|
|
2011-03-11 16:08:43 +03:00
|
|
|
class ChartHypothesis;
|
2014-08-08 00:20:10 +04:00
|
|
|
class ChartSearchGraphWriter;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** Holds everything you need to decode 1 sentence with the hierachical/syntax decoder
|
|
|
|
*/
|
2014-12-02 17:38:03 +03:00
|
|
|
class ChartManager : public BaseManager
|
2010-04-08 21:16:10 +04:00
|
|
|
{
|
2011-10-06 14:31:09 +04:00
|
|
|
private:
|
2011-03-11 19:28:36 +03:00
|
|
|
InputType const& m_source; /**< source sentence to be translated */
|
2011-02-24 15:36:50 +03:00
|
|
|
ChartCellCollection m_hypoStackColl;
|
2011-03-11 19:28:36 +03:00
|
|
|
std::auto_ptr<SentenceStats> m_sentenceStats;
|
2011-02-24 15:36:50 +03:00
|
|
|
clock_t m_start; /**< starting time, used for logging */
|
2011-10-06 14:31:09 +04:00
|
|
|
unsigned m_hypothesisId; /* For handing out hypothesis ids to ChartHypothesis */
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2012-10-11 15:29:02 +04:00
|
|
|
ChartParser m_parser;
|
2012-10-11 14:54:22 +04:00
|
|
|
|
2012-09-25 20:08:38 +04:00
|
|
|
ChartTranslationOptionList m_translationOptionList; /**< pre-computed list of translation options for the phrases in this sentence */
|
|
|
|
|
2014-08-08 00:20:10 +04:00
|
|
|
/* auxilliary functions for SearchGraphs */
|
|
|
|
void FindReachableHypotheses(
|
|
|
|
const ChartHypothesis *hypo, std::map<unsigned,bool> &reachable , size_t* winners, size_t* losers) const;
|
|
|
|
void WriteSearchGraph(const ChartSearchGraphWriter& writer) const;
|
|
|
|
|
2014-12-02 20:40:53 +03:00
|
|
|
// output
|
|
|
|
void OutputNBestList(OutputCollector *collector,
|
|
|
|
const ChartKBestExtractor::KBestVec &nBestList,
|
2014-12-02 22:09:10 +03:00
|
|
|
long translationId) const;
|
|
|
|
size_t CalcSourceSize(const Moses::ChartHypothesis *hypo) const;
|
2014-12-02 20:40:53 +03:00
|
|
|
size_t OutputAlignmentNBest(Alignments &retAlign,
|
|
|
|
const Moses::ChartKBestExtractor::Derivation &derivation,
|
2014-12-02 22:09:10 +03:00
|
|
|
size_t startTarget) const;
|
2014-12-03 15:05:35 +03:00
|
|
|
size_t OutputAlignment(Alignments &retAlign,
|
|
|
|
const Moses::ChartHypothesis *hypo,
|
|
|
|
size_t startTarget) const;
|
2014-12-03 20:04:10 +03:00
|
|
|
void OutputDetailedTranslationReport(
|
|
|
|
OutputCollector *collector,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const;
|
|
|
|
void OutputTranslationOptions(std::ostream &out,
|
|
|
|
ApplicationContext &applicationContext,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const;
|
|
|
|
void OutputTranslationOption(std::ostream &out,
|
|
|
|
ApplicationContext &applicationContext,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const;
|
|
|
|
void ReconstructApplicationContext(const ChartHypothesis &hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
ApplicationContext &context) const;
|
2014-12-04 21:35:19 +03:00
|
|
|
void OutputTreeFragmentsTranslationOptions(std::ostream &out,
|
|
|
|
ApplicationContext &applicationContext,
|
|
|
|
const ChartHypothesis *hypo,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const;
|
2014-12-05 20:59:53 +03:00
|
|
|
void OutputDetailedAllTranslationReport(
|
|
|
|
OutputCollector *collector,
|
|
|
|
const std::vector<boost::shared_ptr<Moses::ChartKBestExtractor::Derivation> > &nBestList,
|
|
|
|
const Sentence &sentence,
|
|
|
|
long translationId) const;
|
2014-12-02 20:40:53 +03:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
public:
|
2014-10-08 22:23:26 +04:00
|
|
|
ChartManager(InputType const& source);
|
2011-03-11 16:08:43 +03:00
|
|
|
~ChartManager();
|
2014-12-05 20:59:53 +03:00
|
|
|
void Decode();
|
2012-03-28 07:29:24 +04:00
|
|
|
void AddXmlChartOptions();
|
2011-03-11 16:08:43 +03:00
|
|
|
const ChartHypothesis *GetBestHypothesis() const;
|
2014-04-21 13:19:00 +04:00
|
|
|
void CalcNBest(size_t n, std::vector<boost::shared_ptr<ChartKBestExtractor::Derivation> > &nBestList, bool onlyDistinct=false) const;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2014-08-08 00:20:10 +04:00
|
|
|
/** "Moses" (osg) type format */
|
|
|
|
void OutputSearchGraphMoses(std::ostream &outputSearchGraphStream) const;
|
|
|
|
|
2014-08-06 19:37:09 +04:00
|
|
|
/** Output in (modified) Kenneth hypergraph format */
|
|
|
|
void OutputSearchGraphAsHypergraph(std::ostream &outputSearchGraphStream) const;
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2014-08-08 00:20:10 +04:00
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
//! the input sentence being decoded
|
2011-03-11 19:28:36 +03:00
|
|
|
const InputType& GetSource() const {
|
2011-02-24 15:36:50 +03:00
|
|
|
return m_source;
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
//! debug data collected when decoding sentence
|
2011-03-11 19:28:36 +03:00
|
|
|
SentenceStats& GetSentenceStats() const {
|
2010-04-08 21:16:10 +04:00
|
|
|
return *m_sentenceStats;
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2013-09-23 18:30:28 +04:00
|
|
|
//DIMw
|
|
|
|
const ChartCellCollection& GetChartCellCollection() const {
|
|
|
|
return m_hypoStackColl;
|
|
|
|
}
|
|
|
|
|
2011-02-24 15:36:50 +03:00
|
|
|
/***
|
|
|
|
* to be called after processing a sentence (which may consist of more than just calling ProcessSentence() )
|
2012-07-03 21:11:53 +04:00
|
|
|
* currently an empty function
|
2011-02-24 15:36:50 +03:00
|
|
|
*/
|
2013-06-10 21:11:55 +04:00
|
|
|
void CalcDecoderStatistics() const {
|
|
|
|
}
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2011-03-11 19:28:36 +03:00
|
|
|
void ResetSentenceStats(const InputType& source) {
|
|
|
|
m_sentenceStats = std::auto_ptr<SentenceStats>(new SentenceStats(source));
|
2010-04-08 21:16:10 +04:00
|
|
|
}
|
2011-02-24 15:36:50 +03:00
|
|
|
|
2012-07-03 21:11:53 +04:00
|
|
|
//! contigious hypo id for each input sentence. For debugging purposes
|
2013-05-29 21:16:15 +04:00
|
|
|
unsigned GetNextHypoId() {
|
|
|
|
return m_hypothesisId++;
|
|
|
|
}
|
2014-03-17 17:09:54 +04:00
|
|
|
|
|
|
|
const ChartParser &GetParser() const { return m_parser; }
|
2014-08-06 19:37:09 +04:00
|
|
|
|
2014-12-02 20:40:53 +03:00
|
|
|
// outputs
|
2014-12-02 22:09:10 +03:00
|
|
|
void OutputNBest(OutputCollector *collector) const;
|
2014-12-03 15:05:35 +03:00
|
|
|
void OutputLatticeSamples(OutputCollector *collector) const
|
|
|
|
{}
|
|
|
|
void OutputAlignment(OutputCollector *collector) const;
|
2014-12-03 20:04:10 +03:00
|
|
|
void OutputDetailedTranslationReport(OutputCollector *collector) const;
|
2014-12-04 19:25:19 +03:00
|
|
|
void OutputUnknowns(OutputCollector *collector) const;
|
2014-12-04 21:35:19 +03:00
|
|
|
void OutputDetailedTreeFragmentsTranslationReport(OutputCollector *collector) const;
|
2014-12-05 20:23:08 +03:00
|
|
|
void OutputWordGraph(OutputCollector *collector) const
|
|
|
|
{}
|
|
|
|
void OutputSearchGraph(OutputCollector *collector) const;
|
2014-12-05 22:40:51 +03:00
|
|
|
void OutputSearchGraphSLF() const
|
|
|
|
{}
|
2014-12-03 20:04:10 +03:00
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|