00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #include <map>
00023 #include <set>
00024 #include <string>
00025 #include <vector>
00026
00027 #include <boost/unordered_map.hpp>
00028
00029 #include "moses/AlignmentInfo.h"
00030
00031 #include "Alignment.h"
00032
00033 namespace MosesTraining
00034 {
00035
00036
00037 typedef std::map <int, std::set<int> > HSentenceVertices;
00038
00039
00040 class PhraseOrientation
00041 {
00042 public:
00043
00044 enum REO_MODEL_TYPE {REO_MODEL_TYPE_MSD, REO_MODEL_TYPE_MSLR, REO_MODEL_TYPE_MONO};
00045 enum REO_CLASS {REO_CLASS_LEFT, REO_CLASS_RIGHT, REO_CLASS_DLEFT, REO_CLASS_DRIGHT, REO_CLASS_UNKNOWN};
00046 enum REO_DIR {REO_DIR_L2R, REO_DIR_R2L, REO_DIR_BIDIR};
00047
00048 PhraseOrientation() {};
00049
00050 PhraseOrientation(int sourceSize,
00051 int targetSize,
00052 const Alignment &alignment);
00053
00054 PhraseOrientation(int sourceSize,
00055 int targetSize,
00056 const Moses::AlignmentInfo &alignTerm,
00057 const Moses::AlignmentInfo &alignNonTerm);
00058
00059 PhraseOrientation(int sourceSize,
00060 int targetSize,
00061 const std::vector<std::vector<int> > &alignedToT,
00062 const std::vector<std::vector<int> > &alignedToS,
00063 const std::vector<int> &alignedCountS);
00064
00065 REO_CLASS GetOrientationInfo(int startF, int endF, REO_DIR direction) const;
00066 REO_CLASS GetOrientationInfo(int startF, int startE, int endF, int endE, REO_DIR direction) const;
00067 const std::string GetOrientationInfoString(int startF, int endF, REO_DIR direction=REO_DIR_BIDIR) const;
00068 const std::string GetOrientationInfoString(int startF, int startE, int endF, int endE, REO_DIR direction=REO_DIR_BIDIR) const;
00069 static const std::string GetOrientationString(const REO_CLASS orient, const REO_MODEL_TYPE modelType=REO_MODEL_TYPE_MSLR);
00070 static void WriteOrientation(std::ostream& out, const REO_CLASS orient, const REO_MODEL_TYPE modelType=REO_MODEL_TYPE_MSLR);
00071 void IncrementPriorCount(REO_DIR direction, REO_CLASS orient, float increment);
00072 static void WritePriorCounts(std::ostream& out, const REO_MODEL_TYPE modelType=REO_MODEL_TYPE_MSLR);
00073 bool SourceSpanIsAligned(int index1, int index2) const;
00074 bool TargetSpanIsAligned(int index1, int index2) const;
00075
00076 private:
00077
00078 void Init(int sourceSize, int targetSize,
00079 const std::vector<std::vector<int> > &alignedToT,
00080 const std::vector<std::vector<int> > &alignedToS,
00081 const std::vector<int> &alignedCountS);
00082
00083 void InsertVertex( HSentenceVertices & corners, int x, int y );
00084
00085 void InsertPhraseVertices(HSentenceVertices & topLeft,
00086 HSentenceVertices & topRight,
00087 HSentenceVertices & bottomLeft,
00088 HSentenceVertices & bottomRight,
00089 int startF, int startE, int endF, int endE);
00090
00091 REO_CLASS GetOrientHierModel(REO_MODEL_TYPE modelType,
00092 int startF, int endF, int startE, int endE, int countF, int zeroF, int zeroE, int unit,
00093 bool (*ge)(int, int), bool (*lt)(int, int),
00094 const HSentenceVertices & bottomRight, const HSentenceVertices & bottomLeft) const;
00095
00096 bool SpanIsAligned(int index1, int index2, const boost::unordered_map< std::pair<int,int> , std::pair<int,int> > &minAndMaxAligned) const;
00097
00098 bool IsAligned(int fi, int ei) const;
00099
00100 static bool ge(int first, int second) {
00101 return first >= second;
00102 };
00103 static bool le(int first, int second) {
00104 return first <= second;
00105 };
00106 static bool lt(int first, int second) {
00107 return first < second;
00108 };
00109
00110 int m_countF;
00111 int m_countE;
00112
00113 std::vector<std::vector<int> > m_alignedToT;
00114
00115 HSentenceVertices m_topLeft;
00116 HSentenceVertices m_topRight;
00117 HSentenceVertices m_bottomLeft;
00118 HSentenceVertices m_bottomRight;
00119
00120 boost::unordered_map< std::pair<int,int> , std::pair<int,int> > m_minAndMaxAlignedToSourceSpan;
00121 boost::unordered_map< std::pair<int,int> , std::pair<int,int> > m_minAndMaxAlignedToTargetSpan;
00122
00123 static std::vector<float> m_l2rOrientationPriorCounts;
00124 static std::vector<float> m_r2lOrientationPriorCounts;
00125 };
00126
00127 }