File size: 6,271 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
/*
* Hypothesis.cpp
*
* Created on: 24 Oct 2015
* Author: hieu hoang
*/
#include <boost/foreach.hpp>
#include <stdlib.h>
#include <deque>
#include "Hypothesis.h"
#include "Manager.h"
#include "Sentence.h"
#include "TargetPhraseImpl.h"
#include "../InputPathBase.h"
#include "../System.h"
#include "../Scores.h"
#include "../Phrase.h"
#include "../FF/StatefulFeatureFunction.h"
using namespace std;
namespace Moses2
{
Hypothesis *Hypothesis::Create(MemPool &pool, Manager &mgr)
{
// ++g_numHypos;
Hypothesis *ret;
Recycler<HypothesisBase*> &recycler = mgr.GetHypoRecycle();
ret = static_cast<Hypothesis*>(recycler.Get());
if (ret) {
// got new hypo from recycler. Do nothing
} else {
ret = new (pool.Allocate<Hypothesis>()) Hypothesis(pool, mgr.system);
//cerr << "Hypothesis=" << sizeof(Hypothesis) << " " << ret << endl;
recycler.Keep(ret);
}
return ret;
}
Hypothesis::Hypothesis(MemPool &pool, const System &system) :
HypothesisBase(pool, system), m_currTargetWordsRange()
{
}
Hypothesis::~Hypothesis()
{
// TODO Auto-generated destructor stub
}
void Hypothesis::Init(Manager &mgr, const InputPathBase &path,
const TargetPhraseImpl &tp, const Bitmap &bitmap)
{
m_mgr = &mgr;
m_targetPhrase = &tp;
m_sourceCompleted = &bitmap;
m_path = &path;
m_prevHypo = NULL;
m_currTargetWordsRange.SetStartPos(NOT_FOUND);
m_currTargetWordsRange.SetEndPos(NOT_FOUND);
m_estimatedScore = 0;
m_scores->Reset(mgr.system);
}
void Hypothesis::Init(Manager &mgr, const Hypothesis &prevHypo,
const InputPathBase &path, const TargetPhraseImpl &tp, const Bitmap &bitmap,
SCORE estimatedScore)
{
m_mgr = &mgr;
m_targetPhrase = &tp;
m_sourceCompleted = &bitmap;
m_path = &path;
m_prevHypo = &prevHypo;
m_currTargetWordsRange.SetStartPos(
prevHypo.m_currTargetWordsRange.GetEndPos() + 1);
m_currTargetWordsRange.SetEndPos(
prevHypo.m_currTargetWordsRange.GetEndPos() + tp.GetSize());
m_estimatedScore = estimatedScore;
m_scores->Reset(mgr.system);
m_scores->PlusEquals(mgr.system, prevHypo.GetScores());
m_scores->PlusEquals(mgr.system, GetTargetPhrase().GetScores());
}
size_t Hypothesis::hash() const
{
// coverage
size_t seed = (size_t) m_sourceCompleted;
seed = HypothesisBase::hash(seed);
return seed;
}
bool Hypothesis::operator==(const Hypothesis &other) const
{
// coverage
if (m_sourceCompleted != other.m_sourceCompleted) {
return false;
}
bool ret = HypothesisBase::operator ==(other);
return ret;
}
std::string Hypothesis::Debug(const System &system) const
{
stringstream out;
// coverage
out << GetBitmap() << " " << GetInputPath().range << " ";
// states
const std::vector<const StatefulFeatureFunction*> &sfffs =
GetManager().system.featureFunctions.GetStatefulFeatureFunctions();
size_t numStatefulFFs = sfffs.size();
for (size_t i = 0; i < numStatefulFFs; ++i) {
const FFState &state = *GetState(i);
out << "(" << state << ") ";
}
// string
//Debug(out, m_mgr->system);
out << " ";
out << "fc=" << GetFutureScore() << " ";
out << GetScores().Debug(GetManager().system);
return out.str();
}
void Hypothesis::OutputToStream(std::ostream &out) const
{
if (m_prevHypo) {
m_prevHypo->OutputToStream(out);
}
//cerr << "range=" << GetInputPath().range << endl;
const TargetPhrase<Moses2::Word> &tp = GetTargetPhrase();
if (tp.GetSize()) {
const SubPhrase<Moses2::Word> &subPhrase = static_cast<const InputPath&>(GetInputPath()).subPhrase;
//cerr << "tp=" << tp.Debug(m_mgr->system) << endl;
//cerr << "subPhrase=" << subPhrase.Debug(m_mgr->system) << endl;
tp.OutputToStream(m_mgr->system, subPhrase, out);
}
if (m_path->range.GetStartPos() != NOT_FOUND) {
if (m_mgr->system.options.output.ReportSegmentation == 1) {
// just report phrase segmentation
out << "|" << m_path->range.GetStartPos() << "-" << m_path->range.GetEndPos() << "| ";
} else if (m_mgr->system.options.output.ReportSegmentation == 2) {
// more detailed info about every segment
out << "|";
// phrase segmentation
out << m_path->range.GetStartPos() << "-" << m_path->range.GetEndPos() << ",";
// score breakdown
m_scores->OutputBreakdown(out, m_mgr->system);
out << "| ";
}
}
}
void Hypothesis::EmptyHypothesisState(const InputType &input)
{
const std::vector<const StatefulFeatureFunction*> &sfffs =
GetManager().system.featureFunctions.GetStatefulFeatureFunctions();
BOOST_FOREACH(const StatefulFeatureFunction *sfff, sfffs) {
size_t statefulInd = sfff->GetStatefulInd();
FFState *state = m_ffStates[statefulInd];
sfff->EmptyHypothesisState(*state, GetManager(), input, *this);
}
}
void Hypothesis::EvaluateWhenApplied()
{
const std::vector<const StatefulFeatureFunction*> &sfffs =
GetManager().system.featureFunctions.GetStatefulFeatureFunctions();
BOOST_FOREACH(const StatefulFeatureFunction *sfff, sfffs) {
EvaluateWhenApplied(*sfff);
}
//cerr << *this << endl;
}
void Hypothesis::EvaluateWhenApplied(const StatefulFeatureFunction &sfff)
{
size_t statefulInd = sfff.GetStatefulInd();
const FFState *prevState = m_prevHypo->GetState(statefulInd);
FFState *thisState = m_ffStates[statefulInd];
assert(prevState);
sfff.EvaluateWhenApplied(GetManager(), *this, *prevState, *m_scores,
*thisState);
}
/** recursive - pos is relative from start of sentence */
const Word &Hypothesis::GetWord(size_t pos) const
{
const Hypothesis *hypo = this;
while (pos < hypo->GetCurrTargetWordsRange().GetStartPos()) {
hypo = hypo->GetPrevHypo();
UTIL_THROW_IF2(hypo == NULL, "Previous hypothesis should not be NULL");
}
return hypo->GetCurrWord(pos - hypo->GetCurrTargetWordsRange().GetStartPos());
}
void Hypothesis::Swap(Hypothesis &other)
{
/*
Swap(m_targetPhrase, other.m_targetPhrase);
Swap(m_sourceCompleted, other.m_sourceCompleted);
Swap(m_range, other.m_range);
Swap(m_prevHypo, other.m_prevHypo);
Swap(m_ffStates, other.m_ffStates);
Swap(m_estimatedScore, other.m_estimatedScore);
Swap(m_currTargetWordsRange, other.m_currTargetWordsRange);
*/
}
}
|