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

IndexedPhrasesPair.h « eppex « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18e3a39fd9c1f4d2e1ff8cb3adc44f1d83145aff (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
 * IndexedPhrasesPair - implementation of a single phrase pair source and target
 * phrases represented by numeric indices.
 *
 * (C) Ceslav Przywara, UFAL MFF UK, 2011
 *
 * $Id$
 *
 * TODO:
 * - current unordered_map implementation is terribly slow. More sophisticated
 * design of hash function should help.
 */

#ifndef INDEXEDPHRASESPAIR_H
#define	INDEXEDPHRASESPAIR_H

#include <vector>
#include <new>
#include <algorithm>
#include <string.h>
#ifdef USE_UNORDERED_MAP
#include <tr1/functional_hash.h>
#endif

// Forward declaration because of friend comparison operator declaration below.
template<class OrientationIndexType, class TokenIndexType> class IndexedPhrasesPair;

#ifdef USE_UNORDERED_MAP
template<class OrientationIndexType, class TokenIndexType>
class IndexedPhrasePairHasher;

// Comparison operator (is going to be declared as friend).
template<class OrientationIndexType, class TokenIndexType>
bool operator== (const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& lhs, const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& rhs);
#else
// Comparison operator (is going to be declared as friend).
template<class OrientationIndexType, class TokenIndexType>
bool operator< (const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& lhs, const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& rhs);
#endif

/**
 * Structure capable of holding a phrase pair consisting of:
 * a) source phrase
 * b) target phrase
 * c) word alignment
 * d) orientation info
 * @param OrientationIndexType
 * @param TokenIndexType - datatype for token indices.
 */
template<class OrientationIndexType = unsigned char, class TokenIndexType = unsigned int>
class IndexedPhrasesPair {
public:

    typedef TokenIndexType token_index_t;

    typedef OrientationIndexType orientation_info_index_t;

    typedef std::vector<TokenIndexType> phrase_t;

    // A single alignment point.
    typedef unsigned char alignment_point_t;

    // A single pair of alignments points.
    typedef std::pair<alignment_point_t, alignment_point_t> alignment_pair_t;

    // A single phrase alignment (eg. 0-0 0-1 1-2)
    typedef std::vector<alignment_pair_t> alignment_t;

#ifdef USE_UNORDERED_MAP
    // Unordered map requires hashing functor object.
    typedef IndexedPhrasePairHasher<OrientationIndexType, TokenIndexType> Hash;
#endif

private:

    /** @var Source and target phrase as array of respective token indices */
    token_index_t* _data;

    /** @var A single phrase alignment stored in array */
    alignment_point_t* _alignment;

    /** @var Index of orientation info string */
    orientation_info_index_t _orientationInfoIndex;

    alignment_point_t _srcPhraseLength;

    alignment_point_t _tgtPhraseLength;

    alignment_point_t _alignmentLength;

public:

    IndexedPhrasesPair(void): _data(NULL), _alignment(NULL), _orientationInfoIndex(0), _srcPhraseLength(0), _tgtPhraseLength(0), _alignmentLength(0) {}

    IndexedPhrasesPair(const phrase_t& srcPhrase, const phrase_t& tgtPhrase, orientation_info_index_t orientationInfo, const alignment_t& alignment);

    IndexedPhrasesPair(const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& copy);

    ~IndexedPhrasesPair(void);

    IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& operator=(const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& other);

    phrase_t srcPhrase(void) const { return phrase_t(_data, _data + _srcPhraseLength); }

    phrase_t tgtPhrase(void) const { return phrase_t(_data + _srcPhraseLength, _data + _srcPhraseLength + _tgtPhraseLength); }

    orientation_info_index_t orientationInfo(void) const { return _orientationInfoIndex; }

    alignment_t alignment(void) const;

    const alignment_point_t * alignmentData(void) const { return _alignment; }

    alignment_point_t alignmentLength(void) const { return _alignmentLength; }

#ifdef USE_UNORDERED_MAP
    friend class IndexedPhrasePairHasher<OrientationIndexType, TokenIndexType>;
    friend bool operator== <> (const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& lhs, const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& rhs);
#else
    friend bool operator< <> (const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& lhs, const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& rhs);
#endif
};


////////////////////////////////////////////////////////////////////////////////
//// IndexedPhrasesPair IMPLEMENTATION /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

template<class OrientationIndexType, class TokenIndexType>
IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::IndexedPhrasesPair(const phrase_t& srcPhrase, const phrase_t& tgtPhrase, orientation_info_index_t orientationInfo, const alignment_t& alignment):
    _data(NULL), _alignment(NULL), _orientationInfoIndex(orientationInfo), _srcPhraseLength(static_cast<alignment_point_t>(srcPhrase.size())), _tgtPhraseLength(static_cast<alignment_point_t>(tgtPhrase.size())), _alignmentLength(alignment.size()) {

    // Save alignment.
    _alignment = new alignment_point_t[2 * _alignmentLength]; // Note: *2 for each pair.
    for ( size_t i = 0; i < alignment.size(); ++i ) {
        _alignment[i*2] = alignment[i].first;
        _alignment[i*2 + 1] = alignment[i].second;
    }

    // Save data.
    _data = new token_index_t[_srcPhraseLength + _tgtPhraseLength];
    std::copy(srcPhrase.begin(), srcPhrase.end(), _data);
    std::copy(tgtPhrase.begin(), tgtPhrase.end(), _data + _srcPhraseLength);

}

template<class OrientationIndexType, class TokenIndexType>
IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::IndexedPhrasesPair(const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& copy):
    _data(NULL), _alignment(NULL), _orientationInfoIndex(copy._orientationInfoIndex), _srcPhraseLength(copy._srcPhraseLength), _tgtPhraseLength(copy._tgtPhraseLength), _alignmentLength(copy._alignmentLength) {

    // Copy alignment.
    // alignment_point_t alignmentLength = std::max(_srcPhraseLength, _tgtPhraseLength);
    _alignment = new alignment_point_t[2 * _alignmentLength]; // Note: *2 for each pair.
    memcpy(_alignment, copy._alignment, _alignmentLength * 2 * sizeof(alignment_point_t));

    // Copy data.
    _data = new token_index_t[_srcPhraseLength + _tgtPhraseLength];
    std::copy(copy._data, copy._data + _srcPhraseLength + _tgtPhraseLength, _data);
}

template<class OrientationIndexType, class TokenIndexType>
IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::~IndexedPhrasesPair(void) {
    delete[] _alignment;
    delete[] _data;
}

template<class OrientationIndexType, class TokenIndexType>
IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::operator=(const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& other) {

    if ( this != &other ) {

        // Copy alignment.
        _alignmentLength = other._alignmentLength;
        // alignment_point_t alignmentLength = std::max(_srcPhraseLength, _tgtPhraseLength);
        alignment_point_t * alignment = new alignment_point_t[2 * _alignmentLength]; // Note: *2 for each pair.
        memcpy(alignment, other._alignment, _alignmentLength * 2 * sizeof(alignment_point_t));
        if ( _alignment != NULL ) {
            delete[] _alignment; // !
        }
        _alignment = alignment; // !

        // Copy data.
        _srcPhraseLength = other._srcPhraseLength;
        _tgtPhraseLength = other._tgtPhraseLength;
        token_index_t * data = new token_index_t[_srcPhraseLength + _tgtPhraseLength];
        std::copy(other._data, other._data + _srcPhraseLength + _tgtPhraseLength, data);
        if ( _data != NULL ) {
            delete[] _data; // !
        }
        _data = data; // !

        //
        _orientationInfoIndex = other._orientationInfoIndex;

    }

    return *this;
}

template<class OrientationIndexType, class TokenIndexType>
typename IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::alignment_t IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::alignment(void) const {
    alignment_t a;

    //alignment_point_t alignmentLength = std::max(_srcPhraseLength, _tgtPhraseLength);
    for ( size_t i = 0; i < _alignmentLength; ++i ) {
        a.push_back(alignment_pair_t(_alignment[2*i], _alignment[2*i + 1]));
    }

    return a;
}

#ifdef USE_UNORDERED_MAP
template<class OrientationIndexType, class TokenIndexType>
class IndexedPhrasePairHasher: public std::unary_function<IndexedPhrasesPair<OrientationIndexType, TokenIndexType>, size_t> {

    typedef typename IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::alignment_point_t alignment_point_t;

    std::tr1::hash<TokenIndexType> _hash;

public:
    size_t operator()(const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& phrasePair) const {
        size_t hash = 0;
        for ( alignment_point_t i = 0; i < phrasePair._srcPhraseLength + phrasePair._tgtPhraseLength; ++i ) {
            hash ^= _hash(phrasePair._data[i]);
        }
        return hash;
    }
};

template<class OrientationIndexType, class TokenIndexType>
bool operator== (const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& lhs, const IndexedPhrasesPair<OrientationIndexType, TokenIndexType>& rhs) {

    typedef typename IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::alignment_point_t alignment_point_t;
    typedef typename IndexedPhrasesPair<OrientationIndexType, TokenIndexType>::token_index_t string_index_t;

    // Alignments comparable?
    if ( lhs._alignmentLength != rhs._alignmentLength ) {
        return false;
    }

    // Same alignment length -> compare alignments.
    int cmp = memcmp(lhs._alignment, rhs._alignment, lhs._alignmentLength * 2 * sizeof(alignment_point_t));

    if ( cmp != 0 ) {
        // Alignments differ.
        return false;
    }

    // Alignments are equal, compare phrases (data).

    if ( lhs._srcPhraseLength != rhs._srcPhraseLength ) {
        // Source phrase lengths differs.
        return false;
    }

    if ( lhs._tgtPhraseLength != rhs._tgtPhraseLength ) {
        // Target phrase lengths differs.
        return false;
    }

    // Phrases have matching lengths, compare the data in the end:
    cmp = memcmp(lhs._data, rhs._data, (lhs._srcPhraseLength + lhs._tgtPhraseLength) * sizeof(string_index_t));

    if ( cmp != 0 ) {
        // Data differ.
        return false;
    }

    // Compare orientation info in the end.
    return lhs._orientationInfoIndex == rhs._orientationInfoIndex;
}
#else
template<class OrientationIndexType, class StringIndexType>
bool operator< (const IndexedPhrasesPair<OrientationIndexType, StringIndexType>& lhs, const IndexedPhrasesPair<OrientationIndexType, StringIndexType>& rhs) {

    typedef typename IndexedPhrasesPair<OrientationIndexType, StringIndexType>::alignment_point_t alignment_point_t;
    typedef typename IndexedPhrasesPair<OrientationIndexType, StringIndexType>::token_index_t string_index_t;

    // Alignments comparable?
    if ( lhs._alignmentLength != rhs._alignmentLength ) {
        // Shorter alignment length => lesser item.
        return lhs._alignmentLength < rhs._alignmentLength;
    }

    // Same alignment length -> compare alignments.
    int cmp = memcmp(lhs._alignment, rhs._alignment, lhs._alignmentLength * 2 * sizeof(alignment_point_t));

    if ( cmp != 0 ) {
        // Alignments differ.
        return cmp < 0;
    }

    // Alignments are equal, compare phrases (data).

    if ( lhs._srcPhraseLength != rhs._srcPhraseLength ) {
        // Source phrase lengths differs.
        return lhs._srcPhraseLength < rhs._srcPhraseLength;
    }

    if ( lhs._tgtPhraseLength != rhs._tgtPhraseLength ) {
        // Target phrase lengths differs.
        return lhs._tgtPhraseLength < rhs._tgtPhraseLength;
    }

    // Phrases have matching lengths, compare the data in the end:
    cmp = memcmp(lhs._data, rhs._data, (lhs._srcPhraseLength + lhs._tgtPhraseLength) * sizeof(string_index_t));
    if ( cmp != 0 ) {
        // Data differ.
        return cmp < 0;
    }

    // Compare orientation info in the end.
    return lhs._orientationInfoIndex < rhs._orientationInfoIndex;
}
#endif

#endif	/* INDEXEDPHRASESPAIR_H */