00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_ConsistentPhrases_h
00023 #define moses_ConsistentPhrases_h
00024
00025 #include <set>
00026
00027 namespace Moses
00028 {
00029
00030 class ConsistentPhrases
00031 {
00032 public:
00033 struct Phrase {
00034 int i, j, m, n;
00035 Phrase(int i_, int m_, int j_, int n_) : i(i_), j(j_), m(m_), n(n_) { }
00036 };
00037
00038 struct PhraseSorter {
00039 bool operator()(Phrase a, Phrase b) {
00040 if(a.n > b.n)
00041 return true;
00042 if(a.n == b.n && a.j < b.j)
00043 return true;
00044 if(a.n == b.n && a.j == b.j && a.m > b.m)
00045 return true;
00046 if(a.n == b.n && a.j == b.j && a.m == b.m && a.i < b.i)
00047 return true;
00048 return false;
00049 }
00050 };
00051
00052 private:
00053 typedef std::set<Phrase, PhraseSorter> PhraseQueue;
00054 PhraseQueue m_phraseQueue;
00055
00056 typedef std::pair<unsigned char, unsigned char> AlignPoint;
00057 typedef std::set<AlignPoint> Alignment;
00058
00059 public:
00060
00061 ConsistentPhrases(int mmax, int nmax, Alignment& a) {
00062 for(int i = 0; i < mmax; i++) {
00063 for(int m = 1; m <= mmax-i; m++) {
00064 for(int j = 0; j < nmax; j++) {
00065 for(int n = 1; n <= nmax-j; n++) {
00066 bool consistant = true;
00067 for(Alignment::iterator it = a.begin(); it != a.end(); it++) {
00068 int ip = it->first;
00069 int jp = it->second;
00070 if((i <= ip && ip < i+m) != (j <= jp && jp < j+n)) {
00071 consistant = false;
00072 break;
00073 }
00074 }
00075 if(consistant)
00076 m_phraseQueue.insert(Phrase(i, m, j, n));
00077 }
00078 }
00079 }
00080 }
00081 m_phraseQueue.erase(Phrase(0, mmax, 0, nmax));
00082 }
00083
00084 size_t Empty() {
00085 return !m_phraseQueue.size();
00086 }
00087
00088 Phrase Pop() {
00089 if(m_phraseQueue.size()) {
00090 Phrase p = *m_phraseQueue.begin();
00091 m_phraseQueue.erase(m_phraseQueue.begin());
00092 return p;
00093 }
00094 return Phrase(0,0,0,0);
00095 }
00096
00097 void RemoveOverlap(Phrase p) {
00098 PhraseQueue ok;
00099 for(PhraseQueue::iterator it = m_phraseQueue.begin(); it != m_phraseQueue.end(); it++) {
00100 Phrase pp = *it;
00101 if(!((p.i <= pp.i && pp.i < p.i + p.m) || (pp.i <= p.i && p.i < pp.i + pp.m) ||
00102 (p.j <= pp.j && pp.j < p.j + p.n) || (pp.j <= p.j && p.j < pp.j + pp.n)))
00103 ok.insert(pp);
00104 }
00105 m_phraseQueue = ok;
00106 }
00107
00108 };
00109
00110 }
00111
00112 #endif