1 package org.apache.lucene.search.spell;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.util.Iterator;
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;
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.
37 public class TestLuceneDictionary extends LuceneTestCase {
39 private Directory store;
41 private IndexReader indexReader = null;
42 private LuceneDictionary ld;
43 private Iterator<String> it;
46 public void setUp() throws Exception {
48 store = newDirectory();
49 IndexWriter writer = new IndexWriter(store, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
54 doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
55 writer.addDocument(doc);
58 doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
59 writer.addDocument(doc);
62 doc.add(new Field("contents", "Tom", Field.Store.YES, Field.Index.ANALYZED));
63 writer.addDocument(doc);
66 doc.add(new Field("contents", "Jerry", Field.Store.YES, Field.Index.ANALYZED));
67 writer.addDocument(doc);
70 doc.add(newField("zzz", "bar", Field.Store.YES, Field.Index.ANALYZED));
71 writer.addDocument(doc);
78 public void tearDown() throws Exception {
79 if (indexReader != null)
85 public void testFieldNonExistent() throws IOException {
87 indexReader = IndexReader.open(store, true);
89 ld = new LuceneDictionary(indexReader, "nonexistent_field");
90 it = ld.getWordsIterator();
92 assertFalse("More elements than expected", it.hasNext());
93 assertTrue("Nonexistent element is really null", it.next() == null);
95 if (indexReader != null) { indexReader.close(); }
99 public void testFieldAaa() throws IOException {
101 indexReader = IndexReader.open(store, true);
103 ld = new LuceneDictionary(indexReader, "aaa");
104 it = ld.getWordsIterator();
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);
111 if (indexReader != null) { indexReader.close(); }
115 public void testFieldContents_1() throws IOException {
117 indexReader = IndexReader.open(store, true);
119 ld = new LuceneDictionary(indexReader, "contents");
120 it = ld.getWordsIterator();
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);
129 ld = new LuceneDictionary(indexReader, "contents");
130 it = ld.getWordsIterator();
133 while (it.hasNext()) {
138 assertTrue("Number of words incorrect", counter == 0);
141 if (indexReader != null) { indexReader.close(); }
145 public void testFieldContents_2() throws IOException {
147 indexReader = IndexReader.open(store, true);
149 ld = new LuceneDictionary(indexReader, "contents");
150 it = ld.getWordsIterator();
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());
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);
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());
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);
173 if (indexReader != null) { indexReader.close(); }
177 public void testFieldZzz() throws IOException {
179 indexReader = IndexReader.open(store, true);
181 ld = new LuceneDictionary(indexReader, "zzz");
182 it = ld.getWordsIterator();
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);
190 if (indexReader != null) { indexReader.close(); }
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]);