1 package org.apache.lucene.search.suggest.fst;
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.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.Random;
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;
30 import org.apache.lucene.search.suggest.LookupBenchmarkTest;
31 import org.apache.lucene.search.suggest.TermFreq;
32 import org.apache.lucene.search.suggest.TermFreqArrayIterator;
35 * Unit tests for {@link FSTLookup}.
37 public class FSTLookupTest extends LuceneTestCase {
38 public static TermFreq tf(String t, float v) {
39 return new TermFreq(t, v);
42 private FSTLookup lookup;
44 public void setUp() throws Exception {
46 final TermFreq[] keys = new TermFreq[] {
65 lookup = new FSTLookup();
66 lookup.build(new TermFreqArrayIterator(keys));
69 public void testExactMatchHighPriority() throws Exception {
70 assertMatchEquals(lookup.lookup("two", true, 1), "two/1.0");
73 public void testExactMatchLowPriority() throws Exception {
74 assertMatchEquals(lookup.lookup("one", true, 2),
79 public void testMiss() throws Exception {
80 assertMatchEquals(lookup.lookup("xyz", true, 1));
83 public void testAlphabeticWithWeights() throws Exception {
84 assertEquals(0, lookup.lookup("xyz", false, 1).size());
87 public void testFullMatchList() throws Exception {
88 assertMatchEquals(lookup.lookup("one", true, Integer.MAX_VALUE),
95 public void testMultilingualInput() throws Exception {
96 List<TermFreq> input = LookupBenchmarkTest.readTop50KWiki();
98 lookup = new FSTLookup();
99 lookup.build(new TermFreqArrayIterator(input));
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);
107 public void testEmptyInput() throws Exception {
108 lookup = new FSTLookup();
109 lookup.build(new TermFreqArrayIterator(new TermFreq[0]));
111 assertMatchEquals(lookup.lookup("", true, 10));
114 public void testRandom() throws Exception {
115 List<TermFreq> freqs = new ArrayList<TermFreq>();
117 for (int i = 0; i < 5000; i++) {
118 freqs.add(new TermFreq("" + rnd.nextLong(), rnd.nextInt(100)));
120 lookup = new FSTLookup();
121 lookup.build(new TermFreqArrayIterator(freqs.toArray(new TermFreq[freqs.size()])));
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));
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();
139 if (!Arrays.equals(expected, result)) {
140 int colLen = Math.max(maxLen(expected), maxLen(result));
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] : "--"));
151 System.err.println(b.toString());
152 fail("Expected different output:\n" + b.toString());
156 private int maxLen(String[] result) {
158 for (String s : result)
159 len = Math.max(len, s.length());