2015-10-24 21:54:16 +03:00
|
|
|
/*
|
|
|
|
* Hypothesis.cpp
|
|
|
|
*
|
|
|
|
* Created on: 24 Oct 2015
|
|
|
|
* Author: hieu
|
|
|
|
*/
|
|
|
|
|
2015-10-29 15:47:53 +03:00
|
|
|
#include <boost/foreach.hpp>
|
2015-10-26 22:31:39 +03:00
|
|
|
#include <stdlib.h>
|
2015-10-24 21:54:16 +03:00
|
|
|
#include "Hypothesis.h"
|
|
|
|
#include "Manager.h"
|
2015-10-26 00:20:55 +03:00
|
|
|
#include "System.h"
|
2015-10-27 00:11:47 +03:00
|
|
|
#include "Scores.h"
|
2015-10-29 15:47:53 +03:00
|
|
|
#include "StatefulFeatureFunction.h"
|
2015-10-24 21:54:16 +03:00
|
|
|
|
2015-10-29 21:15:12 +03:00
|
|
|
using namespace std;
|
|
|
|
|
2015-10-26 01:38:08 +03:00
|
|
|
Hypothesis::Hypothesis(Manager &mgr,
|
|
|
|
const TargetPhrase &tp,
|
|
|
|
const Moses::Range &range,
|
|
|
|
const Moses::Bitmap &bitmap)
|
2015-10-24 21:54:16 +03:00
|
|
|
:m_mgr(mgr)
|
2015-10-26 01:38:08 +03:00
|
|
|
,m_targetPhrase(tp)
|
2015-10-26 22:31:39 +03:00
|
|
|
,m_sourceCompleted(bitmap)
|
2015-10-25 15:49:25 +03:00
|
|
|
,m_range(range)
|
2015-10-26 01:41:06 +03:00
|
|
|
,m_prevHypo(NULL)
|
2015-10-24 21:54:16 +03:00
|
|
|
{
|
2015-10-28 19:11:12 +03:00
|
|
|
MemPool &pool = m_mgr.GetPool();
|
2015-10-27 00:11:47 +03:00
|
|
|
|
2015-10-27 19:54:15 +03:00
|
|
|
size_t numStatefulFFs = m_mgr.GetSystem().GetFeatureFunctions().GetStatefulFeatureFunctions().size();
|
2015-10-29 15:47:53 +03:00
|
|
|
m_ffStates = (const Moses::FFState **) pool.Allocate(sizeof(Moses::FFState*) * numStatefulFFs);
|
2015-10-27 00:11:47 +03:00
|
|
|
|
2015-10-27 20:00:38 +03:00
|
|
|
m_scores = new (pool.Allocate<Scores>()) Scores(pool, m_mgr.GetSystem().GetFeatureFunctions().GetNumScores());
|
2015-10-24 21:54:16 +03:00
|
|
|
}
|
|
|
|
|
2015-10-26 00:46:30 +03:00
|
|
|
Hypothesis::Hypothesis(const Hypothesis &prevHypo,
|
|
|
|
const TargetPhrase &tp,
|
|
|
|
const Moses::Range &pathRange,
|
|
|
|
const Moses::Bitmap &bitmap)
|
|
|
|
:m_mgr(prevHypo.m_mgr)
|
2015-10-26 01:38:08 +03:00
|
|
|
,m_targetPhrase(tp)
|
2015-10-26 22:31:39 +03:00
|
|
|
,m_sourceCompleted(bitmap)
|
2015-10-26 00:46:30 +03:00
|
|
|
,m_range(pathRange)
|
2015-10-26 01:41:06 +03:00
|
|
|
,m_prevHypo(&prevHypo)
|
2015-10-26 00:46:30 +03:00
|
|
|
{
|
2015-10-28 19:11:12 +03:00
|
|
|
MemPool &pool = m_mgr.GetPool();
|
2015-10-27 19:54:15 +03:00
|
|
|
size_t numStatefulFFs = m_mgr.GetSystem().GetFeatureFunctions().GetStatefulFeatureFunctions().size();
|
2015-10-29 15:47:53 +03:00
|
|
|
m_ffStates = (const Moses::FFState **) pool.Allocate(sizeof(Moses::FFState*) * numStatefulFFs);
|
2015-10-27 00:11:47 +03:00
|
|
|
|
2015-10-29 02:26:17 +03:00
|
|
|
m_scores = new (pool.Allocate<Scores>())
|
|
|
|
Scores(pool,
|
|
|
|
m_mgr.GetSystem().GetFeatureFunctions().GetNumScores(),
|
|
|
|
prevHypo.GetScores());
|
|
|
|
m_scores->PlusEquals(m_mgr.GetSystem(), m_targetPhrase.GetScores());
|
2015-10-26 00:46:30 +03:00
|
|
|
}
|
|
|
|
|
2015-10-24 21:54:16 +03:00
|
|
|
Hypothesis::~Hypothesis() {
|
|
|
|
// TODO Auto-generated destructor stub
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Hypothesis::hash() const
|
|
|
|
{
|
2015-10-27 19:54:15 +03:00
|
|
|
size_t numStatefulFFs = m_mgr.GetSystem().GetFeatureFunctions().GetStatefulFeatureFunctions().size();
|
2015-10-24 21:54:16 +03:00
|
|
|
size_t seed;
|
|
|
|
|
|
|
|
// coverage
|
2015-10-29 20:21:54 +03:00
|
|
|
seed = (size_t) &m_sourceCompleted;
|
2015-10-24 21:54:16 +03:00
|
|
|
|
|
|
|
// states
|
|
|
|
for (size_t i = 0; i < numStatefulFFs; ++i) {
|
|
|
|
const Moses::FFState *state = m_ffStates[i];
|
|
|
|
size_t hash = state->hash();
|
|
|
|
boost::hash_combine(seed, hash);
|
|
|
|
}
|
|
|
|
return seed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Hypothesis::operator==(const Hypothesis &other) const
|
|
|
|
{
|
2015-10-27 19:54:15 +03:00
|
|
|
size_t numStatefulFFs = m_mgr.GetSystem().GetFeatureFunctions().GetStatefulFeatureFunctions().size();
|
2015-10-24 21:54:16 +03:00
|
|
|
// coverage
|
2015-10-29 20:21:54 +03:00
|
|
|
if (&m_sourceCompleted != &other.m_sourceCompleted) {
|
2015-10-26 22:31:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-24 21:54:16 +03:00
|
|
|
|
|
|
|
// states
|
|
|
|
for (size_t i = 0; i < numStatefulFFs; ++i) {
|
|
|
|
const Moses::FFState &thisState = *m_ffStates[i];
|
|
|
|
const Moses::FFState &otherState = *other.m_ffStates[i];
|
|
|
|
if (thisState != otherState) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2015-10-26 19:32:47 +03:00
|
|
|
|
2015-10-27 00:11:47 +03:00
|
|
|
void Hypothesis::OutputToStream(std::ostream &out) const
|
|
|
|
{
|
|
|
|
if (m_prevHypo) {
|
|
|
|
m_prevHypo->OutputToStream(out);
|
2015-10-27 01:12:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_targetPhrase.GetSize()) {
|
|
|
|
out << (const Phrase&) m_targetPhrase;
|
2015-10-27 00:11:47 +03:00
|
|
|
out << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:32:47 +03:00
|
|
|
std::ostream& operator<<(std::ostream &out, const Hypothesis &obj)
|
|
|
|
{
|
2015-10-29 18:29:14 +03:00
|
|
|
obj.OutputToStream(out);
|
|
|
|
out << " ";
|
|
|
|
obj.GetScores().Debug(out, obj.m_mgr.GetSystem().GetFeatureFunctions());
|
2015-10-26 19:32:47 +03:00
|
|
|
return out;
|
|
|
|
}
|
2015-10-29 15:47:53 +03:00
|
|
|
|
|
|
|
void Hypothesis::EmptyHypothesisState(const Phrase &input)
|
|
|
|
{
|
|
|
|
const std::vector<const StatefulFeatureFunction*> &sfffs = m_mgr.GetSystem().GetFeatureFunctions().GetStatefulFeatureFunctions();
|
|
|
|
BOOST_FOREACH(const StatefulFeatureFunction *sfff, sfffs) {
|
|
|
|
size_t statefulInd = sfff->GetStatefulInd();
|
|
|
|
const Moses::FFState *state = sfff->EmptyHypothesisState(m_mgr, input);
|
|
|
|
m_ffStates[statefulInd] = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 16:13:01 +03:00
|
|
|
void Hypothesis::EvaluateWhenApplied()
|
|
|
|
{
|
|
|
|
const std::vector<const StatefulFeatureFunction*> &sfffs = m_mgr.GetSystem().GetFeatureFunctions().GetStatefulFeatureFunctions();
|
|
|
|
BOOST_FOREACH(const StatefulFeatureFunction *sfff, sfffs) {
|
|
|
|
size_t statefulInd = sfff->GetStatefulInd();
|
|
|
|
const Moses::FFState *prevState = m_prevHypo->GetState(statefulInd);
|
|
|
|
assert(prevState);
|
|
|
|
const Moses::FFState *state = sfff->EvaluateWhenApplied(m_mgr, *this, *prevState, *m_scores);
|
|
|
|
m_ffStates[statefulInd] = state;
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:35:06 +03:00
|
|
|
//cerr << *this << endl;
|
2015-10-29 16:13:01 +03:00
|
|
|
}
|
2015-10-29 15:47:53 +03:00
|
|
|
|