mosesdecoder/moses/ChartHypothesis.h

223 lines
7.0 KiB
C
Raw Normal View History

// vim:tabstop=2
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2010 Hieu Hoang
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.
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.
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
***********************************************************************/
#pragma once
#include <vector>
2015-01-07 17:25:43 +03:00
#include <boost/scoped_ptr.hpp>
#include "Util.h"
#include "WordsRange.h"
#include "ScoreComponentCollection.h"
#include "Phrase.h"
2012-09-25 20:34:43 +04:00
#include "ChartTranslationOptions.h"
#include "ObjectPool.h"
namespace Moses
{
class ChartKBestExtractor;
class ChartHypothesis;
class ChartManager;
class RuleCubeItem;
2013-09-01 19:58:23 +04:00
class FFState;
typedef std::vector<ChartHypothesis*> ChartArcList;
/** a hypothesis in the hierarchical/syntax decoder.
* Contain a pointer to the current target phrase, a vector of previous hypos, and some scores
*/
class ChartHypothesis
{
friend std::ostream& operator<<(std::ostream&, const ChartHypothesis&);
2015-01-07 17:25:43 +03:00
// friend class ChartKBestExtractor;
protected:
#ifdef USE_HYPO_POOL
static ObjectPool<ChartHypothesis> s_objectPool;
#endif
2013-08-13 13:33:04 +04:00
boost::shared_ptr<ChartTranslationOption> m_transOpt;
WordsRange m_currSourceWordsRange;
2013-05-29 21:16:15 +04:00
std::vector<const FFState*> m_ffStates; /*! stateful feature function states */
2015-01-07 17:25:43 +03:00
/*! sum of scores of this hypothesis, and previous hypotheses. Lazily initialised. */
2015-01-14 14:07:42 +03:00
mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown;
mutable boost::scoped_ptr<ScoreComponentCollection> m_deltaScoreBreakdown;
2015-01-07 17:25:43 +03:00
ScoreComponentCollection m_currScoreBreakdown /*! scores for this hypothesis only */
,m_lmNGram
,m_lmPrefix;
float m_totalScore;
ChartArcList *m_arcList; /*! all arcs that end at the same trellis point as this hypothesis */
const ChartHypothesis *m_winningHypo;
std::vector<const ChartHypothesis*> m_prevHypos; // always sorted by source position?
ChartManager& m_manager;
unsigned m_id; /* pkoehn wants to log the order in which hypotheses were generated */
2012-07-03 21:11:53 +04:00
//! not implemented
2013-05-29 21:16:15 +04:00
ChartHypothesis();
2012-07-03 21:11:53 +04:00
//! not implemented
ChartHypothesis(const ChartHypothesis &copy);
public:
#ifdef USE_HYPO_POOL
2015-01-14 14:07:42 +03:00
void *operator new(size_t /* num_bytes */) {
void *ptr = s_objectPool.getPtr();
return ptr;
}
2012-07-02 23:01:07 +04:00
//! delete \param hypo. Works with object pool too
2015-01-14 14:07:42 +03:00
static void Delete(ChartHypothesis *hypo) {
s_objectPool.freeObject(hypo);
}
#else
2012-07-02 23:01:07 +04:00
//! delete \param hypo. Works with object pool too
2015-01-14 14:07:42 +03:00
static void Delete(ChartHypothesis *hypo) {
delete hypo;
}
#endif
2012-09-25 20:34:43 +04:00
ChartHypothesis(const ChartTranslationOptions &, const RuleCubeItem &item,
ChartManager &manager);
2015-01-07 17:25:43 +03:00
//! only used by ChartKBestExtractor
ChartHypothesis(const ChartHypothesis &, const ChartKBestExtractor &);
~ChartHypothesis();
2015-01-14 14:07:42 +03:00
unsigned GetId() const {
2013-05-29 21:16:15 +04:00
return m_id;
}
2015-01-14 14:07:42 +03:00
const ChartTranslationOption &GetTranslationOption() const {
2013-08-13 13:33:04 +04:00
return *m_transOpt;
}
2012-07-02 23:01:07 +04:00
//! Get the rule that created this hypothesis
2015-01-14 14:07:42 +03:00
const TargetPhrase &GetCurrTargetPhrase() const {
2013-08-13 13:33:04 +04:00
return m_transOpt->GetPhrase();
}
2013-05-29 21:16:15 +04:00
2012-07-02 23:01:07 +04:00
//! the source range that this hypothesis spans
2015-01-14 14:07:42 +03:00
const WordsRange &GetCurrSourceRange() const {
return m_currSourceWordsRange;
}
2013-05-29 21:16:15 +04:00
2012-07-02 23:01:07 +04:00
//! the arc list when creating n-best lists
2015-01-14 14:07:42 +03:00
inline const ChartArcList* GetArcList() const {
return m_arcList;
}
2013-05-29 21:16:15 +04:00
2012-07-02 23:01:07 +04:00
//! the feature function states for a particular feature \param featureID
2015-01-14 14:07:42 +03:00
inline const FFState* GetFFState( size_t featureID ) const {
2013-05-29 21:16:15 +04:00
return m_ffStates[ featureID ];
}
2012-07-02 23:01:07 +04:00
//! reference back to the manager
2015-01-14 14:07:42 +03:00
inline const ChartManager& GetManager() const {
2013-05-29 21:16:15 +04:00
return m_manager;
}
void GetOutputPhrase(Phrase &outPhrase) const;
Phrase GetOutputPhrase() const;
// get leftmost/rightmost words only
// leftRightMost: 1=left, 2=right
2015-01-07 17:25:43 +03:00
void GetOutputPhrase(size_t leftRightMost, size_t numWords, Phrase &outPhrase) const;
2013-05-29 21:16:15 +04:00
int RecombineCompare(const ChartHypothesis &compare) const;
2014-08-08 19:41:16 +04:00
void EvaluateWhenApplied();
void AddArc(ChartHypothesis *loserHypo);
void CleanupArcList();
void SetWinningHypo(const ChartHypothesis *hypo);
2012-07-03 21:11:53 +04:00
//! get the unweighted score for each feature function
2015-01-14 14:07:42 +03:00
const ScoreComponentCollection &GetScoreBreakdown() const {
2015-01-07 17:25:43 +03:00
// Note: never call this method before m_currScoreBreakdown is fully computed
2015-01-14 14:07:42 +03:00
if (!m_scoreBreakdown.get()) {
2015-01-07 17:25:43 +03:00
m_scoreBreakdown.reset(new ScoreComponentCollection());
// score breakdown from current translation rule
2015-01-14 14:07:42 +03:00
if (m_transOpt) {
2015-01-07 17:25:43 +03:00
m_scoreBreakdown->PlusEquals(GetTranslationOption().GetScores());
}
m_scoreBreakdown->PlusEquals(m_currScoreBreakdown);
// score breakdowns from prev hypos
2015-01-14 14:07:42 +03:00
for (std::vector<const ChartHypothesis*>::const_iterator iter = m_prevHypos.begin(); iter != m_prevHypos.end(); ++iter) {
2015-01-07 17:25:43 +03:00
const ChartHypothesis &prevHypo = **iter;
m_scoreBreakdown->PlusEquals(prevHypo.GetScoreBreakdown());
}
}
return *(m_scoreBreakdown.get());
}
//! get the unweighted score delta for each feature function
2015-01-14 14:07:42 +03:00
const ScoreComponentCollection &GetDeltaScoreBreakdown() const {
2015-01-07 17:25:43 +03:00
// Note: never call this method before m_currScoreBreakdown is fully computed
2015-01-14 14:07:42 +03:00
if (!m_deltaScoreBreakdown.get()) {
2015-01-07 17:25:43 +03:00
m_deltaScoreBreakdown.reset(new ScoreComponentCollection());
// score breakdown from current translation rule
2015-01-14 14:07:42 +03:00
if (m_transOpt) {
2015-01-07 17:25:43 +03:00
m_deltaScoreBreakdown->PlusEquals(GetTranslationOption().GetScores());
}
m_deltaScoreBreakdown->PlusEquals(m_currScoreBreakdown);
// delta: score breakdowns from prev hypos _not_ added
}
return *(m_deltaScoreBreakdown.get());
2013-05-29 21:16:15 +04:00
}
2012-07-03 21:11:53 +04:00
//! Get the weighted total score
2015-01-14 14:07:42 +03:00
float GetTotalScore() const {
2015-01-07 17:25:43 +03:00
// scores from current translation rule. eg. translation models & word penalty
2013-05-29 21:16:15 +04:00
return m_totalScore;
}
2013-05-29 21:16:15 +04:00
//! vector of previous hypotheses this hypo is built on
2015-01-14 14:07:42 +03:00
const std::vector<const ChartHypothesis*> &GetPrevHypos() const {
2013-05-29 21:16:15 +04:00
return m_prevHypos;
}
2012-07-02 23:01:07 +04:00
//! get a particular previous hypos
2015-01-14 14:07:42 +03:00
const ChartHypothesis* GetPrevHypo(size_t pos) const {
2013-05-29 21:16:15 +04:00
return m_prevHypos[pos];
}
2012-07-03 21:11:53 +04:00
//! get the constituency label that covers this hypo
2015-01-14 14:07:42 +03:00
const Word &GetTargetLHS() const {
return GetCurrTargetPhrase().GetTargetLHS();
}
2012-07-02 23:01:07 +04:00
//! get the best hypo in the arc list when doing n-best list creation. It's either this hypothesis, or the best hypo is this hypo is in the arc list
2015-01-14 14:07:42 +03:00
const ChartHypothesis* GetWinningHypothesis() const {
2013-05-29 21:16:15 +04:00
return m_winningHypo;
}
TO_STRING();
}; // class ChartHypothesis
}