1 package org.apache.lucene.search.spell;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.index.IndexReader;
22 import java.util.Iterator;
24 import org.apache.lucene.index.TermEnum;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.util.StringHelper;
31 * Lucene Dictionary: terms taken from the given field
34 * When using IndexReader.terms(Term) the code must not call next() on TermEnum
35 * as the first call to TermEnum, see: http://issues.apache.org/jira/browse/LUCENE-6
40 public class LuceneDictionary implements Dictionary {
41 private IndexReader reader;
44 public LuceneDictionary(IndexReader reader, String field) {
46 this.field = StringHelper.intern(field);
49 public final Iterator<String> getWordsIterator() {
50 return new LuceneIterator();
54 final class LuceneIterator implements Iterator<String> {
55 private TermEnum termEnum;
56 private Term actualTerm;
57 private boolean hasNextCalled;
61 termEnum = reader.terms(new Term(field));
62 } catch (IOException e) {
63 throw new RuntimeException(e);
67 public String next() {
71 hasNextCalled = false;
75 } catch (IOException e) {
76 throw new RuntimeException(e);
79 return (actualTerm != null) ? actualTerm.text() : null;
82 public boolean hasNext() {
84 return actualTerm != null;
88 actualTerm = termEnum.term();
90 // if there are no words return false
91 if (actualTerm == null) {
95 String currentField = actualTerm.field();
97 // if the next word doesn't have the same field return false
98 if (currentField != field) {
106 public void remove() {
107 throw new UnsupportedOperationException();