2010-04-08 21:16:10 +04:00
|
|
|
#include "TranslationOptionList.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "TranslationOption.h"
|
2015-02-06 01:44:11 +03:00
|
|
|
#include <boost/foreach.hpp>
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-06-14 01:06:34 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
|
2015-02-19 15:27:23 +03:00
|
|
|
TranslationOptionList::
|
|
|
|
TranslationOptionList(const TranslationOptionList ©)
|
|
|
|
{
|
|
|
|
const_iterator iter;
|
|
|
|
for (iter = copy.begin(); iter != copy.end(); ++iter) {
|
|
|
|
const TranslationOption &origTransOpt = **iter;
|
|
|
|
TranslationOption *newTransOpt = new TranslationOption(origTransOpt);
|
|
|
|
Add(newTransOpt);
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
2015-02-19 15:27:23 +03:00
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2015-02-19 15:27:23 +03:00
|
|
|
TranslationOptionList::
|
|
|
|
~TranslationOptionList()
|
|
|
|
{
|
|
|
|
RemoveAllInColl(m_coll);
|
|
|
|
}
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2015-02-19 15:27:23 +03:00
|
|
|
TO_STRING_BODY(TranslationOptionList);
|
2010-06-14 01:06:34 +04:00
|
|
|
|
2015-02-19 15:27:23 +03:00
|
|
|
std::ostream& operator<<(std::ostream& out, const TranslationOptionList& coll)
|
|
|
|
{
|
|
|
|
TranslationOptionList::const_iterator iter;
|
|
|
|
for (iter = coll.begin(); iter != coll.end(); ++iter) {
|
|
|
|
const TranslationOption &transOpt = **iter;
|
|
|
|
out << transOpt << endl;
|
2015-02-06 01:44:11 +03:00
|
|
|
}
|
2015-02-19 15:27:23 +03:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
TranslationOptionList::
|
|
|
|
SelectNBest(size_t const N)
|
|
|
|
{
|
|
|
|
if (N == 0 || N >= m_coll.size()) return 0;
|
|
|
|
static TranslationOption::Better cmp;
|
|
|
|
NTH_ELEMENT4(m_coll.begin(), m_coll.begin() + N, m_coll.end(), cmp);
|
|
|
|
// delete the rest
|
|
|
|
for (size_t i = N ; i < m_coll.size() ; ++i) delete m_coll[i];
|
|
|
|
size_t ret = m_coll.size() - N;
|
|
|
|
m_coll.resize(N);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
TranslationOptionList::
|
|
|
|
PruneByThreshold(float const th)
|
|
|
|
{
|
|
|
|
if (m_coll.size() <= 1) return 0;
|
|
|
|
if (th == -std::numeric_limits<float>::infinity()) return 0;
|
|
|
|
|
|
|
|
// first, find the best score
|
|
|
|
float bestScore = -std::numeric_limits<float>::infinity();
|
|
|
|
BOOST_FOREACH(TranslationOption const* t, m_coll) {
|
|
|
|
if (t->GetFutureScore() > bestScore)
|
|
|
|
bestScore = t->GetFutureScore();
|
2015-02-06 01:44:11 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 15:27:23 +03:00
|
|
|
size_t old_size = m_coll.size();
|
|
|
|
|
|
|
|
// then, remove items that are worse than best score + threshold
|
|
|
|
// why '+' th ??? Does this ever hold?
|
|
|
|
for (size_t i=0; i < m_coll.size() ; ++i) {
|
|
|
|
if (m_coll[i]->GetFutureScore() < bestScore + th) {
|
|
|
|
delete m_coll[i];
|
|
|
|
if(i + 1 < m_coll.size())
|
|
|
|
std::swap(m_coll[i],m_coll.back());
|
|
|
|
m_coll.pop_back();
|
|
|
|
}
|
2011-02-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 15:27:23 +03:00
|
|
|
m_coll.resize(m_coll.size());
|
|
|
|
return old_size - m_coll.size();
|
|
|
|
}
|
|
|
|
|
2010-04-08 21:16:10 +04:00
|
|
|
|
2010-06-14 01:06:34 +04:00
|
|
|
} // namespace
|