summaryrefslogtreecommitdiff
path: root/morfologik-tools/src/main/java/morfologik/tools/SequenceEncoders.java
blob: 37cd0ccdf9f4225f52497c36320c5120e3548178 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package morfologik.tools;

import morfologik.stemming.EncoderType;

import com.carrotsearch.hppc.ByteArrayList;

/**
 * Container class for sequence encoders.
 */
public final class SequenceEncoders {
    private SequenceEncoders() {}

    /**
     * Maximum encodable single-byte code.
     */
    private static final int REMOVE_EVERYTHING = 255;

    public static interface IEncoder {
        public ByteArrayList encode(ByteArrayList src, ByteArrayList derived, ByteArrayList encodedBuffer);
        public ByteArrayList decode(ByteArrayList src, ByteArrayList encoded, ByteArrayList derivedBuffer);
        public EncoderType type();
    }

    /**
     * Encodes <code>dst</code> relative to <code>src</code> by trimming 
     * whatever non-equal suffix <code>src</code> has. The output code is (bytes):
     * <pre>
     * {K}{suffix}
     * </pre>
     * where (<code>K</code> - 'A') bytes should be trimmed from the end of <code>src</code> 
     * and then the <code>suffix</code> should be appended to the resulting byte sequence.
     * 
     * <p>Examples:</p>
     * <pre>
     * src: foo
     * dst: foobar
     * encoded: Abar
     * 
     * src: foo
     * dst: bar
     * encoded: Dbar
     * </pre>
     * 
     * <p><strong>Note:</strong> The code length is a single byte. If equal to 
     * {@link SequenceEncoders#REMOVE_EVERYTHING} the entire <code>src</code> sequence
     * should be discarded.</p>
     */
    public static class TrimSuffixEncoder implements IEncoder {
        public ByteArrayList encode(ByteArrayList src, ByteArrayList dst, ByteArrayList encoded) {
            int sharedPrefix = sharedPrefixLength(src, dst);
            int truncateBytes = src.size() - sharedPrefix;
            if (truncateBytes >= REMOVE_EVERYTHING) {
                truncateBytes = REMOVE_EVERYTHING;
                sharedPrefix = 0;
            }

            final byte suffixTrimCode = (byte) (truncateBytes + 'A');
            encoded.add(suffixTrimCode);
            encoded.add(dst.buffer, sharedPrefix, dst.size() - sharedPrefix);

            return encoded;
        }

        public ByteArrayList decode(ByteArrayList src, ByteArrayList encoded, ByteArrayList dst) {
            int suffixTrimCode = encoded.get(0);
            int truncateBytes = (suffixTrimCode - 'A') & 0xFF;
            if (truncateBytes == REMOVE_EVERYTHING) {
                truncateBytes = src.size();
            }

            dst.add(src.buffer, 0, src.size() - truncateBytes);
            dst.add(encoded.buffer, 1, encoded.size() - 1);

            return dst;
        }
        
        @Override
        public EncoderType type() {
            return EncoderType.SUFFIX;
        }
        
        @Override
        public String toString() {
            return getClass().getSimpleName();
        }
    }

    /**
     * Encodes <code>dst</code> relative to <code>src</code> by trimming 
     * whatever non-equal suffix and prefix <code>src</code> and <code>dst</code> have. 
     * The output code is (bytes):
     * <pre>
     * {P}{K}{suffix}
     * </pre>
     * where (<code>P</code> - 'A') bytes should be trimmed from the start of <code>src</code>,
     *  (<code>K</code> - 'A') bytes should be trimmed from the end of <code>src</code>
     * and then the <code>suffix</code> should be appended to the resulting byte sequence.
     * 
     * <p>Examples:</p>
     * <pre>
     * src: abc
     * dst: abcd
     * encoded: AAd
     * 
     * src: abc
     * dst: xyz
     * encoded: ADxyz
     * </pre>
     * 
     * <p><strong>Note:</strong> Each code's length is a single byte. If any is equal to 
     * {@link SequenceEncoders#REMOVE_EVERYTHING} the entire <code>src</code> sequence
     * should be discarded.</p>
     */
    public static class TrimPrefixAndSuffixEncoder implements IEncoder {
        public ByteArrayList encode(ByteArrayList src, ByteArrayList dst, ByteArrayList encoded) {
            // Search for the maximum matching subsequence that can be encoded. 
            int maxSubsequenceLength = 0;
            int maxSubsequenceIndex = 0;
            for (int i = 0; i < src.size(); i++) {
                // prefix at i => shared subsequence (infix)
                int sharedPrefix = sharedPrefixLength(src, i, dst, 0);
                // Only update maxSubsequenceLength if we will be able to encode it.
                if (sharedPrefix > maxSubsequenceLength
                        && i < REMOVE_EVERYTHING
                        && (src.size() - (i + sharedPrefix)) < REMOVE_EVERYTHING) {
                    maxSubsequenceLength = sharedPrefix;
                    maxSubsequenceIndex = i;
                }
            }

            // Determine how much to remove (and where) from src to get a prefix of dst.
            int truncatePrefixBytes = maxSubsequenceIndex;
            int truncateSuffixBytes = (src.size() - (maxSubsequenceIndex + maxSubsequenceLength));
            if (truncatePrefixBytes >= REMOVE_EVERYTHING ||
                truncateSuffixBytes >= REMOVE_EVERYTHING) {
                maxSubsequenceIndex = maxSubsequenceLength = 0;
                truncatePrefixBytes = truncateSuffixBytes = REMOVE_EVERYTHING;
            }

            encoded.add((byte) ((truncatePrefixBytes + 'A') & 0xFF));
            encoded.add((byte) ((truncateSuffixBytes + 'A') & 0xFF));
            encoded.add(dst.buffer, maxSubsequenceLength, dst.size() - maxSubsequenceLength);

            return encoded;
        }

        public ByteArrayList decode(ByteArrayList src, ByteArrayList encoded, ByteArrayList dst) {
            int truncatePrefixBytes = (encoded.get(0) - 'A') & 0xFF;
            int truncateSuffixBytes = (encoded.get(1) - 'A') & 0xFF;

            if (truncatePrefixBytes == REMOVE_EVERYTHING ||
                truncateSuffixBytes == REMOVE_EVERYTHING) {
                truncatePrefixBytes = src.size();
                truncateSuffixBytes = 0;
            }

            dst.add(src.buffer, truncatePrefixBytes, src.size() - (truncateSuffixBytes + truncatePrefixBytes));
            dst.add(encoded.buffer, 2, encoded.size() - 2);

            return dst;
        }
        
        @Override
        public EncoderType type() {
            return EncoderType.PREFIX;
        }
        
        @Override
        public String toString() {
            return getClass().getSimpleName();
        }        
    }

    /**
     * Encodes <code>dst</code> relative to <code>src</code> by trimming 
     * whatever non-equal suffix and infix <code>src</code> and <code>dst</code> have. 
     * The output code is (bytes):
     * <pre>
     * {X}{L}{K}{suffix}
     * </pre>
     * where <code>src's</code> infix at position (<code>X</code> - 'A') and of length
     * (<code>L</code> - 'A') should be removed, then (<code>K</code> - 'A') bytes 
     * should be trimmed from the end
     * and then the <code>suffix</code> should be appended to the resulting byte sequence.
     * 
     * <p>Examples:</p>
     * <pre>
     * src: ayz
     * dst: abc
     * encoded: AACbc
     * 
     * src: aillent
     * dst: aller
     * encoded: BBCr
     * </pre>
     * 
     * <p><strong>Note:</strong> Each code's length is a single byte. If any is equal to 
     * {@link SequenceEncoders#REMOVE_EVERYTHING} the entire <code>src</code> sequence
     * should be discarded.</p>
     */
    public static class TrimInfixAndSuffixEncoder implements IEncoder {
        ByteArrayList scratch = new ByteArrayList();

        public ByteArrayList encode(ByteArrayList src, ByteArrayList dst, ByteArrayList encoded) {
            // Search for the infix that can we can encode and remove from src
            // to get a maximum-length prefix of dst. This could be done more efficiently
            // by running a smarter longest-common-subsequence algorithm and some pruning (?).
            //
            // For now, naive loop should do.

            // There can be only two positions for the infix to delete:
            // 1) we remove leading bytes, even if they are partially matching (but a longer match
            //    exists somewhere later on).
            // 2) we leave max. matching prefix and remove non-matching bytes that follow. 
            int maxInfixIndex = 0;
            int maxSubsequenceLength = sharedPrefixLength(src, dst);
            int maxInfixLength = 0;
            for (int i : new int [] {0, maxSubsequenceLength}) {
                for (int j = 1; j <= src.size() - i; j++) {
                    // Compute temporary src with the infix removed.
                    // Concatenate in scratch space for simplicity.
                    scratch.clear();
                    scratch.add(src.buffer, 0, i);
                    scratch.add(src.buffer, i + j, src.size() - (i + j));
    
                    int sharedPrefix = sharedPrefixLength(scratch, dst);

                    // Only update maxSubsequenceLength if we will be able to encode it.
                    if (sharedPrefix > 0 && 
                        sharedPrefix > maxSubsequenceLength &&
                        i < REMOVE_EVERYTHING &&
                        j < REMOVE_EVERYTHING) {
                        maxSubsequenceLength = sharedPrefix;
                        maxInfixIndex = i;
                        maxInfixLength = j;
                    }
                }
            }
            
            int truncateSuffixBytes = src.size() - (maxInfixLength + maxSubsequenceLength);
            
            // Special case: if we're removing the suffix in the infix code, move it
            // to the suffix code instead.
            if (truncateSuffixBytes == 0 &&
                maxInfixIndex + maxInfixLength == src.size()) {
                truncateSuffixBytes = maxInfixLength;
                maxInfixIndex = maxInfixLength = 0;
            }
                
            
            if (maxInfixIndex >= REMOVE_EVERYTHING ||
                maxInfixLength >= REMOVE_EVERYTHING ||
                truncateSuffixBytes >= REMOVE_EVERYTHING) {
                maxInfixIndex = maxSubsequenceLength = 0;
                maxInfixLength = truncateSuffixBytes = REMOVE_EVERYTHING;
            }

            encoded.add((byte) ((maxInfixIndex       + 'A') & 0xFF));
            encoded.add((byte) ((maxInfixLength      + 'A') & 0xFF));
            encoded.add((byte) ((truncateSuffixBytes + 'A') & 0xFF));
            encoded.add(dst.buffer, maxSubsequenceLength, dst.size() - maxSubsequenceLength);

            return encoded;
        }

        public ByteArrayList decode(ByteArrayList src, ByteArrayList encoded, ByteArrayList dst) {
            int infixIndex  = (encoded.get(0) - 'A') & 0xFF;
            int infixLength = (encoded.get(1) - 'A') & 0xFF;
            int truncateSuffixBytes = (encoded.get(2) - 'A') & 0xFF;

            if (infixLength == REMOVE_EVERYTHING ||
                truncateSuffixBytes == REMOVE_EVERYTHING) {
                infixIndex = 0;
                infixLength = src.size();
                truncateSuffixBytes = 0;
            }

            dst.add(src.buffer, 0, infixIndex);
            dst.add(src.buffer, infixIndex + infixLength, src.size() - (infixIndex + infixLength + truncateSuffixBytes));
            dst.add(encoded.buffer, 3, encoded.size() - 3);

            return dst;
        }

        @Override
        public EncoderType type() {
            return EncoderType.INFIX;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName();
        }        
    }
    
    /**
     * 
     */
    public static class CopyEncoder implements IEncoder {
        @Override
        public ByteArrayList encode(ByteArrayList src, ByteArrayList derived, ByteArrayList encodedBuffer)
        {
            encodedBuffer.add(derived.buffer, 0, derived.size());
            return encodedBuffer;
        }
        
        @Override
        public ByteArrayList decode(ByteArrayList src, ByteArrayList encoded, ByteArrayList derivedBuffer)
        {
            derivedBuffer.add(encoded.buffer, 0, encoded.size());
            return derivedBuffer;
        }
        
        @Override
        public EncoderType type() {
            return EncoderType.NONE;
        }

        @Override
        public String toString() {
            return getClass().getSimpleName();
        }        
    }

    /**
     * Compute the length of the shared prefix between two byte sequences.
     */
    private static int sharedPrefixLength(ByteArrayList a, ByteArrayList b) {
        final int max = Math.min(a.size(), b.size());
        int i = 0;
        while (i < max && a.get(i) == b.get(i)) {
            i++;
        }
        return i;
    }

    /**
     * Compute the length of the shared prefix between two byte sequences.
     */
    private static int sharedPrefixLength(ByteArrayList a, int aStart, ByteArrayList b, int bStart) {

        int i = 0;
        while (aStart < a.size() && 
               bStart < b.size() &&
               a.get(aStart++) == b.get(bStart++)) {
            i++;
        }
        return i;
    }

    public static IEncoder forType(EncoderType encType)
    {
        switch (encType) {
            case INFIX:  return new TrimInfixAndSuffixEncoder();
            case PREFIX: return new TrimPrefixAndSuffixEncoder();
            case SUFFIX: return new TrimSuffixEncoder();
            case NONE:   return new CopyEncoder();
        }
        throw new RuntimeException("Unknown encoder: " + encType); 
    }
}