// Method to reduce, if possible the spans void ReorderingStack::Reduce(Range current) { bool cont_loop = true; while (cont_loop && m_stack.size() > 0) { Range previous = m_stack.back(); if (current.GetStartPos() - previous.GetEndPos() == 1) { //mono&merge m_stack.pop_back(); Range t(previous.GetStartPos(), current.GetEndPos()); current = t; } else if (previous.GetStartPos() - current.GetEndPos() == 1) { //swap&merge m_stack.pop_back(); Range t(current.GetStartPos(), previous.GetEndPos()); current = t; } else { // discontinuous, no more merging cont_loop = false; } } // finished reducing, exit // add to stack m_stack.push_back(current); }
// Method to push (shift element into the stack and reduce if reqd) int ReorderingStack::ShiftReduce(Range input_span) { int distance; // value to return: the initial distance between this and previous span // stack is empty if(m_stack.empty()) { m_stack.push_back(input_span); return input_span.GetStartPos() + 1; // - (-1) } // stack is non-empty Range prev_span = m_stack.back(); //access last element added //calculate the distance we are returning if(input_span.GetStartPos() > prev_span.GetStartPos()) { distance = input_span.GetStartPos() - prev_span.GetEndPos(); } else { distance = input_span.GetEndPos() - prev_span.GetStartPos(); } if(distance == 1) { //monotone m_stack.pop_back(); Range new_span(prev_span.GetStartPos(), input_span.GetEndPos()); Reduce(new_span); } else if(distance == -1) { //swap m_stack.pop_back(); Range new_span(input_span.GetStartPos(), prev_span.GetEndPos()); Reduce(new_span); } else { // discontinuous m_stack.push_back(input_span); } return distance; }
void ChartParser::Create(const Range &range, ChartParserCallback &to) { assert(m_decodeGraphList.size() == m_ruleLookupManagers.size()); std::vector <DecodeGraph*>::const_iterator iterDecodeGraph; std::vector <ChartRuleLookupManager*>::const_iterator iterRuleLookupManagers = m_ruleLookupManagers.begin(); for (iterDecodeGraph = m_decodeGraphList.begin(); iterDecodeGraph != m_decodeGraphList.end(); ++iterDecodeGraph, ++iterRuleLookupManagers) { const DecodeGraph &decodeGraph = **iterDecodeGraph; assert(decodeGraph.GetSize() == 1); ChartRuleLookupManager &ruleLookupManager = **iterRuleLookupManagers; size_t maxSpan = decodeGraph.GetMaxChartSpan(); size_t last = m_source.GetSize()-1; if (maxSpan != 0) { last = min(last, range.GetStartPos()+maxSpan); } if (maxSpan == 0 || range.GetNumWordsCovered() <= maxSpan) { const InputPath &inputPath = GetInputPath(range); ruleLookupManager.GetChartRuleCollection(inputPath, last, to); } } if (range.GetNumWordsCovered() == 1 && range.GetStartPos() != 0 && range.GetStartPos() != m_source.GetSize()-1) { bool alwaysCreateDirectTranslationOption = StaticData::Instance().IsAlwaysCreateDirectTranslationOption(); if (to.Empty() || alwaysCreateDirectTranslationOption) { // create unknown words for 1 word coverage where we don't have any trans options const Word &sourceWord = m_source.GetWord(range.GetStartPos()); m_unknown.Process(sourceWord, range, to); } } }
LRModel::ReorderingType LRModel:: GetOrientation(Range const& prev, Range const& cur) const { UTIL_THROW_IF2(m_modelType == None, "No reordering model type specified"); return ((m_modelType == LeftRight) ? prev.GetEndPos() <= cur.GetStartPos() ? R : L : (cur.GetStartPos() == prev.GetEndPos() + 1) ? M : (m_modelType == Monotonic) ? NM : (prev.GetStartPos() == cur.GetEndPos() + 1) ? S : (m_modelType == MSD) ? D : (cur.GetStartPos() > prev.GetEndPos()) ? DR : DL); }
LRModel::ReorderingType LRModel:: GetOrientation(Range const& prev, Range const& cur, Bitmap const& cov) const { return ((m_modelType == LeftRight) ? cur.GetStartPos() > prev.GetEndPos() ? R : L : IsMonotonicStep(prev,cur,cov) ? M : (m_modelType == Monotonic) ? NM : IsSwap(prev,cur,cov) ? S : (m_modelType == MSD) ? D : cur.GetStartPos() > prev.GetEndPos() ? DR : DL); }
bool IsSwap(Range const& prev, Range const& cur, Bitmap const& cov) { size_t s = prev.GetStartPos(); size_t e = cur.GetEndPos(); return (e+1 == s || (e < s && !cov.GetValue(s-1))); }
float DistortionScoreProducer:: CalculateDistortionScore(const Hypothesis& hypo, const Range &prev, const Range &curr, const int FirstGap) { // if(!StaticData::Instance().UseEarlyDistortionCost()) { if(!hypo.GetManager().options()->reordering.use_early_distortion_cost) { return - (float) hypo.GetInput().ComputeDistortionDistance(prev, curr); } // else { /* Pay distortion score as soon as possible, from Moore and Quirk MT Summit 2007 Definitions: S : current source range S' : last translated source phrase range S'' : longest fully-translated initial segment */ int prefixEndPos = (int)FirstGap-1; if((int)FirstGap==-1) prefixEndPos = -1; // case1: S is adjacent to S'' => return 0 if ((int) curr.GetStartPos() == prefixEndPos+1) { IFVERBOSE(4) std::cerr<< "MQ07disto:case1" << std::endl; return 0; } // case2: S is to the left of S' => return 2(length(S)) if ((int) curr.GetEndPos() < (int) prev.GetEndPos()) { IFVERBOSE(4) std::cerr<< "MQ07disto:case2" << std::endl; return (float) -2*(int)curr.GetNumWordsCovered(); } // case3: S' is a subsequence of S'' => return 2(nbWordBetween(S,S'')+length(S)) if ((int) prev.GetEndPos() <= prefixEndPos) { IFVERBOSE(4) std::cerr<< "MQ07disto:case3" << std::endl; int z = (int)curr.GetStartPos()-prefixEndPos - 1; return (float) -2*(z + (int)curr.GetNumWordsCovered()); } // case4: otherwise => return 2(nbWordBetween(S,S')+length(S)) IFVERBOSE(4) std::cerr<< "MQ07disto:case4" << std::endl; return (float) -2*((int)curr.GetNumWordsBetween(prev) + (int)curr.GetNumWordsCovered()); }
bool IsMonotonicStep(Range const& prev, // words range of last source phrase Range const& cur, // words range of current source phrase Bitmap const& cov) // coverage bitmap { size_t e = prev.GetEndPos() + 1; size_t s = cur.GetStartPos(); return (s == e || (s >= e && !cov.GetValue(e))); }
/// return orientation for the first phrase LRModel::ReorderingType LRModel:: GetOrientation(Range const& cur) const { UTIL_THROW_IF2(m_modelType == None, "Reordering Model Type is None"); return ((m_modelType == LeftRight) ? R : (cur.GetStartPos() == 0) ? M : (m_modelType == MSD) ? D : (m_modelType == MSLR) ? DR : NM); }
Phrase Phrase::GetSubString(const Range &range) const { Phrase retPhrase(range.GetNumWordsCovered()); for (size_t currPos = range.GetStartPos() ; currPos <= range.GetEndPos() ; currPos++) { Word &word = retPhrase.AddWord(); word = GetWord(currPos); } return retPhrase; }
Phrase Phrase::GetSubString(const Range &range, FactorType factorType) const { Phrase retPhrase(range.GetNumWordsCovered()); for (size_t currPos = range.GetStartPos() ; currPos <= range.GetEndPos() ; currPos++) { const Factor* f = GetFactor(currPos, factorType); Word &word = retPhrase.AddWord(); word.SetFactor(factorType, f); } return retPhrase; }