add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spellchecker / src / test / org / apache / lucene / search / suggest / fst / FSTLookupTest.java
1 package org.apache.lucene.search.suggest.fst;
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.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.Random;
25
26 import org.apache.lucene.search.suggest.Lookup.LookupResult;
27 import org.apache.lucene.search.suggest.fst.FSTLookup;
28 import org.apache.lucene.util.LuceneTestCase;
29
30 import org.apache.lucene.search.suggest.LookupBenchmarkTest;
31 import org.apache.lucene.search.suggest.TermFreq;
32 import org.apache.lucene.search.suggest.TermFreqArrayIterator;
33
34 /**
35  * Unit tests for {@link FSTLookup}.
36  */
37 public class FSTLookupTest extends LuceneTestCase {
38   public static TermFreq tf(String t, float v) {
39     return new TermFreq(t, v);
40   }
41
42   private FSTLookup lookup;
43
44   public void setUp() throws Exception {
45     super.setUp();
46     final TermFreq[] keys = new TermFreq[] {
47         tf("one", 0.5f),
48         tf("oneness", 1),
49         tf("onerous", 1),
50         tf("onesimus", 1),
51         tf("two", 1),
52         tf("twofold", 1),
53         tf("twonk", 1),
54         tf("thrive", 1),
55         tf("through", 1),
56         tf("threat", 1),
57         tf("three", 1),
58         tf("foundation", 1),
59         tf("fourier", 1),
60         tf("four", 1),
61         tf("fourty", 1),
62         tf("xo", 1),
63       };
64
65       lookup = new FSTLookup();
66       lookup.build(new TermFreqArrayIterator(keys));
67   }
68
69   public void testExactMatchHighPriority() throws Exception {
70     assertMatchEquals(lookup.lookup("two", true, 1), "two/1.0");
71   }
72
73   public void testExactMatchLowPriority() throws Exception {
74     assertMatchEquals(lookup.lookup("one", true, 2), 
75         "one/0.0",
76         "oneness/1.0");
77   }
78
79   public void testMiss() throws Exception {
80     assertMatchEquals(lookup.lookup("xyz", true, 1));
81   }
82
83   public void testAlphabeticWithWeights() throws Exception {
84     assertEquals(0, lookup.lookup("xyz", false, 1).size());
85   }
86
87   public void testFullMatchList() throws Exception {
88     assertMatchEquals(lookup.lookup("one", true, Integer.MAX_VALUE),
89         "oneness/1.0", 
90         "onerous/1.0",
91         "onesimus/1.0", 
92         "one/0.0");
93   }
94
95   public void testMultilingualInput() throws Exception {
96     List<TermFreq> input = LookupBenchmarkTest.readTop50KWiki();
97
98     lookup = new FSTLookup();
99     lookup.build(new TermFreqArrayIterator(input));
100
101     for (TermFreq tf : input) {
102       assertTrue("Not found: " + tf.term, lookup.get(tf.term) != null);
103       assertEquals(tf.term, lookup.lookup(tf.term, true, 1).get(0).key);
104     }
105   }
106
107   public void testEmptyInput() throws Exception {
108     lookup = new FSTLookup();
109     lookup.build(new TermFreqArrayIterator(new TermFreq[0]));
110     
111     assertMatchEquals(lookup.lookup("", true, 10));
112   }
113
114   public void testRandom() throws Exception {
115     List<TermFreq> freqs = new ArrayList<TermFreq>();
116     Random rnd = random;
117     for (int i = 0; i < 5000; i++) {
118       freqs.add(new TermFreq("" + rnd.nextLong(), rnd.nextInt(100)));
119     }
120     lookup = new FSTLookup();
121     lookup.build(new TermFreqArrayIterator(freqs.toArray(new TermFreq[freqs.size()])));
122
123     for (TermFreq tf : freqs) {
124       final String term = tf.term;
125       for (int i = 1; i < term.length(); i++) {
126         String prefix = term.substring(0, i);
127         for (LookupResult lr : lookup.lookup(prefix, true, 10)) {
128           assertTrue(lr.key.startsWith(prefix));
129         }
130       }
131     }
132   }
133
134   private void assertMatchEquals(List<LookupResult> res, String... expected) {
135     String [] result = new String [res.size()];
136     for (int i = 0; i < res.size(); i++)
137       result[i] = res.get(i).toString();
138     
139     if (!Arrays.equals(expected, result)) {
140       int colLen = Math.max(maxLen(expected), maxLen(result));
141       
142       StringBuilder b = new StringBuilder();
143       String format = "%" + colLen + "s  " + "%" + colLen + "s\n"; 
144       b.append(String.format(Locale.ENGLISH, format, "Expected", "Result"));
145       for (int i = 0; i < Math.max(result.length, expected.length); i++) {
146         b.append(String.format(Locale.ENGLISH, format, 
147             i < expected.length ? expected[i] : "--", 
148             i < result.length ? result[i] : "--"));
149       }
150
151       System.err.println(b.toString());
152       fail("Expected different output:\n" + b.toString());
153     }
154   }
155
156   private int maxLen(String[] result) {
157     int len = 0;
158     for (String s : result)
159       len = Math.max(len, s.length());
160     return len;
161   }
162 }