001: package org.apache.lucene.search.spell;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.index.IndexReader;
021:
022: import java.util.Iterator;
023:
024: import org.apache.lucene.index.TermEnum;
025: import org.apache.lucene.index.Term;
026:
027: import java.io.*;
028:
029: /**
030: * Lucene Dictionary: terms taken from the given field
031: * of a Lucene index.
032: *
033: * When using IndexReader.terms(Term) the code must not call next() on TermEnum
034: * as the first call to TermEnum, see: http://issues.apache.org/jira/browse/LUCENE-6
035: *
036: *
037: *
038: */
039: public class LuceneDictionary implements Dictionary {
040: private IndexReader reader;
041: private String field;
042:
043: public LuceneDictionary(IndexReader reader, String field) {
044: this .reader = reader;
045: this .field = field.intern();
046: }
047:
048: public final Iterator getWordsIterator() {
049: return new LuceneIterator();
050: }
051:
052: final class LuceneIterator implements Iterator {
053: private TermEnum termEnum;
054: private Term actualTerm;
055: private boolean hasNextCalled;
056:
057: LuceneIterator() {
058: try {
059: termEnum = reader.terms(new Term(field, ""));
060: } catch (IOException e) {
061: throw new RuntimeException(e);
062: }
063: }
064:
065: public Object next() {
066: if (!hasNextCalled) {
067: hasNext();
068: }
069: hasNextCalled = false;
070:
071: try {
072: termEnum.next();
073: } catch (IOException e) {
074: throw new RuntimeException(e);
075: }
076:
077: return (actualTerm != null) ? actualTerm.text() : null;
078: }
079:
080: public boolean hasNext() {
081: if (hasNextCalled) {
082: return actualTerm != null;
083: }
084: hasNextCalled = true;
085:
086: actualTerm = termEnum.term();
087:
088: // if there are no words return false
089: if (actualTerm == null) {
090: return false;
091: }
092:
093: String currentField = actualTerm.field();
094:
095: // if the next word doesn't have the same field return false
096: if (currentField != field) {
097: actualTerm = null;
098: return false;
099: }
100:
101: return true;
102: }
103:
104: public void remove() {
105: throw new UnsupportedOperationException();
106: }
107: }
108: }
|