| #include <boost/foreach.hpp> |
| #include <sstream> |
| #include "Hypothesis.h" |
| #include "Manager.h" |
| #include "ActiveChart.h" |
| #include "TargetPhraseImpl.h" |
| #include "Sentence.h" |
| #include "../System.h" |
| #include "../Scores.h" |
| #include "../InputPathBase.h" |
| #include "../FF/StatefulFeatureFunction.h" |
|
|
| using namespace std; |
|
|
| namespace Moses2 |
| { |
| namespace SCFG |
| { |
| Hypothesis *Hypothesis::Create(Manager &mgr) |
| { |
| |
| Hypothesis *ret; |
| MemPool &pool = mgr.GetPool(); |
|
|
| Recycler<HypothesisBase*> &recycler = mgr.GetHypoRecycler(); |
| ret = static_cast<Hypothesis*>(recycler.Get()); |
| if (ret) { |
| |
| } else { |
| ret = new (pool.Allocate<Hypothesis>()) Hypothesis(pool, mgr.system); |
| |
| } |
| return ret; |
| } |
|
|
| Hypothesis::Hypothesis(MemPool &pool, |
| const System &system) |
| :HypothesisBase(pool, system) |
| ,m_prevHypos(pool) |
| { |
|
|
| } |
|
|
| void Hypothesis::Init(SCFG::Manager &mgr, |
| const SCFG::InputPath &path, |
| const SCFG::SymbolBind &symbolBind, |
| const SCFG::TargetPhraseImpl &tp, |
| const Vector<size_t> &prevHyposIndices) |
| { |
| m_mgr = &mgr; |
| m_targetPhrase = &tp; |
| m_path = &path; |
| m_symbolBind = &symbolBind; |
|
|
| m_scores->Reset(mgr.system); |
| m_scores->PlusEquals(mgr.system, GetTargetPhrase().GetScores()); |
|
|
| |
| |
| |
| m_prevHypos.resize(symbolBind.numNT); |
|
|
| size_t currInd = 0; |
| for (size_t i = 0; i < symbolBind.coll.size(); ++i) { |
| const SymbolBindElement &ele = symbolBind.coll[i]; |
| |
|
|
| if (ele.hypos) { |
| const Hypotheses &sortedHypos = *ele.hypos; |
|
|
| size_t prevHyposInd = prevHyposIndices[currInd]; |
| assert(prevHyposInd < sortedHypos.size()); |
|
|
| const Hypothesis *prevHypo = static_cast<const SCFG::Hypothesis*>(sortedHypos[prevHyposInd]); |
| m_prevHypos[currInd] = prevHypo; |
|
|
| m_scores->PlusEquals(mgr.system, prevHypo->GetScores()); |
|
|
| ++currInd; |
| } |
| } |
| } |
|
|
| SCORE Hypothesis::GetFutureScore() const |
| { |
| return GetScores().GetTotalScore(); |
| } |
|
|
| void Hypothesis::EvaluateWhenApplied() |
| { |
| const std::vector<const StatefulFeatureFunction*> &sfffs = |
| GetManager().system.featureFunctions.GetStatefulFeatureFunctions(); |
| BOOST_FOREACH(const StatefulFeatureFunction *sfff, sfffs) { |
| EvaluateWhenApplied(*sfff); |
| } |
| |
|
|
| } |
|
|
| void Hypothesis::EvaluateWhenApplied(const StatefulFeatureFunction &sfff) |
| { |
| const SCFG::Manager &mgr = static_cast<const SCFG::Manager&>(GetManager()); |
| size_t statefulInd = sfff.GetStatefulInd(); |
| FFState *thisState = m_ffStates[statefulInd]; |
| sfff.EvaluateWhenApplied(mgr, *this, statefulInd, GetScores(), |
| *thisState); |
|
|
| } |
|
|
| void Hypothesis::OutputToStream(std::ostream &strm) const |
| { |
| const SCFG::TargetPhraseImpl &tp = GetTargetPhrase(); |
| |
|
|
| for (size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) { |
| const SCFG::Word &word = tp[targetPos]; |
| |
| if (word.isNonTerminal) { |
| |
| |
| size_t nonTermInd = tp.GetAlignNonTerm().GetNonTermIndexMap()[targetPos]; |
| const Hypothesis *prevHypo = m_prevHypos[nonTermInd]; |
| prevHypo->OutputToStream(strm); |
| } else { |
| word.OutputToStream(*m_mgr, targetPos, *this, strm); |
| strm << " "; |
| } |
|
|
| } |
| } |
|
|
| std::string Hypothesis::Debug(const System &system) const |
| { |
| stringstream out; |
| out << this << flush; |
|
|
| out << " RANGE:"; |
| out << m_path->range << " "; |
| out << m_symbolBind->Debug(system) << " "; |
|
|
| |
| out << " SCORE:" << GetScores().Debug(GetManager().system) << flush; |
|
|
| out << m_targetPhrase->Debug(GetManager().system); |
|
|
| out << "PREV:"; |
| for (size_t i = 0; i < m_prevHypos.size(); ++i) { |
| const Hypothesis *prevHypo = m_prevHypos[i]; |
| out << prevHypo << prevHypo->GetInputPath().range << "(" << prevHypo->GetFutureScore() << ") "; |
| } |
| out << endl; |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| return out.str(); |
| } |
|
|
| void Hypothesis::OutputTransOpt(std::ostream &out) const |
| { |
| out << GetInputPath().range << " " |
| << "score=" << GetScores().GetTotalScore() << " " |
| << GetTargetPhrase().Debug(m_mgr->system) << endl; |
|
|
| BOOST_FOREACH(const Hypothesis *prevHypo, m_prevHypos) { |
| prevHypo->OutputTransOpt(out); |
| } |
| } |
|
|
| } |
| } |
|
|
|
|