| 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 prefix <code>src</code> and <code>dst</code> have. The |
| 8 |
* output code is (bytes): |
| 9 |
* |
| 10 |
* <pre> |
| 11 |
* {P}{K}{suffix} |
| 12 |
* </pre> |
| 13 |
* |
| 14 |
* where (<code>P</code> - 'A') bytes should be trimmed from the start of |
| 15 |
* <code>src</code>, (<code>K</code> - 'A') bytes should be trimmed from the |
| 16 |
* end of <code>src</code> and then the <code>suffix</code> should be appended |
| 17 |
* to the resulting byte sequence. |
| 18 |
* |
| 19 |
* <p> |
| 20 |
* Examples: |
| 21 |
* </p> |
| 22 |
* |
| 23 |
* <pre> |
| 24 |
* src: abc |
| 25 |
* dst: abcd |
| 26 |
* encoded: AAd |
| 27 |
* |
| 28 |
* src: abc |
| 29 |
* dst: xyz |
| 30 |
* encoded: ADxyz |
| 31 |
* </pre> |
| 32 |
*/ |
| 33 |
public class TrimPrefixAndSuffixEncoder implements ISequenceEncoder { |
| 34 |
/** |
| 35 |
* Maximum encodable single-byte code. |
| 36 |
*/ |
| 37 |
private static final int REMOVE_EVERYTHING = 255; |
| 38 |
|
| 39 |
public ByteBuffer encode(ByteBuffer reuse, ByteBuffer source, ByteBuffer target) { |
| 40 |
// Search for the maximum matching subsequence that can be encoded. |
| 41 |
int maxSubsequenceLength = 0; |
| 42 |
int maxSubsequenceIndex = 0; |
| 43 |
for (int i = 0; i < source.remaining(); i++) { |
| 44 |
// prefix at i => shared subsequence (infix) |
| 45 |
int sharedPrefix = BufferUtils.sharedPrefixLength(source, i, target, 0); |
| 46 |
// Only update maxSubsequenceLength if we will be able to encode it. |
| 47 |
if (sharedPrefix > maxSubsequenceLength && i < REMOVE_EVERYTHING |
| 48 |
&& (source.remaining() - (i + sharedPrefix)) < REMOVE_EVERYTHING) { |
| 49 |
maxSubsequenceLength = sharedPrefix; |
| 50 |
maxSubsequenceIndex = i; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// Determine how much to remove (and where) from src to get a prefix of dst. |
| 55 |
int truncatePrefixBytes = maxSubsequenceIndex; |
| 56 |
int truncateSuffixBytes = (source.remaining() - (maxSubsequenceIndex + maxSubsequenceLength)); |
| 57 |
if (truncatePrefixBytes >= REMOVE_EVERYTHING || truncateSuffixBytes >= REMOVE_EVERYTHING) { |
| 58 |
maxSubsequenceIndex = maxSubsequenceLength = 0; |
| 59 |
truncatePrefixBytes = truncateSuffixBytes = REMOVE_EVERYTHING; |
| 60 |
} |
| 61 |
|
| 62 |
final int len1 = target.remaining() - maxSubsequenceLength; |
| 63 |
reuse = BufferUtils.clearAndEnsureCapacity(reuse, 2 + len1); |
| 64 |
|
| 65 |
assert target.hasArray() && |
| 66 |
target.position() == 0 && |
| 67 |
target.arrayOffset() == 0; |
| 68 |
|
| 69 |
reuse.put((byte) ((truncatePrefixBytes + 'A') & 0xFF)); |
| 70 |
reuse.put((byte) ((truncateSuffixBytes + 'A') & 0xFF)); |
| 71 |
reuse.put(target.array(), maxSubsequenceLength, len1); |
| 72 |
reuse.flip(); |
| 73 |
|
| 74 |
return reuse; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public int prefixBytes() { |
| 79 |
return 2; |
| 80 |
} |
| 81 |
|
| 82 |
public ByteBuffer decode(ByteBuffer reuse, ByteBuffer source, ByteBuffer encoded) { |
| 83 |
assert encoded.remaining() >= 2; |
| 84 |
|
| 85 |
final int p = encoded.position(); |
| 86 |
int truncatePrefixBytes = (encoded.get(p) - 'A') & 0xFF; |
| 87 |
int truncateSuffixBytes = (encoded.get(p + 1) - 'A') & 0xFF; |
| 88 |
|
| 89 |
if (truncatePrefixBytes == REMOVE_EVERYTHING || |
| 90 |
truncateSuffixBytes == REMOVE_EVERYTHING) { |
| 91 |
truncatePrefixBytes = source.remaining(); |
| 92 |
truncateSuffixBytes = 0; |
| 93 |
} |
| 94 |
|
| 95 |
assert source.hasArray() && |
| 96 |
source.position() == 0 && |
| 97 |
source.arrayOffset() == 0; |
| 98 |
|
| 99 |
assert encoded.hasArray() && |
| 100 |
encoded.position() == 0 && |
| 101 |
encoded.arrayOffset() == 0; |
| 102 |
|
| 103 |
final int len1 = source.remaining() - (truncateSuffixBytes + truncatePrefixBytes); |
| 104 |
final int len2 = encoded.remaining() - 2; |
| 105 |
reuse = BufferUtils.clearAndEnsureCapacity(reuse, len1 + len2); |
| 106 |
|
| 107 |
reuse.put(source.array(), truncatePrefixBytes, len1); |
| 108 |
reuse.put(encoded.array(), 2, len2); |
| 109 |
reuse.flip(); |
| 110 |
|
| 111 |
return reuse; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public String toString() { |
| 116 |
return getClass().getSimpleName(); |
| 117 |
} |
| 118 |
} |