|
|
|
|
|
|
|
|
|
|
|
|
|
#include <sstream> |
|
#include "Distortion.h" |
|
#include "../PhraseBased/Hypothesis.h" |
|
#include "../PhraseBased/Manager.h" |
|
#include "../legacy/Range.h" |
|
#include "../legacy/Bitmap.h" |
|
|
|
using namespace std; |
|
|
|
namespace Moses2 |
|
{ |
|
|
|
struct DistortionState_traditional: public FFState { |
|
Range range; |
|
int first_gap; |
|
|
|
DistortionState_traditional() : |
|
range() { |
|
|
|
} |
|
|
|
void Set(const Range& wr, int fg) { |
|
range = wr; |
|
first_gap = fg; |
|
} |
|
|
|
size_t hash() const { |
|
return range.GetEndPos(); |
|
} |
|
virtual bool operator==(const FFState& other) const { |
|
const DistortionState_traditional& o = |
|
static_cast<const DistortionState_traditional&>(other); |
|
return range.GetEndPos() == o.range.GetEndPos(); |
|
} |
|
|
|
virtual std::string ToString() const { |
|
stringstream sb; |
|
sb << first_gap << " " << range; |
|
return sb.str(); |
|
} |
|
|
|
}; |
|
|
|
|
|
Distortion::Distortion(size_t startInd, const std::string &line) : |
|
StatefulFeatureFunction(startInd, line) |
|
{ |
|
ReadParameters(); |
|
} |
|
|
|
Distortion::~Distortion() |
|
{ |
|
|
|
} |
|
|
|
FFState* Distortion::BlankState(MemPool &pool, const System &sys) const |
|
{ |
|
return new (pool.Allocate<DistortionState_traditional>()) DistortionState_traditional(); |
|
} |
|
|
|
void Distortion::EmptyHypothesisState(FFState &state, const ManagerBase &mgr, |
|
const InputType &input, const Hypothesis &hypo) const |
|
{ |
|
DistortionState_traditional &stateCast = |
|
static_cast<DistortionState_traditional&>(state); |
|
|
|
|
|
size_t start = NOT_FOUND; |
|
size_t end = NOT_FOUND; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stateCast.range = Range(start, end); |
|
stateCast.first_gap = NOT_FOUND; |
|
} |
|
|
|
void Distortion::EvaluateInIsolation(MemPool &pool, const System &system, |
|
const Phrase<Moses2::Word> &source, const TargetPhraseImpl &targetPhrase, Scores &scores, |
|
SCORE &estimatedScore) const |
|
{ |
|
} |
|
|
|
void Distortion::EvaluateInIsolation(MemPool &pool, const System &system, const Phrase<SCFG::Word> &source, |
|
const TargetPhrase<SCFG::Word> &targetPhrase, Scores &scores, |
|
SCORE &estimatedScore) const |
|
{ |
|
} |
|
|
|
void Distortion::EvaluateWhenApplied(const ManagerBase &mgr, |
|
const Hypothesis &hypo, const FFState &prevState, Scores &scores, |
|
FFState &state) const |
|
{ |
|
const DistortionState_traditional &prev = |
|
static_cast<const DistortionState_traditional&>(prevState); |
|
SCORE distortionScore = CalculateDistortionScore(prev.range, |
|
hypo.GetInputPath().range, prev.first_gap); |
|
|
|
|
|
scores.PlusEquals(mgr.system, *this, distortionScore); |
|
|
|
DistortionState_traditional &stateCast = |
|
static_cast<DistortionState_traditional&>(state); |
|
stateCast.Set(hypo.GetInputPath().range, hypo.GetBitmap().GetFirstGapPos()); |
|
|
|
|
|
} |
|
|
|
SCORE Distortion::CalculateDistortionScore(const Range &prev, const Range &curr, |
|
const int FirstGap) const |
|
{ |
|
bool useEarlyDistortionCost = false; |
|
if (!useEarlyDistortionCost) { |
|
return -(SCORE) ComputeDistortionDistance(prev, curr); |
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int prefixEndPos = (int) FirstGap - 1; |
|
if ((int) FirstGap == -1) prefixEndPos = -1; |
|
|
|
|
|
if ((int) curr.GetStartPos() == prefixEndPos + 1) { |
|
|
|
return 0; |
|
} |
|
|
|
|
|
if ((int) curr.GetEndPos() < (int) prev.GetEndPos()) { |
|
|
|
return (float) -2 * (int) curr.GetNumWordsCovered(); |
|
} |
|
|
|
|
|
if ((int) prev.GetEndPos() <= prefixEndPos) { |
|
|
|
int z = (int) curr.GetStartPos() - prefixEndPos - 1; |
|
return (float) -2 * (z + (int) curr.GetNumWordsCovered()); |
|
} |
|
|
|
|
|
|
|
return (float) -2 |
|
* ((int) curr.GetNumWordsBetween(prev) + (int) curr.GetNumWordsCovered()); |
|
|
|
} |
|
} |
|
|
|
int Distortion::ComputeDistortionDistance(const Range& prev, |
|
const Range& current) const |
|
{ |
|
int dist = 0; |
|
if (prev.GetNumWordsCovered() == 0) { |
|
dist = current.GetStartPos(); |
|
} else { |
|
dist = (int) prev.GetEndPos() - (int) current.GetStartPos() + 1; |
|
} |
|
return abs(dist); |
|
} |
|
|
|
void Distortion::EvaluateWhenApplied(const SCFG::Manager &mgr, |
|
const SCFG::Hypothesis &hypo, int featureID, Scores &scores, |
|
FFState &state) const |
|
{ |
|
UTIL_THROW2("Not implemented"); |
|
} |
|
|
|
} |
|
|