01: package org.apache.lucene.search.spell;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 java.util.Iterator;
21: import java.io.*;
22:
23: /**
24: * Dictionary represented by a text file.
25: *
26: * <p/>Format allowed: 1 word per line:<br/>
27: * word1<br/>
28: * word2<br/>
29: * word3<br/>
30: *
31: *
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: * Create a dictionary based on a reader. Used by the test case.
49: */
50: protected PlainTextDictionary(Reader reader) {
51: in = new BufferedReader(reader);
52: }
53:
54: public Iterator getWordsIterator() {
55: return new fileIterator();
56: }
57:
58: final class fileIterator implements Iterator {
59: public Object 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: }
|