pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spellchecker / src / java / org / apache / lucene / search / spell / LuceneDictionary.java
1 package org.apache.lucene.search.spell;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.index.IndexReader;
21
22 import java.util.Iterator;
23
24 import org.apache.lucene.index.TermEnum;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.util.StringHelper;
27
28 import java.io.*;
29
30 /**
31  * Lucene Dictionary: terms taken from the given field
32  * of a Lucene index.
33  *
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
36  *
37  *
38  *
39  */
40 public class LuceneDictionary implements Dictionary {
41   private IndexReader reader;
42   private String field;
43
44   public LuceneDictionary(IndexReader reader, String field) {
45     this.reader = reader;
46     this.field = StringHelper.intern(field);
47   }
48
49   public final Iterator<String> getWordsIterator() {
50     return new LuceneIterator();
51   }
52
53
54   final class LuceneIterator implements Iterator<String> {
55     private TermEnum termEnum;
56     private Term actualTerm;
57     private boolean hasNextCalled;
58
59     LuceneIterator() {
60       try {
61         termEnum = reader.terms(new Term(field));
62       } catch (IOException e) {
63         throw new RuntimeException(e);
64       }
65     }
66
67     public String next() {
68       if (!hasNextCalled) {
69         hasNext();
70       }
71       hasNextCalled = false;
72
73       try {
74         termEnum.next();
75       } catch (IOException e) {
76         throw new RuntimeException(e);
77       }
78
79       return (actualTerm != null) ? actualTerm.text() : null;
80     }
81
82     public boolean hasNext() {
83       if (hasNextCalled) {
84         return actualTerm != null;
85       }
86       hasNextCalled = true;
87
88       actualTerm = termEnum.term();
89
90       // if there are no words return false
91       if (actualTerm == null) {
92         return false;
93       }
94
95       String currentField = actualTerm.field();
96
97       // if the next word doesn't have the same field return false
98       if (currentField != field) {
99         actualTerm = null;
100         return false;
101       }
102
103       return true;
104     }
105
106     public void remove() {
107       throw new UnsupportedOperationException();
108     }
109   }
110 }