mosesdecoder/contrib/other-builds/moses2/SCFG/Stack.cpp

111 lines
2.4 KiB
C++
Raw Normal View History

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
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-17 11:47:04 +03:00
HypothesisColl &coll = GetMiniStack(lhs);
2016-08-05 00:41:29 +03:00
coll.Add(m_mgr.system, hypo, hypoRecycle, arcLists);
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]);
2016-05-06 17:41:50 +03:00
if (ret == NULL || ret->GetFutureScore() < bestHypoColl->GetFutureScore()) {
ret = bestHypoColl;
2016-05-06 17:41:50 +03:00
}
}
return ret;
}
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
}
}