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

AlignTables.h « src « mgizapp - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 52debc9956ec287f7ae2c582978028f4f87edbc4 (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
/*

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.

*/
#ifndef _aligntables_h
#define _aligntables_h 1

#include "defs.h"


#include <cassert>

#include <iostream>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
//#include <vector>
#include "Vector.h"
#include <utility>
#if __GNUC__>2
#include <ext/hash_map>
using __gnu_cxx::hash_map;
#else
#include <hash_map>
#endif
#include <cmath>
#include <fstream>
#include "transpair_model1.h"


/* ----------------- Class Defintions for hashmyalignment --------------------
   Objective: This class is used to define a hash mapping function to map
   an alignment (defined as a vector of integers) into a hash key
 ----------------------------------------------------------------------------*/

class hashmyalignment : public unary_function< Vector<WordIndex>, size_t >
{
public:
  size_t operator() (const Vector<WordIndex>& key) const
  // to define the mapping function. it takes an alignment (a vector of
  // integers) and it returns an integer value (hash key).
  {
    WordIndex j ;
    size_t s  ;
    size_t key_sum = 0 ;
    //      logmsg << "For alignment:" ;
    for (j = 1 ; j < key.size() ; j++) {
      //	logmsg << " " << key[j] ;
      key_sum += (size_t) (int) pow(double(key[j]), double((j % 6)+1));
    }
    //      logmsg << " , Key value was : " <<  key_sum;
    s = key_sum % 1000000 ;
    //      logmsg << " h(k) = " << s << endl ;
    return(s);
  }
#ifdef WIN32
  enum {
    // parameters for hash table
    bucket_size = 1		// 0 < bucket_size
  };

  bool operator()(const Vector<WordIndex> t1,
                  const Vector<WordIndex> t2) const {
    WordIndex j ;
    if (t1.size() != t2.size())
      return(false);
    for (j = 1 ; j < t1.size() ; j++)
      if (t1[j] != t2[j])
        return(false);
    return(true);
  }
#endif
};

#ifndef WIN32
class equal_to_myalignment
{
  // returns true if two alignments are the same (two vectors have same enties)
public:
  bool operator()(const Vector<WordIndex> t1,
                  const Vector<WordIndex> t2) const {
    WordIndex j ;
    if (t1.size() != t2.size())
      return(false);
    for (j = 1 ; j < t1.size() ; j++)
      if (t1[j] != t2[j])
        return(false);
    return(true);
  }

};
#endif

/* ---------------- End of Class Defnition for hashmyalignment --------------*/


/* ------------------ Class Defintions for alignmodel -----------------------
 Class Name: alignmodel
 Objective: Alignments neighborhhoods (collection of alignments) are stored in
 a hash table (for easy lookup). Each alignment vector is mapped into a hash
 key using the operator defined above.
 *--------------------------------------------------------------------------*/

class alignmodel
{
private:
#ifdef WIN32
  typedef hash_map<Vector<WordIndex>, LogProb, hashmyalignment > alignment_hash;

#else
  typedef hash_map<Vector<WordIndex>, LogProb, hashmyalignment, equal_to_myalignment > alignment_hash;

#endif
  alignment_hash a;
private:
  //  void erase(Vector<WordIndex>&);
public:

  // methods;

  inline alignment_hash::iterator begin(void) {
    return a.begin(); // begining of hash
  }
  inline alignment_hash::iterator end(void) {
    return a.end(); // end of hash
  }
  inline const alignment_hash& getHash() const {
    return a;
  }; // reference to hash table
  bool insert(Vector<WordIndex>&, LogProb val=0.0); // add a alignmnet
//  void setValue(Vector<WordIndex>&, LogProb val); // not needed
  LogProb getValue(Vector<WordIndex>&)const; // retrieve prob. of alignment
  inline void clear(void) {
    a.clear();
  }; // clear hash table
  //  void printTable(const char* filename);
  inline void resize(WordIndex n) {
#ifndef WIN32
    a.resize(n);
#endif
  }; // resize table

};

/* -------------- End of alignmode Class Definitions ------------------------*/
#endif