pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spellchecker / src / java / org / apache / lucene / search / spell / PlainTextDictionary.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
21 import java.util.Iterator;
22 import java.io.*;
23
24
25 /**
26  * Dictionary represented by a text file.
27  * 
28  * <p/>Format allowed: 1 word per line:<br/>
29  * word1<br/>
30  * word2<br/>
31  * word3<br/>
32  */
33 public class PlainTextDictionary implements Dictionary {
34
35   private BufferedReader in;
36   private String line;
37   private boolean hasNextCalled;
38
39   public PlainTextDictionary(File file) throws FileNotFoundException {
40     in = new BufferedReader(new FileReader(file));
41   }
42
43   public PlainTextDictionary(InputStream dictFile) {
44     in = new BufferedReader(new InputStreamReader(dictFile));
45   }
46
47   /**
48    * Creates a dictionary based on a reader.
49    */
50   public PlainTextDictionary(Reader reader) {
51     in = new BufferedReader(reader);
52   }
53
54   public Iterator<String> getWordsIterator() {
55     return new fileIterator();
56   }
57
58   final class fileIterator implements Iterator<String> {
59     public String next() {
60       if (!hasNextCalled) {
61         hasNext();
62       }
63       hasNextCalled = false;
64       return line;
65     }
66
67     public boolean hasNext() {
68       hasNextCalled = true;
69       try {
70         line = in.readLine();
71       } catch (IOException ex) {
72         throw new RuntimeException(ex);
73       }
74       return (line != null) ? true : false;
75     }
76
77     public void remove() {
78       throw new UnsupportedOperationException();
79     }
80   }
81
82 }