2008-06-11 14:52:57 +04:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
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 "PartialTranslOptColl.h"
|
|
|
|
#include <algorithm>
|
2013-05-08 14:28:31 +04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
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
|
|
|
/** constructor, intializes counters and thresholds */
|
|
|
|
PartialTranslOptColl::PartialTranslOptColl()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
m_bestScore = -std::numeric_limits<float>::infinity();
|
|
|
|
m_worstScore = -std::numeric_limits<float>::infinity();
|
|
|
|
m_maxSize = StaticData::Instance().GetMaxNoPartTransOpt();
|
|
|
|
m_totalPruned = 0;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** add a partial translation option to the collection (without pruning) */
|
2013-05-11 17:13:26 +04:00
|
|
|
void PartialTranslOptColl::AddNoPrune(TranslationOption *partialTranslOpt)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
if (partialTranslOpt->GetFutureScore() >= m_worstScore) {
|
|
|
|
m_list.push_back(partialTranslOpt);
|
|
|
|
if (partialTranslOpt->GetFutureScore() > m_bestScore)
|
|
|
|
m_bestScore = partialTranslOpt->GetFutureScore();
|
|
|
|
} else {
|
|
|
|
m_totalPruned++;
|
|
|
|
delete partialTranslOpt;
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** add a partial translation option to the collection, prune if necessary.
|
2011-02-24 16:14:42 +03:00
|
|
|
* This is done similar to the Prune() in TranslationOptionCollection */
|
2008-06-11 14:52:57 +04:00
|
|
|
|
2013-05-11 17:13:26 +04:00
|
|
|
void PartialTranslOptColl::Add(TranslationOption *partialTranslOpt)
|
2008-06-11 14:52:57 +04:00
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// add
|
2013-05-11 17:13:26 +04:00
|
|
|
AddNoPrune(partialTranslOpt );
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
// done if not too large (lazy pruning, only if twice as large as max)
|
|
|
|
if ( m_list.size() > 2 * m_maxSize ) {
|
|
|
|
Prune();
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** helper, used by pruning */
|
|
|
|
bool ComparePartialTranslationOption(const TranslationOption *a, const TranslationOption *b)
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
return a->GetFutureScore() > b->GetFutureScore();
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** pruning, remove partial translation options, if list too big */
|
|
|
|
void PartialTranslOptColl::Prune()
|
|
|
|
{
|
2011-02-24 16:14:42 +03:00
|
|
|
// done if not too big
|
|
|
|
if ( m_list.size() <= m_maxSize ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TRACE_ERR( "pruning partial translation options from size " << m_list.size() << std::endl);
|
|
|
|
|
|
|
|
// find nth element
|
2013-11-15 14:55:38 +04:00
|
|
|
NTH_ELEMENT4(m_list.begin(),
|
2014-01-15 19:42:02 +04:00
|
|
|
m_list.begin() + m_maxSize,
|
|
|
|
m_list.end(),
|
|
|
|
ComparePartialTranslationOption);
|
2011-02-24 16:14:42 +03:00
|
|
|
|
|
|
|
m_worstScore = m_list[ m_maxSize-1 ]->GetFutureScore();
|
|
|
|
// delete the rest
|
|
|
|
for (size_t i = m_maxSize ; i < m_list.size() ; ++i) {
|
|
|
|
delete m_list[i];
|
|
|
|
m_totalPruned++;
|
|
|
|
}
|
|
|
|
m_list.resize(m_maxSize);
|
|
|
|
// TRACE_ERR( "pruned to size " << m_list.size() << ", total pruned: " << m_totalPruned << std::endl);
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 14:28:31 +04:00
|
|
|
// friend
|
|
|
|
ostream& operator<<(ostream& out, const PartialTranslOptColl& possibleTranslation)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < possibleTranslation.m_list.size(); ++i) {
|
|
|
|
const TranslationOption &transOpt = *possibleTranslation.m_list[i];
|
|
|
|
out << transOpt << endl;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2008-10-09 03:51:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|