File size: 3,171 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// $Id$
// vim:tabstop=2
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#ifndef moses_ConsistentPhrases_h
#define moses_ConsistentPhrases_h

#include <set>

namespace Moses
{

class ConsistentPhrases
{
public:
  struct Phrase {
    int i, j, m, n;
    Phrase(int i_, int m_, int j_, int n_) : i(i_), j(j_), m(m_), n(n_) { }
  };

  struct PhraseSorter {
    bool operator()(Phrase a, Phrase b) {
      if(a.n > b.n)
        return true;
      if(a.n == b.n && a.j < b.j)
        return true;
      if(a.n == b.n && a.j == b.j && a.m > b.m)
        return true;
      if(a.n == b.n && a.j == b.j && a.m == b.m && a.i < b.i)
        return true;
      return false;
    }
  };

private:
  typedef std::set<Phrase, PhraseSorter> PhraseQueue;
  PhraseQueue m_phraseQueue;

  typedef std::pair<unsigned char, unsigned char> AlignPoint;
  typedef std::set<AlignPoint> Alignment;

public:

  ConsistentPhrases(int mmax, int nmax, Alignment& a) {
    for(int i = 0; i < mmax; i++) {
      for(int m = 1; m <= mmax-i; m++) {
        for(int j = 0; j < nmax; j++) {
          for(int n = 1; n <= nmax-j; n++) {
            bool consistant = true;
            for(Alignment::iterator it = a.begin(); it != a.end(); it++) {
              int ip = it->first;
              int jp = it->second;
              if((i <= ip && ip < i+m) != (j <= jp && jp < j+n)) {
                consistant = false;
                break;
              }
            }
            if(consistant)
              m_phraseQueue.insert(Phrase(i, m, j, n));
          }
        }
      }
    }
    m_phraseQueue.erase(Phrase(0, mmax, 0, nmax));
  }

  size_t Empty() {
    return !m_phraseQueue.size();
  }

  Phrase Pop() {
    if(m_phraseQueue.size()) {
      Phrase p = *m_phraseQueue.begin();
      m_phraseQueue.erase(m_phraseQueue.begin());
      return p;
    }
    return Phrase(0,0,0,0);
  }

  void RemoveOverlap(Phrase p) {
    PhraseQueue ok;
    for(PhraseQueue::iterator it = m_phraseQueue.begin(); it != m_phraseQueue.end(); it++) {
      Phrase pp = *it;
      if(!((p.i <= pp.i && pp.i < p.i + p.m) || (pp.i <= p.i && p.i < pp.i + pp.m) ||
           (p.j <= pp.j && pp.j < p.j + p.n) || (pp.j <= p.j && p.j < pp.j + pp.n)))
        ok.insert(pp);
    }
    m_phraseQueue = ok;
  }

};

}

#endif