00001 #include "ug_lexical_reordering.h"
00002 #include "moses/Util.h"
00003 namespace sapt
00004 {
00005 using namespace std;
00006
00007 LRModel::ReorderingType po_other = LRModel::NONE;
00008
00009
00010
00011
00012 bool
00013 check(vector<ushort> const& v,
00014 size_t const LFT, size_t const RGT,
00015 ushort& L, ushort& R, size_t& count)
00016 {
00017 if (v.size() == 0) return 0;
00018 if (L > v.front() && (L=v.front()) < LFT) return false;
00019 if (R < v.back() && (R=v.back()) > RGT) return false;
00020 count += v.size();
00021 return true;
00022 }
00023
00025 int
00026 expand_block(vector<vector<ushort> > const& row2col,
00027 vector<vector<ushort> > const& col2row,
00028 size_t row, size_t col,
00029 size_t const TOP, size_t const LFT,
00030 size_t const BOT, size_t const RGT,
00031 ushort* top = NULL, ushort* lft = NULL,
00032 ushort* bot = NULL, ushort* rgt = NULL)
00033 {
00034 if (row < TOP || row > BOT || col < LFT || col > RGT) return -1;
00035 UTIL_THROW_IF2(row >= row2col.size(), "out of bounds");
00036 UTIL_THROW_IF2(col >= col2row.size(), "out of bounds");
00037
00038
00039
00040
00041
00042 ushort T, L, B, R;
00043
00044
00045 if (row2col[row].size() == 0 && col2row[col].size() == 0)
00046 {
00047 if (row == TOP) while (row < BOT && !row2col[++row].size());
00048 else if (row == BOT) while (row > TOP && !row2col[--row].size());
00049
00050 if (col == LFT) while (col < RGT && !col2row[++col].size());
00051 else if (col == RGT) while (col > RGT && !col2row[--col].size());
00052
00053 if (row2col[row].size() == 0 && col2row[col].size() == 0)
00054 return 0;
00055 }
00056 if (row2col[row].size() == 0)
00057 row = col2row[col].front();
00058 if (col2row[col].size() == 0)
00059 col = row2col[row].front();
00060
00061 if ((T = col2row[col].front()) < TOP) return -1;
00062 if ((B = col2row[col].back()) > BOT) return -1;
00063 if ((L = row2col[row].front()) < LFT) return -1;
00064 if ((R = row2col[row].back()) > RGT) return -1;
00065
00066 if (B == T && R == L) return 1;
00067
00068
00069 ushort rs = row, re = row, cs = col, ce = col;
00070 int ret = row2col[row].size();
00071 for (size_t tmp = 1; tmp; ret += tmp)
00072 {
00073 tmp = 0;;
00074 while (rs>T) if (!check(row2col[--rs],LFT,RGT,L,R,tmp)) return -1;
00075 while (re<B) if (!check(row2col[++re],LFT,RGT,L,R,tmp)) return -1;
00076 while (cs>L) if (!check(col2row[--cs],TOP,BOT,T,B,tmp)) return -1;
00077 while (ce<R) if (!check(col2row[++ce],TOP,BOT,T,B,tmp)) return -1;
00078 }
00079 if (top) *top = T;
00080 if (bot) *bot = B;
00081 if (lft) *lft = L;
00082 if (rgt) *rgt = R;
00083 return ret;
00084 }
00085
00086
00087 sapt::PhraseOrientation
00088 find_po_fwd(vector<vector<ushort> >& a1,
00089 vector<vector<ushort> >& a2,
00090 size_t s1, size_t e1,
00091 size_t s2, size_t e2)
00092 {
00093 if (e2 == a2.size())
00094 return LRModel::M;
00095 size_t y = e2, L = e2, R = a2.size()-1;
00096 size_t x = e1, T = e1, B = a1.size()-1;
00097 if (e1 < a1.size() && expand_block(a1,a2,x,y,T,L,B,R) >= 0)
00098 return LRModel::M;
00099 B = x = s1-1; T = 0;
00100 if (s1 && expand_block(a1,a2,x,y,T,L,B,R) >= 0)
00101 return LRModel::S;
00102 while (e2 < a2.size() && a2[e2].size() == 0) ++e2;
00103 if (e2 == a2.size())
00104 return LRModel::NONE;
00105 if (a2[e2].back() < s1)
00106 return LRModel::DL;
00107 if (a2[e2].front() >= e1)
00108 return LRModel::DR;
00109 return LRModel::NONE;
00110 }
00111
00112
00113
00114 PhraseOrientation
00115 find_po_bwd(vector<vector<ushort> >& a1,
00116 vector<vector<ushort> >& a2,
00117 size_t s1, size_t e1,
00118 size_t s2, size_t e2)
00119 {
00120 if (s1 == 0 && s2 == 0) return LRModel::M;
00121 if (s2 == 0) return LRModel::DR;
00122 if (s1 == 0) return LRModel::DL;
00123 size_t y = s2-1, L = 0, R = s2-1;
00124 size_t x = s1-1, T = 0, B = s1-1;
00125 if (expand_block(a1,a2,x,y,T,L,B,R) >= 0)
00126 return LRModel::M;
00127 T = x = e1; B = a1.size()-1;
00128 if (expand_block(a1,a2,x,y,T,L,B,R) >= 0)
00129 return LRModel::S;
00130 while (s2-- && a2[s2].size() == 0);
00131
00132 LRModel::ReorderingType ret;
00133 ret = (a2[s2].size() == 0 ? po_other :
00134 a2[s2].back() < s1 ? LRModel::DR :
00135 a2[s2].front() >= e1 ? LRModel::DL :
00136 po_other);
00137 #if 0
00138 cout << "s1=" << s1 << endl;
00139 cout << "s2=" << s2x << "=>" << s2 << endl;
00140 cout << "e1=" << e1 << endl;
00141 cout << "e2=" << e2 << endl;
00142 cout << "a2[s2].size()=" << a2[s2].size() << endl;
00143 cout << "a2[s2].back()=" << a2[s2].back() << endl;
00144 cout << "a2[s2].front()=" << a2[s2].front() << endl;
00145 cout << "RETURNING " << ret << endl;
00146 #endif
00147 return ret;
00148 }
00149
00150 }
00151