|
25 | 25 | import java.nio.charset.StandardCharsets;
|
26 | 26 | import java.util.ArrayList;
|
27 | 27 | import java.util.Collections;
|
| 28 | +import java.util.Iterator; |
28 | 29 | import java.util.List;
|
| 30 | +import java.util.NoSuchElementException; |
| 31 | +import java.util.Spliterator; |
| 32 | +import java.util.Spliterators; |
29 | 33 | import java.util.function.Predicate;
|
30 | 34 | import java.util.stream.IntStream;
|
| 35 | +import java.util.stream.Stream; |
| 36 | +import java.util.stream.StreamSupport; |
31 | 37 |
|
32 | 38 | public class JapaneseDictionary implements Dictionary, DictionaryAccess {
|
33 | 39 |
|
@@ -127,6 +133,45 @@ public void close() throws IOException {
|
127 | 133 | }
|
128 | 134 | }
|
129 | 135 |
|
| 136 | + /** |
| 137 | + * Iterator of morphemes in the dictionary. |
| 138 | + */ |
| 139 | + private class EntryItr implements Iterator<Morpheme> { |
| 140 | + private final GrammarImpl grammar; |
| 141 | + private final LexiconSet lexicon; |
| 142 | + private Iterator<Integer> wordIdItr; |
| 143 | + |
| 144 | + EntryItr() { |
| 145 | + this.grammar = getGrammar(); |
| 146 | + this.lexicon = getLexicon(); |
| 147 | + this.wordIdItr = this.lexicon.wordIds(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean hasNext() { |
| 152 | + return wordIdItr.hasNext(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public Morpheme next() { |
| 157 | + if (!hasNext()) { |
| 158 | + throw new NoSuchElementException(); |
| 159 | + } |
| 160 | + return new SingleMorphemeImpl(this.grammar, this.lexicon, wordIdItr.next()); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Stream<Morpheme> entries() { |
| 166 | + Iterator<Morpheme> iterator = new EntryItr(); |
| 167 | + int size = getLexicon().size(); |
| 168 | + int characteristics = Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.SIZED; |
| 169 | + boolean parallel = true; |
| 170 | + |
| 171 | + Spliterator<Morpheme> spliterator = Spliterators.spliterator(iterator, size, characteristics); |
| 172 | + return StreamSupport.stream(spliterator, parallel); |
| 173 | + } |
| 174 | + |
130 | 175 | @Override
|
131 | 176 | public List<Morpheme> lookup(CharSequence surface) {
|
132 | 177 | UTF8InputTextBuilder builder = new UTF8InputTextBuilder(surface, grammar);
|
@@ -204,10 +249,12 @@ static String readAll(InputStream input) throws IOException {
|
204 | 249 | }
|
205 | 250 | }
|
206 | 251 |
|
| 252 | + @Override |
207 | 253 | public GrammarImpl getGrammar() {
|
208 | 254 | return grammar;
|
209 | 255 | }
|
210 | 256 |
|
| 257 | + @Override |
211 | 258 | public LexiconSet getLexicon() {
|
212 | 259 | return lexicon;
|
213 | 260 | }
|
|
0 commit comments