Debian Codegraph @

morfologik-stemming/src/main/java/morfologik/stemming/TrimInfixAndSuffixEncoder.java

raw
1 package morfologik.stemming;
2
3 import java.nio.ByteBuffer;
4
5 /**
6 * Encodes <code>dst</code> relative to <code>src</code> by trimming whatever
7 * non-equal suffix and infix <code>src</code> and <code>dst</code> have. The
8 * output code is (bytes):
9 *
10 * <pre>
11 * {X}{L}{K}{suffix}
12 * </pre>
13 *
14 * where <code>src's</code> infix at position (<code>X</code> - 'A') and of
15 * length (<code>L</code> - 'A') should be removed, then (<code>K</code> -
16 * 'A') bytes should be trimmed from the end and then the <code>suffix</code>
17 * should be appended to the resulting byte sequence.
18 *
19 * <p>
20 * Examples:
21 * </p>
22 *
23 * <pre>
24 * src: ayz
25 * dst: abc
26 * encoded: AACbc
27 *
28 * src: aillent
29 * dst: aller
30 * encoded: BBCr
31 * </pre>
32 */
33 public class TrimInfixAndSuffixEncoder implements ISequenceEncoder {
34 /**
35 * Maximum encodable single-byte code.
36 */
37 private static final int REMOVE_EVERYTHING = 255;
38 private ByteBuffer scratch = ByteBuffer.allocate(0);
39
40 public ByteBuffer encode(ByteBuffer reuse, ByteBuffer source, ByteBuffer target) {
41 assert source.hasArray() &&
42 source.position() == 0 &&
43 source.arrayOffset() == 0;
44
45 assert target.hasArray() &&
46 target.position() == 0 &&
47 target.arrayOffset() == 0;
48
49 // Search for the infix that can we can encode and remove from src
50 // to get a maximum-length prefix of dst. This could be done more efficiently
51 // by running a smarter longest-common-subsequence algorithm and some pruning (?).
52 //
53 // For now, naive loop should do.
54
55 // There can be only two positions for the infix to delete:
56 // 1) we remove leading bytes, even if they are partially matching (but a longer match
57 // exists somewhere later on).
58 // 2) we leave max. matching prefix and remove non-matching bytes that follow.
59 int maxInfixIndex = 0;
60 int maxSubsequenceLength = BufferUtils.sharedPrefixLength(source, target);
61 int maxInfixLength = 0;
62 for (int i : new int[] { 0, maxSubsequenceLength }) {
63 for (int j = 1; j <= source.remaining() - i; j++) {
64 // Compute temporary src with the infix removed.
65 // Concatenate in scratch space for simplicity.
66 final int len2 = source.remaining() - (i + j);
67 scratch = BufferUtils.clearAndEnsureCapacity(scratch, i + len2);
68 scratch.put(source.array(), 0, i);
69 scratch.put(source.array(), i + j, len2);
70 scratch.flip();
71
72 int sharedPrefix = BufferUtils.sharedPrefixLength(scratch, target);
73
74 // Only update maxSubsequenceLength if we will be able to encode it.
75 if (sharedPrefix > 0 && sharedPrefix > maxSubsequenceLength && i < REMOVE_EVERYTHING && j < REMOVE_EVERYTHING) {
76 maxSubsequenceLength = sharedPrefix;
77 maxInfixIndex = i;
78 maxInfixLength = j;
79 }
80 }
81 }
82
83 int truncateSuffixBytes = source.remaining() - (maxInfixLength + maxSubsequenceLength);
84
85 // Special case: if we're removing the suffix in the infix code, move it
86 // to the suffix code instead.
87 if (truncateSuffixBytes == 0 &&
88 maxInfixIndex + maxInfixLength == source.remaining()) {
89 truncateSuffixBytes = maxInfixLength;
90 maxInfixIndex = maxInfixLength = 0;
91 }
92
93 if (maxInfixIndex >= REMOVE_EVERYTHING ||
94 maxInfixLength >= REMOVE_EVERYTHING ||
95 truncateSuffixBytes >= REMOVE_EVERYTHING) {
96 maxInfixIndex = maxSubsequenceLength = 0;
97 maxInfixLength = truncateSuffixBytes = REMOVE_EVERYTHING;
98 }
99
100 final int len1 = target.remaining() - maxSubsequenceLength;
101 reuse = BufferUtils.clearAndEnsureCapacity(reuse, 3 + len1);
102
103 reuse.put((byte) ((maxInfixIndex + 'A') & 0xFF));
104 reuse.put((byte) ((maxInfixLength + 'A') & 0xFF));
105 reuse.put((byte) ((truncateSuffixBytes + 'A') & 0xFF));
106 reuse.put(target.array(), maxSubsequenceLength, len1);
107 reuse.flip();
108
109 return reuse;
110 }
111
112 @Override
113 public int prefixBytes() {
114 return 3;
115 }
116
117 public ByteBuffer decode(ByteBuffer reuse, ByteBuffer source, ByteBuffer encoded) {
118 assert encoded.remaining() >= 3;
119
120 final int p = encoded.position();
121 int infixIndex = (encoded.get(p) - 'A') & 0xFF;
122 int infixLength = (encoded.get(p + 1) - 'A') & 0xFF;
123 int truncateSuffixBytes = (encoded.get(p + 2) - 'A') & 0xFF;
124
125 if (infixLength == REMOVE_EVERYTHING ||
126 truncateSuffixBytes == REMOVE_EVERYTHING) {
127 infixIndex = 0;
128 infixLength = source.remaining();
129 truncateSuffixBytes = 0;
130 }
131
132 final int len1 = source.remaining() - (infixIndex + infixLength + truncateSuffixBytes);
133 final int len2 = encoded.remaining() - 3;
134 reuse = BufferUtils.clearAndEnsureCapacity(reuse, infixIndex + len1 + len2);
135
136 assert encoded.hasArray() &&
137 encoded.position() == 0 &&
138 encoded.arrayOffset() == 0;
139
140 assert source.hasArray() &&
141 source.position() == 0 &&
142 source.arrayOffset() == 0;
143
144 reuse.put(source.array(), 0, infixIndex);
145 reuse.put(source.array(), infixIndex + infixLength, len1);
146 reuse.put(encoded.array(), 3, len2);
147 reuse.flip();
148
149 return reuse;
150 }
151
152 @Override
153 public String toString() {
154 return getClass().getSimpleName();
155 }
156 }