| 1 |
package morfologik.stemming; |
| 2 |
|
| 3 |
import java.io.UnsupportedEncodingException; |
| 4 |
import java.nio.ByteBuffer; |
| 5 |
import java.nio.CharBuffer; |
| 6 |
import java.nio.charset.*; |
| 7 |
|
| 8 |
/** |
| 9 |
* Stem and tag data associated with a given word. |
| 10 |
* |
| 11 |
* Instances of this class are reused and mutable (values |
| 12 |
* returned from {@link #getStem()}, {@link #getWord()} |
| 13 |
* and other related methods change on subsequent calls to |
| 14 |
* {@link DictionaryLookup} class that returned a given |
| 15 |
* instance of {@link WordData}. |
| 16 |
* |
| 17 |
* If you need a copy of the |
| 18 |
* stem or tag data for a given word, you have to create a custom buffer |
| 19 |
* yourself and copy the associated data, perform {@link #clone()} or create |
| 20 |
* strings (they are immutable) using {@link #getStem()} and then |
| 21 |
* {@link CharSequence#toString()}. |
| 22 |
* |
| 23 |
* For reasons above it makes no sense to use instances |
| 24 |
* of this class in associative containers or lists. In fact, |
| 25 |
* both {@link #equals(Object)} and {@link #hashCode()} are overridden and throw |
| 26 |
* exceptions to prevent accidental damage. |
| 27 |
*/ |
| 28 |
public final class WordData implements Cloneable { |
| 29 |
/** |
| 30 |
* Error information if somebody puts us in a Java collection. |
| 31 |
*/ |
| 32 |
private static final String COLLECTIONS_ERROR_MESSAGE = "Not suitable for use" |
| 33 |
+ " in Java collections framework (volatile content). Refer to documentation."; |
| 34 |
|
| 35 |
/** Character encoding in internal buffers. */ |
| 36 |
private final CharsetDecoder decoder; |
| 37 |
|
| 38 |
/** |
| 39 |
* Inflected word form data. |
| 40 |
*/ |
| 41 |
private CharSequence wordCharSequence; |
| 42 |
|
| 43 |
/** |
| 44 |
* Character sequence after converting {@link #stemBuffer} using |
| 45 |
* {@link #decoder}. |
| 46 |
*/ |
| 47 |
private CharBuffer stemCharSequence; |
| 48 |
|
| 49 |
/** |
| 50 |
* Character sequence after converting {@link #tagBuffer} using |
| 51 |
* {@link #decoder}. |
| 52 |
*/ |
| 53 |
private CharBuffer tagCharSequence; |
| 54 |
|
| 55 |
/** Byte buffer holding the inflected word form data. */ |
| 56 |
ByteBuffer wordBuffer; |
| 57 |
|
| 58 |
/** Byte buffer holding stem data. */ |
| 59 |
ByteBuffer stemBuffer; |
| 60 |
|
| 61 |
/** Byte buffer holding tag data. */ |
| 62 |
ByteBuffer tagBuffer; |
| 63 |
|
| 64 |
/** |
| 65 |
* Package scope constructor. |
| 66 |
*/ |
| 67 |
WordData(CharsetDecoder decoder) { |
| 68 |
this.decoder = decoder; |
| 69 |
|
| 70 |
stemBuffer = ByteBuffer.allocate(0); |
| 71 |
tagBuffer = ByteBuffer.allocate(0); |
| 72 |
stemCharSequence = CharBuffer.allocate(0); |
| 73 |
tagCharSequence = CharBuffer.allocate(0); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* A constructor for tests only. |
| 78 |
*/ |
| 79 |
WordData(String stem, String tag, String encoding) { |
| 80 |
this(Charset.forName(encoding).newDecoder()); |
| 81 |
|
| 82 |
try { |
| 83 |
if (stem != null) |
| 84 |
stemBuffer.put(stem.getBytes(encoding)); |
| 85 |
if (tag != null) |
| 86 |
tagBuffer.put(tag.getBytes(encoding)); |
| 87 |
} catch (UnsupportedEncodingException e) { |
| 88 |
throw new RuntimeException(e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Copy the stem's binary data (no charset decoding) to a custom byte |
| 94 |
* buffer. |
| 95 |
* |
| 96 |
* The buffer is cleared prior to copying and flipped for reading |
| 97 |
* upon returning from this method. If the buffer is null or not large |
| 98 |
* enough to hold the result, a new buffer is allocated. |
| 99 |
* |
| 100 |
* @param target |
| 101 |
* Target byte buffer to copy the stem buffer to or |
| 102 |
* <code>null</code> if a new buffer should be allocated. |
| 103 |
* |
| 104 |
* @return Returns <code>target</code> or the new reallocated buffer. |
| 105 |
*/ |
| 106 |
public ByteBuffer getStemBytes(ByteBuffer target) { |
| 107 |
target = BufferUtils.clearAndEnsureCapacity(target, stemBuffer.remaining()); |
| 108 |
stemBuffer.mark(); |
| 109 |
target.put(stemBuffer); |
| 110 |
stemBuffer.reset(); |
| 111 |
target.flip(); |
| 112 |
return target; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Copy the tag's binary data (no charset decoding) to a custom byte buffer. |
| 117 |
* |
| 118 |
* The buffer is cleared prior to copying and flipped for reading |
| 119 |
* upon returning from this method. If the buffer is null or not large |
| 120 |
* enough to hold the result, a new buffer is allocated. |
| 121 |
* |
| 122 |
* @param target |
| 123 |
* Target byte buffer to copy the tag buffer to or |
| 124 |
* <code>null</code> if a new buffer should be allocated. |
| 125 |
* |
| 126 |
* @return Returns <code>target</code> or the new reallocated buffer. |
| 127 |
*/ |
| 128 |
public ByteBuffer getTagBytes(ByteBuffer target) { |
| 129 |
target = BufferUtils.clearAndEnsureCapacity(target, tagBuffer.remaining()); |
| 130 |
tagBuffer.mark(); |
| 131 |
target.put(tagBuffer); |
| 132 |
tagBuffer.reset(); |
| 133 |
target.flip(); |
| 134 |
return target; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Copy the inflected word's binary data (no charset decoding) to a custom |
| 139 |
* byte buffer. |
| 140 |
* |
| 141 |
* The buffer is cleared prior to copying and flipped for reading |
| 142 |
* upon returning from this method. If the buffer is null or not large |
| 143 |
* enough to hold the result, a new buffer is allocated. |
| 144 |
* |
| 145 |
* @param target |
| 146 |
* Target byte buffer to copy the word buffer to or |
| 147 |
* <code>null</code> if a new buffer should be allocated. |
| 148 |
* |
| 149 |
* @return Returns <code>target</code> or the new reallocated buffer. |
| 150 |
*/ |
| 151 |
public ByteBuffer getWordBytes(ByteBuffer target) { |
| 152 |
target = BufferUtils.clearAndEnsureCapacity(target, wordBuffer.remaining()); |
| 153 |
wordBuffer.mark(); |
| 154 |
target.put(wordBuffer); |
| 155 |
wordBuffer.reset(); |
| 156 |
target.flip(); |
| 157 |
return target; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @return Return tag data decoded to a character sequence or |
| 162 |
* <code>null</code> if no associated tag data exists. |
| 163 |
*/ |
| 164 |
public CharSequence getTag() { |
| 165 |
tagCharSequence = BufferUtils.bytesToChars(decoder, tagBuffer, tagCharSequence); |
| 166 |
return tagCharSequence.remaining() == 0 ? null : tagCharSequence; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @return Return stem data decoded to a character sequence or |
| 171 |
* <code>null</code> if no associated stem data exists. |
| 172 |
*/ |
| 173 |
public CharSequence getStem() { |
| 174 |
stemCharSequence = BufferUtils.bytesToChars(decoder, stemBuffer, stemCharSequence); |
| 175 |
return stemCharSequence.remaining() == 0 ? null : stemCharSequence; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @return Return inflected word form data. Usually the parameter passed to |
| 180 |
* {@link DictionaryLookup#lookup(CharSequence)}. |
| 181 |
*/ |
| 182 |
public CharSequence getWord() { |
| 183 |
return wordCharSequence; |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 187 |
* |
| 188 |
*/ |
| 189 |
@Override |
| 190 |
public boolean equals(Object obj) { |
| 191 |
throw new UnsupportedOperationException(COLLECTIONS_ERROR_MESSAGE); |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* |
| 196 |
*/ |
| 197 |
@Override |
| 198 |
public int hashCode() { |
| 199 |
throw new UnsupportedOperationException(COLLECTIONS_ERROR_MESSAGE); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public String toString() { |
| 204 |
return "WordData[" |
| 205 |
+ this.getWord() + "," |
| 206 |
+ this.getStem() + "," |
| 207 |
+ this.getTag() + "]"; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Declare a covariant of {@link Object#clone()} that returns a deep copy of |
| 212 |
* this object. The content of all internal buffers is copied. |
| 213 |
*/ |
| 214 |
@Override |
| 215 |
public WordData clone() { |
| 216 |
final WordData clone = new WordData(this.decoder); |
| 217 |
clone.wordCharSequence = cloneCharSequence(wordCharSequence); |
| 218 |
clone.wordBuffer = getWordBytes(null); |
| 219 |
clone.stemBuffer = getStemBytes(null); |
| 220 |
clone.tagBuffer = getTagBytes(null); |
| 221 |
return clone; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Clone char sequences only if not immutable. |
| 226 |
*/ |
| 227 |
private CharSequence cloneCharSequence(CharSequence chs) { |
| 228 |
if (chs instanceof String) |
| 229 |
return chs; |
| 230 |
return chs.toString(); |
| 231 |
} |
| 232 |
|
| 233 |
void update(ByteBuffer wordBuffer, CharSequence word) { |
| 234 |
this.stemCharSequence.clear(); |
| 235 |
this.tagCharSequence.clear(); |
| 236 |
this.stemBuffer.clear(); |
| 237 |
this.tagBuffer.clear(); |
| 238 |
|
| 239 |
this.wordBuffer = wordBuffer; |
| 240 |
this.wordCharSequence = word; |
| 241 |
} |
| 242 |
} |