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

ATables.h « GIZA++-v2 - github.com/moses-smt/giza-pp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1183ab67ea71e7dfea2744db3f705fa94751f9d (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
/*

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.

*/
/* --------------------------------------------------------------------------*
 *                                                                           *
 * Module :ATables                                                           *
 *                                                                           *
 * Prototypes File: ATables.h                                                *
 *                                                                           *
 * Objective: Defines clases and methods for handling I/O for distortion  &  *
 *            alignment tables.                                              *
 *****************************************************************************/

#ifndef _atables_h
#define _atables_h 1

#include "defs.h"
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include "Vector.h"
#include <utility>
#if __GNUC__>2
#include <ext/hash_map>
using __gnu_cxx::hash_map;
#else
#include <hash_map>
#endif
#include <fstream>
#include "Array4.h"
#include "myassert.h"
#include "Globals.h"

extern bool CompactADTable;
extern float amodel_smooth_factor;
extern short NoEmptyWord;

/* ------------------- Class Defintions of amodel ---------------------------*/
/* Class Name: amodel:
   Objective: This defines the underlying data structure for distortiont prob.
   and count tables. They are defined as a hash table. Each entry in the hash
   table is the probability (d(j/l,m,i), where j is word target position, i is
   source word position connected to it, m is target sentence length, and l is
   source sentence length) or count collected for it. The  probability and the
   count are represented as log integer probability as 
   defined by the class LogProb .  

  This class is used to represents a Tables (probabiliity) and d (distortion)
  tables and also their corresponding count tables .
  
  *--------------------------------------------------------------------------*/

inline int Mabs(int a)
{
  if(a<0)
    return -a;
  else
    return a;
}

template <class VALTYPE>
class amodel
{
 public:
  Array4<VALTYPE> a;
  bool is_distortion ; 
  WordIndex MaxSentLength;
  bool ignoreL, ignoreM;
  VALTYPE get(WordIndex aj, WordIndex j, WordIndex l, WordIndex m)const
    {
      massert( (!is_distortion) || aj<=m );massert( (!is_distortion) || j<=l );massert( (!is_distortion) || aj!=0 );
      massert( is_distortion    || aj<=l );massert( is_distortion    || j<=m );massert( (is_distortion) || j!=0 );
      massert( l<MaxSentLength );massert( m<MaxSentLength );
      return a.get(aj, j, (CompactADTable&&is_distortion)?MaxSentLength:(l+1),(CompactADTable&&!is_distortion)?MaxSentLength:(m+1));
    }
  static float smooth_factor;
  amodel(bool flag)
    : a(MAX_SENTENCE_LENGTH+1,0.0), is_distortion(flag), MaxSentLength(MAX_SENTENCE_LENGTH)
    {}; 
  VALTYPE&getRef(WordIndex aj, WordIndex j, WordIndex l, WordIndex m)
    {
      massert( (!is_distortion) || aj<=m );massert( (!is_distortion) || j<=l );
      massert( is_distortion    || aj<=l );massert( is_distortion    || j<=m );massert( (is_distortion) || j!=0 );
      massert( l<MaxSentLength );massert( m<MaxSentLength );
      return a(aj, j, (CompactADTable&&is_distortion)?MaxSentLength:(l+1),(CompactADTable&&!is_distortion)?MaxSentLength:(m+1));
    }
  void setValue(WordIndex aj, WordIndex j, WordIndex l, WordIndex m, VALTYPE val)
    {
      getRef(aj, j, l, m)=val;
    }
  VALTYPE getValue(WordIndex aj, WordIndex j, WordIndex l, WordIndex m) const
    {
      if( is_distortion==0 )
	return max(double(PROB_SMOOTH),amodel_smooth_factor/(l+1)+(1.0-amodel_smooth_factor)*get(aj, j, l, m));
      else
	return max(double(PROB_SMOOTH),amodel_smooth_factor/m+(1.0-amodel_smooth_factor)*get(aj, j, l, m));	
    }
  void printTable(const char* filename)const ; 
  template<class COUNT>
  void normalize(amodel<COUNT>& aTable)const
    {
      WordIndex i, j, l, m ;
      COUNT total;
      int nParam=0;
      for(l=0;l<MaxSentLength;l++)
	for(m=0;m<MaxSentLength;m++)
	  {
	    if( CompactADTable && l!=m )
	      continue;
	    unsigned int L=((CompactADTable&&is_distortion)?MaxSentLength:(l+1))-1;
	    unsigned int M=((CompactADTable&&!is_distortion)?MaxSentLength:(m+1))-1;
	    if( is_distortion==0 )
	      for(j=1;j<=M; j++)
		{
		  total=0.0;
		  for(i=0;i<=L;i++)
		    {
		      total+=get(i, j, L, M);
		    }
		  if( total )
		    for(i=0;i<=L;i++)
		      {
			nParam++;
			aTable.getRef(i, j, L, M)=get(i, j, L, M)/total;
			massert(aTable.getRef(i,j,L,M)<=1.0);
			if( NoEmptyWord&&i==0 )
			  aTable.getRef(i,j,L,M)=0;
		      }
		}
	    else
	      for(i=0;i<=L;i++)
		{
		  total=0.0;
		  for(j=1;j<=M;j++)
		    total+=get(j, i, L, M);
		  if( total )
		    for(j=1;j<=M;j++)
		      {
			aTable.getRef(j, i, L, M)=amodel_smooth_factor/M+(1.0-amodel_smooth_factor)*get(j, i, L, M)/total;
			nParam++;
			massert(aTable.getRef(j,i,L,M)<=1.0);
			if( NoEmptyWord&&i==0 )
			  aTable.getRef(j,i,L,M)=0;
		      }
		}
	  }
      cout << "A/D table contains " << nParam << " parameters.\n";
    }

  void readTable(const char *filename); 
  void clear()
    {a.clear();}
};

/* ------------------- End of amodel Class Definitions ----------------------*/

#endif