Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alignment.h « src « bidirectional « experimental - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17774c6506c6d4c5a28bbcbf7adcf1f0c976c23f (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*

EGYPT Toolkit for Statistical Machine Translation
Written by Yaser Al-Onaizan, Jan Curin, Michael Jahr, Kevin Knight, John Lafferty, Dan Melamed, David Purdy, Franz Och, Noah Smith, and David Yarowsky.

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

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
USA.

*/
/*--
alignment: 'checked' alignment representation with autom. calc. of fertilities
Franz Josef Och (30/07/99)
--*/
#ifndef alignment_h_fjo_defined
#define alignment_h_fjo_defined
#include "Vector.h"
#include <assert.h>
#include "defs.h"
#include "myassert.h"

class al_struct
{
 public:
  al_struct()
    : prev(0),next(0){}
  PositionIndex prev,next;
};


class alignment
{
 private:
  Vector<PositionIndex> a;
  Vector<PositionIndex> positionSum,f;
 public:
  Vector<PositionIndex> als_i;
  Vector<al_struct>  als_j;
  PositionIndex l,m;
  alignment()
    {}
  alignment(PositionIndex _l, PositionIndex _m)
    : a(_m+1, (PositionIndex)0),
    positionSum(_l+1, (PositionIndex)0), f(_l+1, (PositionIndex)0), als_i(_l+1,0),als_j(_m+1),l(_l), m(_m)
    {
      f[0]=m;
      for(PositionIndex j=1;j<=m;j++)
	{
	  if( j>1 )
	    als_j[j].prev= j-1;
	  if( j<m )
	    als_j[j].next= j+1;
	}
      als_i[0]=1;
    }
  PositionIndex get_l()const
    {return l;}
  PositionIndex get_m()const
    {return m;}
  void doMove(int i,int j)
    {
      set(j,i);
    }
  void doSwap(int j1,int j2)
    {
      int aj1=a[j1],aj2=a[j2];
      set(j1,aj2);
      set(j2,aj1);
    }
  void set(PositionIndex j, PositionIndex aj)
    {
      PositionIndex old_aj=a[j];
      massert(j<a.size());massert(aj<f.size());
      massert(old_aj<f.size());massert(f[old_aj]>0);
      massert(j>0);
      positionSum[old_aj]-=j;
      // ausfuegen
      PositionIndex prev=als_j[j].prev;
      PositionIndex next=als_j[j].next;
      if( next )
	als_j[next].prev=prev;
      if( prev )
	als_j[prev].next=next;
      else
	als_i[old_aj]=next;
      
      // neue Position suchen
      PositionIndex lfd=als_i[aj],llfd=0;
      while( lfd && lfd<j )
	lfd = als_j[llfd=lfd].next;

      // einfuegen
      als_j[j].prev=llfd;
      als_j[j].next=lfd;
      if( llfd )
	als_j[llfd].next=j;
      else
	als_i[aj]=j;
      if( lfd )
	als_j[lfd].prev=j;

      f[old_aj]--;
      positionSum[aj]+=j;
      f[aj]++;
      a[j]=aj;
    }
  const Vector<PositionIndex>& getAlignment() const 
    {return a ;}
  PositionIndex get_al(PositionIndex j)const
    {
      massert(j<a.size());
      return a[j];
    }
  PositionIndex operator()(PositionIndex j)const
    {
      massert(j<a.size());
      return a[j];
    }
  PositionIndex fert(PositionIndex i)const
    {
      massert(i<f.size());
      return f[i];
    }
  PositionIndex get_head(PositionIndex i)const
    {
      massert( als_i[i]==_get_head(i) );
      return als_i[i];
    }
  PositionIndex get_center(PositionIndex i)const
    {
      if( i==0 )return 0;
      massert(((positionSum[i]+f[i]-1)/f[i]==_get_center(i)));
      return (positionSum[i]+f[i]-1)/f[i];
    }
  PositionIndex _get_head(PositionIndex i)const
    {
      if( fert(i)==0 )return 0;
      for(PositionIndex j=1;j<=m;j++)
	if( a[j]==i )
	  return j;
      return 0;
    }
  PositionIndex _get_center(PositionIndex i)const
    {
      if( i==0 )return 0;
      massert(fert(i));
      PositionIndex sum=0;
      for(PositionIndex j=1;j<=m;j++)
	if( a[j]==i )
	  sum+=j;
      return (sum+fert(i)-1)/fert(i);
    }
  PositionIndex prev_cept(PositionIndex i)const
    {
      if( i==0 )return 0;
      PositionIndex k=i-1;
      while(k&&fert(k)==0)
	k--;
      return k;
    }
  PositionIndex next_cept(PositionIndex i)const
    {
      PositionIndex k=i+1;
      while(k<l+1&&fert(k)==0)
	k++;
      return k;
    }
  PositionIndex prev_in_cept(PositionIndex j)const
    {
      //PositionIndex k=j-1;
      //while(k&&a[k]!=a[j])
      //k--;
      //assert( als_j[j].prev==k );
      //assert(k);
      //return k;
      massert(als_j[j].prev==0||a[als_j[j].prev]==a[j]);
      return als_j[j].prev;
    }
  friend ostream &operator<<(ostream&out, const alignment&a);
  friend bool operator==(const alignment&a, const alignment&b)
    {
      massert(a.a.size()==b.a.size());
      for(PositionIndex j=1;j<=a.get_m();j++)
	if(a(j)!=b(j))
	  return 0;
      return 1;
    }
  friend bool operator<(const alignment&x, const alignment&y)
    {
      massert(x.get_m()==y.get_m());
      for(PositionIndex j=1;j<=x.get_m();j++)
	if( x(j)<y(j) ) 
	  return 1;
	else if( y(j)<x(j) )
	  return 0;
      return 0;
    }
  friend int differences(const alignment&x, const alignment&y){
    int count=0;
    massert(x.get_m()==y.get_m());
    for(PositionIndex j=1;j<=x.get_m();j++)
      count += (x(j)!=y(j));
    return count;
  }
  bool valid()const
    {
      if( 2*f[0]>m )
	return 0;
      for(unsigned int i=1;i<=l;i++)
	if( f[i]>=MAX_FERTILITY )
	  return 0;
      return 1;
    }
  friend class transpair_model5;
};
#endif