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-06-02 01:44:46 +03:00
|
|
|
void 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-08-18 21:03:43 +03:00
|
|
|
HypothesisColl &coll = GetColl(lhs);
|
2016-12-05 21:04:26 +03:00
|
|
|
coll.Add(m_mgr, hypo, hypoRecycle, arcLists);
|
2016-04-17 11:47:04 +03:00
|
|
|
}
|
|
|
|
|
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-08-22 15:03:45 +03:00
|
|
|
const Moses2::HypothesisColl *Stack::GetColl(const SCFG::Word &nt) const
|
2016-05-01 11:32:49 +03:00
|
|
|
{
|
|
|
|
assert(nt.isNonTerminal);
|
|
|
|
Coll::const_iterator iter = m_coll.find(nt);
|
|
|
|
if (iter != m_coll.end()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:03:43 +03:00
|
|
|
Moses2::HypothesisColl &Stack::GetColl(const SCFG::Word &nt)
|
|
|
|
{
|
|
|
|
Moses2::HypothesisColl *ret;
|
|
|
|
Coll::iterator iter;
|
|
|
|
iter = m_coll.find(nt);
|
|
|
|
if (iter == m_coll.end()) {
|
|
|
|
ret = new Moses2::HypothesisColl(m_mgr);
|
|
|
|
m_coll[nt] = ret;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = iter->second;
|
|
|
|
}
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
2016-08-06 10:16:31 +03:00
|
|
|
const Hypothesis *Stack::GetBestHypo() const
|
2016-05-06 17:41:50 +03:00
|
|
|
{
|
2016-08-06 10:16:31 +03:00
|
|
|
SCORE bestScore = -std::numeric_limits<SCORE>::infinity();
|
2016-12-05 16:29:52 +03:00
|
|
|
const HypothesisBase *bestHypo = NULL;
|
2016-08-06 10:16:31 +03:00
|
|
|
BOOST_FOREACH(const Coll::value_type &val, m_coll){
|
|
|
|
const Moses2::HypothesisColl &hypos = *val.second;
|
|
|
|
const Moses2::HypothesisBase *hypo = hypos.GetBestHypo();
|
|
|
|
|
|
|
|
if (hypo->GetFutureScore() > bestScore) {
|
|
|
|
bestScore = hypo->GetFutureScore();
|
|
|
|
bestHypo = hypo;
|
2016-05-06 17:41:50 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-06 10:16:31 +03:00
|
|
|
return &bestHypo->Cast<SCFG::Hypothesis>();
|
2016-05-06 17:41:50 +03:00
|
|
|
}
|
|
|
|
|
2016-06-20 16:59:31 +03:00
|
|
|
std::string Stack::Debug(const System &system) const
|
2016-06-03 21:57:51 +03:00
|
|
|
{
|
2016-06-20 16:59:31 +03:00
|
|
|
stringstream out;
|
2016-06-11 00:03:11 +03:00
|
|
|
BOOST_FOREACH (const SCFG::Stack::Coll::value_type &valPair, m_coll) {
|
2016-06-03 21:57:51 +03:00
|
|
|
const SCFG::Word &lhs = valPair.first;
|
|
|
|
const Moses2::HypothesisColl &hypos = *valPair.second;
|
2016-06-20 16:59:31 +03:00
|
|
|
out << "lhs=" << lhs.Debug(system);
|
2016-06-14 19:14:48 +03:00
|
|
|
out << "=" << hypos.GetSize() << endl;
|
2016-06-20 16:59:31 +03:00
|
|
|
out << hypos.Debug(system);
|
2016-06-14 19:14:48 +03:00
|
|
|
out << endl;
|
2016-06-03 21:57:51 +03:00
|
|
|
}
|
2016-06-18 00:54:32 +03:00
|
|
|
|
2016-06-20 16:59:31 +03:00
|
|
|
return out.str();
|
2016-06-03 21:57:51 +03:00
|
|
|
}
|
|
|
|
|
2016-02-29 18:51:17 +03:00
|
|
|
}
|
|
|
|
}
|