| 1 |
package morfologik.stemming; |
| 2 |
|
| 3 |
import java.nio.ByteBuffer; |
| 4 |
import java.nio.CharBuffer; |
| 5 |
import java.nio.charset.CharsetDecoder; |
| 6 |
import java.util.Iterator; |
| 7 |
|
| 8 |
/** |
| 9 |
* An iterator over {@link WordData} entries of a {@link Dictionary}. The stems can be decoded from compressed format or |
| 10 |
* the compressed form can be preserved. |
| 11 |
*/ |
| 12 |
public final class DictionaryIterator implements Iterator<WordData> { |
| 13 |
private final CharsetDecoder decoder; |
| 14 |
private final Iterator<ByteBuffer> entriesIter; |
| 15 |
private final WordData entry; |
| 16 |
private final byte separator; |
| 17 |
private final boolean decodeStems; |
| 18 |
|
| 19 |
private ByteBuffer inflectedBuffer = ByteBuffer.allocate(0); |
| 20 |
private CharBuffer inflectedCharBuffer = CharBuffer.allocate(0); |
| 21 |
private ByteBuffer temp = ByteBuffer.allocate(0); |
| 22 |
private final ISequenceEncoder sequenceEncoder; |
| 23 |
|
| 24 |
public DictionaryIterator(Dictionary dictionary, CharsetDecoder decoder, boolean decodeStems) { |
| 25 |
this.entriesIter = dictionary.fsa.iterator(); |
| 26 |
this.separator = dictionary.metadata.getSeparator(); |
| 27 |
this.sequenceEncoder = dictionary.metadata.getSequenceEncoderType().get(); |
| 28 |
this.decoder = decoder; |
| 29 |
this.entry = new WordData(decoder); |
| 30 |
this.decodeStems = decodeStems; |
| 31 |
} |
| 32 |
|
| 33 |
public boolean hasNext() { |
| 34 |
return entriesIter.hasNext(); |
| 35 |
} |
| 36 |
|
| 37 |
public WordData next() { |
| 38 |
final ByteBuffer entryBuffer = entriesIter.next(); |
| 39 |
|
| 40 |
/* |
| 41 |
* Entries are typically: inflected<SEP>codedBase<SEP>tag so try to find this split. |
| 42 |
*/ |
| 43 |
byte[] ba = entryBuffer.array(); |
| 44 |
int bbSize = entryBuffer.remaining(); |
| 45 |
|
| 46 |
int sepPos; |
| 47 |
for (sepPos = 0; sepPos < bbSize; sepPos++) { |
| 48 |
if (ba[sepPos] == separator) { |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (sepPos == bbSize) { |
| 54 |
throw new RuntimeException("Invalid dictionary " + "entry format (missing separator)."); |
| 55 |
} |
| 56 |
|
| 57 |
inflectedBuffer = BufferUtils.clearAndEnsureCapacity(inflectedBuffer, sepPos); |
| 58 |
inflectedBuffer.put(ba, 0, sepPos); |
| 59 |
inflectedBuffer.flip(); |
| 60 |
|
| 61 |
inflectedCharBuffer = BufferUtils.bytesToChars(decoder, inflectedBuffer, inflectedCharBuffer); |
| 62 |
entry.update(inflectedBuffer, inflectedCharBuffer); |
| 63 |
|
| 64 |
temp = BufferUtils.clearAndEnsureCapacity(temp, bbSize - sepPos); |
| 65 |
sepPos++; |
| 66 |
temp.put(ba, sepPos, bbSize - sepPos); |
| 67 |
temp.flip(); |
| 68 |
|
| 69 |
ba = temp.array(); |
| 70 |
bbSize = temp.remaining(); |
| 71 |
|
| 72 |
/* |
| 73 |
* Find the next separator byte's position splitting word form and tag. |
| 74 |
*/ |
| 75 |
assert sequenceEncoder.prefixBytes() <= bbSize : sequenceEncoder.getClass() + " >? " + bbSize; |
| 76 |
sepPos = sequenceEncoder.prefixBytes(); |
| 77 |
for (; sepPos < bbSize; sepPos++) { |
| 78 |
if (ba[sepPos] == separator) |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Decode the stem into stem buffer. |
| 84 |
*/ |
| 85 |
if (decodeStems) { |
| 86 |
entry.stemBuffer = sequenceEncoder.decode(entry.stemBuffer, |
| 87 |
inflectedBuffer, |
| 88 |
ByteBuffer.wrap(ba, 0, sepPos)); |
| 89 |
} else { |
| 90 |
entry.stemBuffer = BufferUtils.clearAndEnsureCapacity(entry.stemBuffer, sepPos); |
| 91 |
entry.stemBuffer.put(ba, 0, sepPos); |
| 92 |
entry.stemBuffer.flip(); |
| 93 |
} |
| 94 |
|
| 95 |
// Skip separator character, if present. |
| 96 |
if (sepPos + 1 <= bbSize) { |
| 97 |
sepPos++; |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* Decode the tag data. |
| 102 |
*/ |
| 103 |
entry.tagBuffer = BufferUtils.clearAndEnsureCapacity(entry.tagBuffer, bbSize - sepPos); |
| 104 |
entry.tagBuffer.put(ba, sepPos, bbSize - sepPos); |
| 105 |
entry.tagBuffer.flip(); |
| 106 |
|
| 107 |
return entry; |
| 108 |
} |
| 109 |
|
| 110 |
public void remove() { |
| 111 |
throw new UnsupportedOperationException(); |
| 112 |
} |
| 113 |
} |