pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spellchecker / src / test / org / apache / lucene / search / spell / TestLuceneDictionary.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 java.io.IOException;
21 import java.util.Iterator;
22
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.analysis.MockTokenizer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.IndexReader;
28 import org.apache.lucene.index.IndexWriter;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31
32 /**
33  * Test case for LuceneDictionary.
34  * It first creates a simple index and then a couple of instances of LuceneDictionary
35  * on different fields and checks if all the right text comes back.
36  */
37 public class TestLuceneDictionary extends LuceneTestCase {
38
39   private Directory store;
40
41   private IndexReader indexReader = null;
42   private LuceneDictionary ld;
43   private Iterator<String> it;
44
45   @Override
46   public void setUp() throws Exception {
47     super.setUp();
48     store = newDirectory();
49     IndexWriter writer = new IndexWriter(store, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
50
51     Document doc;
52
53     doc = new  Document();
54     doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
55     writer.addDocument(doc);
56
57     doc = new  Document();
58     doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
59     writer.addDocument(doc);
60
61     doc = new  Document();
62     doc.add(new  Field("contents", "Tom", Field.Store.YES, Field.Index.ANALYZED));
63     writer.addDocument(doc);
64
65     doc = new  Document();
66     doc.add(new  Field("contents", "Jerry", Field.Store.YES, Field.Index.ANALYZED));
67     writer.addDocument(doc);
68
69     doc = new Document();
70     doc.add(newField("zzz", "bar", Field.Store.YES, Field.Index.ANALYZED));
71     writer.addDocument(doc);
72
73     writer.forceMerge(1);
74     writer.close();
75   }
76
77   @Override
78   public void tearDown() throws Exception {
79     if (indexReader != null)
80       indexReader.close();
81     store.close();
82     super.tearDown();
83   }
84   
85   public void testFieldNonExistent() throws IOException {
86     try {
87       indexReader = IndexReader.open(store, true);
88
89       ld = new LuceneDictionary(indexReader, "nonexistent_field");
90       it = ld.getWordsIterator();
91
92       assertFalse("More elements than expected", it.hasNext());
93       assertTrue("Nonexistent element is really null", it.next() == null);
94     } finally {
95       if  (indexReader != null) { indexReader.close(); }
96     }
97   }
98
99   public void testFieldAaa() throws IOException {
100     try {
101       indexReader = IndexReader.open(store, true);
102
103       ld = new LuceneDictionary(indexReader, "aaa");
104       it = ld.getWordsIterator();
105
106       assertTrue("First element doesn't exist.", it.hasNext());
107       assertTrue("First element isn't correct", it.next().equals("foo"));
108       assertFalse("More elements than expected", it.hasNext());
109       assertTrue("Nonexistent element is really null", it.next() == null);
110     } finally {
111       if  (indexReader != null) { indexReader.close(); }
112     }
113   }
114
115   public void testFieldContents_1() throws IOException {
116     try {
117       indexReader = IndexReader.open(store, true);
118
119       ld = new LuceneDictionary(indexReader, "contents");
120       it = ld.getWordsIterator();
121
122       assertTrue("First element doesn't exist.", it.hasNext());
123       assertTrue("First element isn't correct", it.next().equals("Jerry"));
124       assertTrue("Second element doesn't exist.", it.hasNext());
125       assertTrue("Second element isn't correct", it.next().equals("Tom"));
126       assertFalse("More elements than expected", it.hasNext());
127       assertTrue("Nonexistent element is really null", it.next() == null);
128
129       ld = new LuceneDictionary(indexReader, "contents");
130       it = ld.getWordsIterator();
131
132       int counter = 2;
133       while (it.hasNext()) {
134         it.next();
135         counter--;
136       }
137
138       assertTrue("Number of words incorrect", counter == 0);
139     }
140     finally {
141       if  (indexReader != null) { indexReader.close(); }
142     }
143   }
144
145   public void testFieldContents_2() throws IOException {
146     try {
147       indexReader = IndexReader.open(store, true);
148
149       ld = new LuceneDictionary(indexReader, "contents");
150       it = ld.getWordsIterator();
151
152       // hasNext() should have no side effects
153       assertTrue("First element isn't were it should be.", it.hasNext());
154       assertTrue("First element isn't were it should be.", it.hasNext());
155       assertTrue("First element isn't were it should be.", it.hasNext());
156
157       // just iterate through words
158       assertTrue("First element isn't correct", it.next().equals("Jerry"));
159       assertTrue("Second element isn't correct", it.next().equals("Tom"));
160       assertTrue("Nonexistent element is really null", it.next() == null);
161
162       // hasNext() should still have no side effects ...
163       assertFalse("There should be any more elements", it.hasNext());
164       assertFalse("There should be any more elements", it.hasNext());
165       assertFalse("There should be any more elements", it.hasNext());
166
167       // .. and there are really no more words
168       assertTrue("Nonexistent element is really null", it.next() == null);
169       assertTrue("Nonexistent element is really null", it.next() == null);
170       assertTrue("Nonexistent element is really null", it.next() == null);
171     }
172     finally {
173       if  (indexReader != null) { indexReader.close(); }
174     }
175   }
176
177   public void testFieldZzz() throws IOException {
178     try {
179       indexReader = IndexReader.open(store, true);
180
181       ld = new LuceneDictionary(indexReader, "zzz");
182       it = ld.getWordsIterator();
183
184       assertTrue("First element doesn't exist.", it.hasNext());
185       assertTrue("First element isn't correct", it.next().equals("bar"));
186       assertFalse("More elements than expected", it.hasNext());
187       assertTrue("Nonexistent element is really null", it.next() == null);
188     }
189     finally {
190       if  (indexReader != null) { indexReader.close(); }
191     }
192   }
193   
194   public void testSpellchecker() throws IOException {
195     Directory dir = newDirectory();
196     SpellChecker sc = new SpellChecker(dir);
197     indexReader = IndexReader.open(store, true);
198     sc.indexDictionary(new LuceneDictionary(indexReader, "contents"), newIndexWriterConfig(TEST_VERSION_CURRENT, null), false);
199     String[] suggestions = sc.suggestSimilar("Tam", 1);
200     assertEquals(1, suggestions.length);
201     assertEquals("Tom", suggestions[0]);
202     suggestions = sc.suggestSimilar("Jarry", 1);
203     assertEquals(1, suggestions.length);
204     assertEquals("Jerry", suggestions[0]);
205     indexReader.close();
206     sc.close();
207     dir.close();
208   }
209   
210 }