#include "LRModel.h" #include "moses/Range.h" #include "moses/Bitmap.h" #include "moses/InputType.h" #include "HReorderingForwardState.h" #include "HReorderingBackwardState.h" #include "PhraseBasedReorderingState.h" #include "BidirectionalReorderingState.h" #include "SparseReordering.h" namespace Moses { 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))); } 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))); } size_t LRModel:: GetNumberOfTypes() const { return ((m_modelType == MSD) ? 3 : (m_modelType == MSLR) ? 4 : 2); } size_t LRModel:: GetNumScoreComponents() const { size_t score_per_dir = m_collapseScores ? 1 : GetNumberOfTypes(); return ((m_direction == Bidirectional) ? 2 * score_per_dir + m_additionalScoreComponents : score_per_dir + m_additionalScoreComponents); } void LRModel:: ConfigureSparse(const std::map& sparseArgs, const LexicalReordering* producer) { if (sparseArgs.size()) { m_sparse.reset(new SparseReordering(sparseArgs, producer)); } } void LRModel:: SetAdditionalScoreComponents(size_t number) { m_additionalScoreComponents = number; } /// 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); } 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(int const reoDistance) const { // this one is for HierarchicalReorderingBackwardState return ((m_modelType == LeftRight) ? (reoDistance >= 1) ? R : L : (reoDistance == 1) ? M : (m_modelType == Monotonic) ? NM : (reoDistance == -1) ? S : (m_modelType == MSD) ? D : (reoDistance > 1) ? 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); } LRModel:: LRModel(const std::string &modelType) : m_modelString(modelType) , m_scoreProducer(NULL) , m_modelType(None) , m_phraseBased(true) , m_collapseScores(false) , m_direction(Backward) , m_additionalScoreComponents(0) { std::vector config = Tokenize(modelType, "-"); for (size_t i=0; i