2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
|
|
|
// vim:tabstop=2
|
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2006 University of Edinburgh
|
|
|
|
|
|
|
|
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
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "BitmapContainer.h"
|
|
|
|
#include "HypothesisStackCubePruning.h"
|
2013-05-25 02:57:06 +04:00
|
|
|
#include "moses/FF/DistortionScoreProducer.h"
|
2008-10-30 21:43:17 +03:00
|
|
|
#include "TranslationOptionList.h"
|
2014-01-03 22:45:31 +04:00
|
|
|
#include "Manager.h"
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
class HypothesisScoreOrdererNoDistortion
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
public:
|
|
|
|
bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
|
|
|
|
const float scoreA = hypoA->GetScore();
|
|
|
|
const float scoreB = hypoB->GetScore();
|
|
|
|
|
|
|
|
if (scoreA > scoreB) {
|
|
|
|
return true;
|
|
|
|
} else if (scoreA < scoreB) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return hypoA < hypoB;
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class HypothesisScoreOrdererWithDistortion
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
public:
|
2013-05-11 17:13:26 +04:00
|
|
|
HypothesisScoreOrdererWithDistortion(const WordsRange* transOptRange) :
|
2014-07-24 19:01:54 +04:00
|
|
|
m_transOptRange(transOptRange) {
|
|
|
|
m_totalWeightDistortion = 0;
|
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2014-10-20 15:17:07 +04:00
|
|
|
|
2014-12-14 02:19:09 +03:00
|
|
|
const std::vector<const DistortionScoreProducer*> &ffs = DistortionScoreProducer::GetDistortionFeatureFunctions();
|
2014-10-20 15:17:07 +04:00
|
|
|
std::vector<const DistortionScoreProducer*>::const_iterator iter;
|
2014-07-24 19:01:54 +04:00
|
|
|
for (iter = ffs.begin(); iter != ffs.end(); ++iter) {
|
2014-10-20 15:17:07 +04:00
|
|
|
const DistortionScoreProducer *ff = *iter;
|
2014-07-24 19:01:54 +04:00
|
|
|
|
2014-10-20 15:17:07 +04:00
|
|
|
float weight =staticData.GetAllWeights().GetScoreForProducer(ff);
|
|
|
|
m_totalWeightDistortion += weight;
|
2014-07-24 19:01:54 +04:00
|
|
|
}
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
const WordsRange* m_transOptRange;
|
2014-07-24 19:01:54 +04:00
|
|
|
float m_totalWeightDistortion;
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
|
2013-11-21 18:55:41 +04:00
|
|
|
UTIL_THROW_IF2(m_transOptRange == NULL, "Words range not set");
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2013-02-19 22:31:59 +04:00
|
|
|
|
|
|
|
const float distortionScoreA = DistortionScoreProducer::CalculateDistortionScore(
|
2011-02-24 16:14:42 +03:00
|
|
|
*hypoA,
|
|
|
|
hypoA->GetCurrSourceWordsRange(),
|
|
|
|
*m_transOptRange,
|
|
|
|
hypoA->GetWordsBitmap().GetFirstGapPos()
|
|
|
|
);
|
2013-02-19 22:31:59 +04:00
|
|
|
const float distortionScoreB = DistortionScoreProducer::CalculateDistortionScore(
|
2011-02-24 16:14:42 +03:00
|
|
|
*hypoB,
|
|
|
|
hypoB->GetCurrSourceWordsRange(),
|
|
|
|
*m_transOptRange,
|
|
|
|
hypoB->GetWordsBitmap().GetFirstGapPos()
|
|
|
|
);
|
|
|
|
|
2013-02-20 02:29:48 +04:00
|
|
|
|
2014-07-24 19:01:54 +04:00
|
|
|
const float scoreA = hypoA->GetScore() + distortionScoreA * m_totalWeightDistortion;
|
|
|
|
const float scoreB = hypoB->GetScore() + distortionScoreB * m_totalWeightDistortion;
|
2013-02-20 02:29:48 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
if (scoreA > scoreB) {
|
|
|
|
return true;
|
|
|
|
} else if (scoreA < scoreB) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return hypoA < hypoB;
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BackwardsEdge Code
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
BackwardsEdge::BackwardsEdge(const BitmapContainer &prevBitmapContainer
|
2011-02-24 16:14:42 +03:00
|
|
|
, BitmapContainer &parent
|
|
|
|
, const TranslationOptionList &translations
|
|
|
|
, const SquareMatrix &futureScore,
|
2013-05-11 17:13:26 +04:00
|
|
|
const InputType& itype)
|
2008-06-11 14:52:57 +04:00
|
|
|
: m_initialized(false)
|
|
|
|
, m_prevBitmapContainer(prevBitmapContainer)
|
|
|
|
, m_parent(parent)
|
|
|
|
, m_translations(translations)
|
|
|
|
, m_futurescore(futureScore)
|
|
|
|
, m_seenPosition()
|
|
|
|
{
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
// If either dimension is empty, we haven't got anything to do.
|
|
|
|
if(m_prevBitmapContainer.GetHypotheses().size() == 0 || m_translations.size() == 0) {
|
|
|
|
VERBOSE(3, "Empty cube on BackwardsEdge" << std::endl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the things we need for distortion cost computation.
|
|
|
|
int maxDistortion = StaticData::Instance().GetMaxDistortion();
|
|
|
|
|
|
|
|
if (maxDistortion == -1) {
|
|
|
|
for (HypothesisSet::const_iterator iter = m_prevBitmapContainer.GetHypotheses().begin(); iter != m_prevBitmapContainer.GetHypotheses().end(); ++iter) {
|
|
|
|
m_hypotheses.push_back(*iter);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WordsRange &transOptRange = translations.Get(0)->GetSourceWordsRange();
|
|
|
|
|
|
|
|
HypothesisSet::const_iterator iterHypo = m_prevBitmapContainer.GetHypotheses().begin();
|
|
|
|
HypothesisSet::const_iterator iterEnd = m_prevBitmapContainer.GetHypotheses().end();
|
|
|
|
|
|
|
|
while (iterHypo != iterEnd) {
|
|
|
|
const Hypothesis &hypo = **iterHypo;
|
|
|
|
// Special case: If this is the first hypothesis used to seed the search,
|
|
|
|
// it doesn't have a valid range, and we create the hypothesis, if the
|
|
|
|
// initial position is not further into the sentence than the distortion limit.
|
|
|
|
if (hypo.GetWordsBitmap().GetNumWordsCovered() == 0) {
|
|
|
|
if ((int)transOptRange.GetStartPos() <= maxDistortion)
|
|
|
|
m_hypotheses.push_back(&hypo);
|
|
|
|
} else {
|
|
|
|
int distortionDistance = itype.ComputeDistortionDistance(hypo.GetCurrSourceWordsRange()
|
|
|
|
, transOptRange);
|
|
|
|
|
|
|
|
if (distortionDistance <= maxDistortion)
|
|
|
|
m_hypotheses.push_back(&hypo);
|
|
|
|
}
|
|
|
|
|
|
|
|
++iterHypo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_translations.size() > 1) {
|
2014-07-09 05:41:28 +04:00
|
|
|
UTIL_THROW_IF2(m_translations.Get(0)->GetFutureScore() < m_translations.Get(1)->GetFutureScore(),
|
2015-01-14 14:07:42 +03:00
|
|
|
"Non-monotonic future score: "
|
|
|
|
<< m_translations.Get(0)->GetFutureScore() << " vs. "
|
|
|
|
<< m_translations.Get(1)->GetFutureScore());
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_hypotheses.size() > 1) {
|
2013-11-21 18:55:41 +04:00
|
|
|
UTIL_THROW_IF2(m_hypotheses[0]->GetTotalScore() < m_hypotheses[1]->GetTotalScore(),
|
2015-01-14 14:07:42 +03:00
|
|
|
"Non-monotonic total score"
|
|
|
|
<< m_hypotheses[0]->GetTotalScore() << " vs. "
|
|
|
|
<< m_hypotheses[1]->GetTotalScore());
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
2013-05-11 17:13:26 +04:00
|
|
|
HypothesisScoreOrdererWithDistortion orderer (&transOptRange);
|
2011-02-24 16:14:42 +03:00
|
|
|
std::sort(m_hypotheses.begin(), m_hypotheses.end(), orderer);
|
|
|
|
|
|
|
|
// std::sort(m_hypotheses.begin(), m_hypotheses.end(), HypothesisScoreOrdererNoDistortion());
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BackwardsEdge::~BackwardsEdge()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
m_seenPosition.clear();
|
|
|
|
m_hypotheses.clear();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BackwardsEdge::Initialize()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
if(m_hypotheses.size() == 0 || m_translations.size() == 0) {
|
|
|
|
m_initialized = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hypothesis *expanded = CreateHypothesis(*m_hypotheses[0], *m_translations.Get(0));
|
|
|
|
m_parent.Enqueue(0, 0, expanded, this);
|
|
|
|
SetSeenPosition(0, 0);
|
|
|
|
m_initialized = true;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
Hypothesis *BackwardsEdge::CreateHypothesis(const Hypothesis &hypothesis, const TranslationOption &transOpt)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// create hypothesis and calculate all its scores
|
2014-01-03 22:45:31 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
hypothesis.GetManager().GetSentenceStats().StartTimeBuildHyp();
|
|
|
|
}
|
2013-09-17 17:06:17 +04:00
|
|
|
Hypothesis *newHypo = hypothesis.CreateNext(transOpt); // TODO FIXME This is absolutely broken - don't pass null here
|
2014-01-03 22:45:31 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
hypothesis.GetManager().GetSentenceStats().StopTimeBuildHyp();
|
|
|
|
}
|
2015-10-05 19:25:17 +03:00
|
|
|
//newHypo->EvaluateWhenApplied(m_futurescore);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
return newHypo;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BackwardsEdge::SeenPosition(const size_t x, const size_t y)
|
|
|
|
{
|
2014-10-21 13:44:31 +04:00
|
|
|
boost::unordered_set< int >::iterator iter = m_seenPosition.find((x<<16) + y);
|
2011-02-24 16:14:42 +03:00
|
|
|
return (iter != m_seenPosition.end());
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BackwardsEdge::SetSeenPosition(const size_t x, const size_t y)
|
|
|
|
{
|
2013-11-21 18:55:41 +04:00
|
|
|
UTIL_THROW_IF2(x >= (1<<17), "Error");
|
|
|
|
UTIL_THROW_IF2(y >= (1<<17), "Error");
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
m_seenPosition.insert((x<<16) + y);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BackwardsEdge::GetInitialized()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_initialized;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const BitmapContainer&
|
|
|
|
BackwardsEdge::GetBitmapContainer() const
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_prevBitmapContainer;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BackwardsEdge::PushSuccessors(const size_t x, const size_t y)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
Hypothesis *newHypo;
|
|
|
|
|
|
|
|
if(y + 1 < m_translations.size() && !SeenPosition(x, y + 1)) {
|
|
|
|
SetSeenPosition(x, y + 1);
|
|
|
|
newHypo = CreateHypothesis(*m_hypotheses[x], *m_translations.Get(y + 1));
|
|
|
|
if(newHypo != NULL) {
|
|
|
|
m_parent.Enqueue(x, y + 1, newHypo, (BackwardsEdge*)this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(x + 1 < m_hypotheses.size() && !SeenPosition(x + 1, y)) {
|
|
|
|
SetSeenPosition(x + 1, y);
|
|
|
|
newHypo = CreateHypothesis(*m_hypotheses[x + 1], *m_translations.Get(y));
|
|
|
|
if(newHypo != NULL) {
|
|
|
|
m_parent.Enqueue(x + 1, y, newHypo, (BackwardsEdge*)this);
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BitmapContainer Code
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
BitmapContainer::BitmapContainer(const WordsBitmap &bitmap
|
2015-09-25 12:03:23 +03:00
|
|
|
, HypothesisStackCubePruning &stack
|
|
|
|
, bool deterministic)
|
2008-06-11 14:52:57 +04:00
|
|
|
: m_bitmap(bitmap)
|
|
|
|
, m_stack(stack)
|
2015-09-25 12:03:23 +03:00
|
|
|
, m_deterministic(deterministic)
|
2011-02-24 16:14:42 +03:00
|
|
|
, m_numStackInsertions(0)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
m_hypotheses = HypothesisSet();
|
|
|
|
m_edges = BackwardsEdgeSet();
|
|
|
|
m_queue = HypothesisQueue();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BitmapContainer::~BitmapContainer()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// As we have created the square position objects we clean up now.
|
|
|
|
|
|
|
|
while (!m_queue.empty()) {
|
2013-05-29 21:16:15 +04:00
|
|
|
HypothesisQueueItem *item = m_queue.top();
|
|
|
|
m_queue.pop();
|
2012-07-17 14:01:07 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
FREEHYPO( item->GetHypothesis() );
|
|
|
|
delete item;
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all edges.
|
|
|
|
RemoveAllInColl(m_edges);
|
|
|
|
|
|
|
|
m_hypotheses.clear();
|
|
|
|
m_edges.clear();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::Enqueue(int hypothesis_pos
|
2011-02-24 16:14:42 +03:00
|
|
|
, int translation_pos
|
|
|
|
, Hypothesis *hypothesis
|
|
|
|
, BackwardsEdge *edge)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2015-09-25 12:03:23 +03:00
|
|
|
// Only supply target phrase if running deterministic search mode
|
|
|
|
const TargetPhrase *target_phrase = m_deterministic ? &(hypothesis->GetCurrTargetPhrase()) : NULL;
|
2011-02-24 16:14:42 +03:00
|
|
|
HypothesisQueueItem *item = new HypothesisQueueItem(hypothesis_pos
|
|
|
|
, translation_pos
|
|
|
|
, hypothesis
|
2015-09-25 12:03:23 +03:00
|
|
|
, edge
|
|
|
|
, target_phrase);
|
2014-01-03 22:45:31 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
item->GetHypothesis()->GetManager().GetSentenceStats().StartTimeManageCubes();
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
m_queue.push(item);
|
2014-01-03 22:45:31 +04:00
|
|
|
IFVERBOSE(2) {
|
|
|
|
item->GetHypothesis()->GetManager().GetSentenceStats().StopTimeManageCubes();
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HypothesisQueueItem*
|
|
|
|
BitmapContainer::Dequeue(bool keepValue)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
if (!m_queue.empty()) {
|
|
|
|
HypothesisQueueItem *item = m_queue.top();
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
if (!keepValue) {
|
|
|
|
m_queue.pop();
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
return item;
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
return NULL;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HypothesisQueueItem*
|
|
|
|
BitmapContainer::Top() const
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_queue.top();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
BitmapContainer::Size()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_queue.size();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BitmapContainer::Empty() const
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_queue.empty();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const WordsBitmap&
|
|
|
|
BitmapContainer::GetWordsBitmap()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_bitmap;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const HypothesisSet&
|
|
|
|
BitmapContainer::GetHypotheses() const
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_hypotheses;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
BitmapContainer::GetHypothesesSize() const
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_hypotheses.size();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const BackwardsEdgeSet&
|
|
|
|
BitmapContainer::GetBackwardsEdges()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return m_edges;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::AddHypothesis(Hypothesis *hypothesis)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
bool itemExists = false;
|
|
|
|
HypothesisSet::const_iterator iter = m_hypotheses.begin();
|
|
|
|
HypothesisSet::const_iterator iterEnd = m_hypotheses.end();
|
|
|
|
|
|
|
|
// cfedermann: do we actually need this check?
|
|
|
|
while (iter != iterEnd) {
|
|
|
|
if (*iter == hypothesis) {
|
|
|
|
itemExists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
2013-11-21 18:55:41 +04:00
|
|
|
UTIL_THROW_IF2(itemExists, "Duplicate hypotheses");
|
2011-02-24 16:14:42 +03:00
|
|
|
m_hypotheses.push_back(hypothesis);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::AddBackwardsEdge(BackwardsEdge *edge)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
m_edges.insert(edge);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::InitializeEdges()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
BackwardsEdgeSet::iterator iter = m_edges.begin();
|
|
|
|
BackwardsEdgeSet::iterator iterEnd = m_edges.end();
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
while (iter != iterEnd) {
|
|
|
|
BackwardsEdge *edge = *iter;
|
|
|
|
edge->Initialize();
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
++iter;
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::EnsureMinStackHyps(const size_t minNumHyps)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
while ((!Empty()) && m_numStackInsertions < minNumHyps) {
|
|
|
|
ProcessBestHypothesis();
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::ProcessBestHypothesis()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
if (m_queue.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the currently best hypothesis from the queue.
|
|
|
|
HypothesisQueueItem *item = Dequeue();
|
|
|
|
|
|
|
|
// If the priority queue is exhausted, we are done and should have exited
|
2013-11-21 18:55:41 +04:00
|
|
|
UTIL_THROW_IF2(item == NULL, "Null object");
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
// check we are pulling things off of priority queue in right order
|
|
|
|
if (!Empty()) {
|
|
|
|
HypothesisQueueItem *check = Dequeue(true);
|
2013-11-21 18:55:41 +04:00
|
|
|
UTIL_THROW_IF2(item->GetHypothesis()->GetTotalScore() < check->GetHypothesis()->GetTotalScore(),
|
2015-01-14 14:07:42 +03:00
|
|
|
"Non-monotonic total score: "
|
|
|
|
<< item->GetHypothesis()->GetTotalScore() << " vs. "
|
|
|
|
<< check->GetHypothesis()->GetTotalScore());
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logging for the criminally insane
|
|
|
|
IFVERBOSE(3) {
|
|
|
|
item->GetHypothesis()->PrintHypothesis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add best hypothesis to hypothesis stack.
|
|
|
|
const bool newstackentry = m_stack.AddPrune(item->GetHypothesis());
|
|
|
|
if (newstackentry)
|
|
|
|
m_numStackInsertions++;
|
|
|
|
|
|
|
|
IFVERBOSE(3) {
|
|
|
|
TRACE_ERR("new stack entry flag is " << newstackentry << std::endl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new hypotheses for the two successors of the hypothesis just added.
|
|
|
|
item->GetBackwardsEdge()->PushSuccessors(item->GetHypothesisPos(), item->GetTranslationPos());
|
|
|
|
|
|
|
|
// We are done with the queue item, we delete it.
|
|
|
|
delete item;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapContainer::SortHypotheses()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
std::sort(m_hypotheses.begin(), m_hypotheses.end(), HypothesisScoreOrderer());
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
2008-10-09 03:51:26 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|