2016-04-17 21:33:21 +03:00
|
|
|
#include <boost/foreach.hpp>
|
2016-02-29 18:51:17 +03:00
|
|
|
#include "Stacks.h"
|
2016-04-17 11:47:04 +03:00
|
|
|
#include "Hypothesis.h"
|
|
|
|
#include "TargetPhraseImpl.h"
|
|
|
|
#include "Manager.h"
|
2016-02-29 18:51:17 +03:00
|
|
|
|
2016-04-21 16:19:31 +03:00
|
|
|
using namespace std;
|
|
|
|
|
2016-02-29 18:51:17 +03:00
|
|
|
namespace Moses2
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace SCFG
|
|
|
|
{
|
|
|
|
|
2016-04-17 11:47:04 +03:00
|
|
|
Stack::Stack(const Manager &mgr)
|
|
|
|
:m_mgr(mgr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-25 23:02:34 +03:00
|
|
|
Stack::~Stack()
|
|
|
|
{
|
|
|
|
BOOST_FOREACH (const Coll::value_type &valPair, m_coll) {
|
|
|
|
Moses2::HypothesisColl *hypos = valPair.second;
|
|
|
|
delete hypos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 07:55:13 +03:00
|
|
|
StackAdd Stack::Add(SCFG::Hypothesis *hypo, Recycler<HypothesisBase*> &hypoRecycle,
|
2016-04-17 11:47:04 +03:00
|
|
|
ArcLists &arcLists)
|
|
|
|
{
|
|
|
|
const SCFG::TargetPhraseImpl &tp = hypo->GetTargetPhrase();
|
|
|
|
const SCFG::Word &lhs = tp.lhs;
|
2016-04-28 17:19:53 +03:00
|
|
|
//cerr << "lhs=" << lhs << endl;
|
2016-04-21 16:19:31 +03:00
|
|
|
|
2016-04-17 11:47:04 +03:00
|
|
|
HypothesisColl &coll = GetMiniStack(lhs);
|
|
|
|
StackAdd added = coll.Add(hypo);
|
2016-04-18 07:55:13 +03:00
|
|
|
return added;
|
2016-04-17 11:47:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Moses2::HypothesisColl &Stack::GetMiniStack(const SCFG::Word &key)
|
|
|
|
{
|
|
|
|
Moses2::HypothesisColl *ret;
|
|
|
|
Coll::iterator iter;
|
|
|
|
iter = m_coll.find(key);
|
|
|
|
if (iter == m_coll.end()) {
|
|
|
|
ret = new Moses2::HypothesisColl(m_mgr);
|
|
|
|
m_coll[key] = ret;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = iter->second;
|
|
|
|
}
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
2016-04-17 21:33:21 +03:00
|
|
|
size_t Stack::GetSize() const
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
BOOST_FOREACH (const Coll::value_type &valPair, m_coll) {
|
|
|
|
Moses2::HypothesisColl &hypos = *valPair.second;
|
|
|
|
ret += hypos.GetSize();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-01 11:32:49 +03:00
|
|
|
const Moses2::HypothesisColl *Stack::GetColl(SCFG::Word &nt) const
|
|
|
|
{
|
|
|
|
assert(nt.isNonTerminal);
|
|
|
|
Coll::const_iterator iter = m_coll.find(nt);
|
|
|
|
if (iter != m_coll.end()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 17:41:50 +03:00
|
|
|
const Hypothesis *Stack::GetBestHypo(
|
|
|
|
const Manager &mgr,
|
|
|
|
ArcLists &arcLists) const
|
|
|
|
{
|
|
|
|
const Hypothesis *ret = NULL;
|
|
|
|
BOOST_FOREACH (const Coll::value_type &valPair, m_coll) {
|
|
|
|
Moses2::HypothesisColl &hypos = *valPair.second;
|
|
|
|
const Hypotheses &sortedHypos = hypos.GetSortedAndPruneHypos(mgr, arcLists);
|
|
|
|
const Hypothesis *bestHypoColl = static_cast<const Hypothesis*>(sortedHypos[0]);
|
|
|
|
|
|
|
|
if (ret == NULL || ret->GetFutureScore() < bestHypoColl->GetFutureScore()) {
|
|
|
|
ret = bestHypoColl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:51:17 +03:00
|
|
|
}
|
|
|
|
}
|