00001
00002
00003 #include "util/check.hh"
00004 #include "moses/FF/FFState.h"
00005 #include "moses/FF/DummyScoreProducers.h"
00006 #include "moses/StaticData.h"
00007 #include "moses/WordsRange.h"
00008 #include "moses/TranslationOption.h"
00009
00010 namespace Moses
00011 {
00012
00013 struct DistortionState_traditional : public FFState {
00014 WordsRange range;
00015 int first_gap;
00016 DistortionState_traditional(const WordsRange& wr, int fg) : range(wr), first_gap(fg) {}
00017 int Compare(const FFState& other) const {
00018 const DistortionState_traditional& o =
00019 static_cast<const DistortionState_traditional&>(other);
00020 if (range.GetEndPos() < o.range.GetEndPos()) return -1;
00021 if (range.GetEndPos() > o.range.GetEndPos()) return 1;
00022 return 0;
00023 }
00024 };
00025
00026 const FFState* DistortionScoreProducer::EmptyHypothesisState(const InputType &input) const
00027 {
00028
00029 size_t start = NOT_FOUND;
00030 size_t end = NOT_FOUND;
00031 if (input.m_frontSpanCoveredLength > 0) {
00032
00033 start = 0;
00034 end = input.m_frontSpanCoveredLength -1;
00035 }
00036 return new DistortionState_traditional(
00037 WordsRange(start, end),
00038 NOT_FOUND);
00039 }
00040
00041 float DistortionScoreProducer::CalculateDistortionScore(const Hypothesis& hypo,
00042 const WordsRange &prev, const WordsRange &curr, const int FirstGap)
00043 {
00044 if(!StaticData::Instance().UseEarlyDistortionCost()) {
00045 return - (float) hypo.GetInput().ComputeDistortionDistance(prev, curr);
00046 }
00047 else {
00048
00049
00050
00051
00052
00053
00054
00055 int prefixEndPos = (int)FirstGap-1;
00056 if((int)FirstGap==-1)
00057 prefixEndPos = -1;
00058
00059
00060 if ((int) curr.GetStartPos() == prefixEndPos+1) {
00061 IFVERBOSE(4) std::cerr<< "MQ07disto:case1" << std::endl;
00062 return 0;
00063 }
00064
00065
00066 if ((int) curr.GetEndPos() < (int) prev.GetEndPos()) {
00067 IFVERBOSE(4) std::cerr<< "MQ07disto:case2" << std::endl;
00068 return (float) -2*(int)curr.GetNumWordsCovered();
00069 }
00070
00071
00072 if ((int) prev.GetEndPos() <= prefixEndPos) {
00073 IFVERBOSE(4) std::cerr<< "MQ07disto:case3" << std::endl;
00074 int z = (int)curr.GetStartPos()-prefixEndPos - 1;
00075 return (float) -2*(z + (int)curr.GetNumWordsCovered());
00076 }
00077
00078
00079 IFVERBOSE(4) std::cerr<< "MQ07disto:case4" << std::endl;
00080 return (float) -2*((int)curr.GetNumWordsBetween(prev) + (int)curr.GetNumWordsCovered());
00081
00082 }
00083 }
00084
00085
00086 FFState* DistortionScoreProducer::Evaluate(
00087 const Hypothesis& hypo,
00088 const FFState* prev_state,
00089 ScoreComponentCollection* out) const
00090 {
00091 const DistortionState_traditional* prev = static_cast<const DistortionState_traditional*>(prev_state);
00092 const float distortionScore = CalculateDistortionScore(
00093 hypo,
00094 prev->range,
00095 hypo.GetCurrSourceWordsRange(),
00096 prev->first_gap);
00097 out->PlusEquals(this, distortionScore);
00098 DistortionState_traditional* res = new DistortionState_traditional(
00099 hypo.GetCurrSourceWordsRange(),
00100 hypo.GetWordsBitmap().GetFirstGapPos());
00101 return res;
00102 }
00103
00104 void WordPenaltyProducer::Evaluate(const TargetPhrase &targetPhrase
00105 , ScoreComponentCollection &scoreBreakdown
00106 , ScoreComponentCollection &estimatedFutureScore) const
00107 {
00108 float score = - (float) targetPhrase.GetNumTerminals();
00109 scoreBreakdown.Assign(this, score);
00110 }
00111
00112 }