1 package org.apache.lucene.search.suggest;
4 import java.io.IOException;
5 import java.util.Iterator;
8 import org.apache.lucene.search.spell.Dictionary;
9 import org.apache.lucene.search.spell.TermFreqIterator;
10 import org.apache.lucene.util.PriorityQueue;
12 public abstract class Lookup {
16 public static final class LookupResult implements Comparable<LookupResult> {
17 public final String key;
18 public final float value;
20 public LookupResult(String key, float value) {
26 public String toString() {
27 return key + "/" + value;
30 /** Compare alphabetically. */
31 public int compareTo(LookupResult o) {
32 return this.key.compareTo(o.key);
36 public static final class LookupPriorityQueue extends PriorityQueue<LookupResult> {
38 public LookupPriorityQueue(int size) {
43 protected boolean lessThan(LookupResult a, LookupResult b) {
44 return a.value < b.value;
47 public LookupResult[] getResults() {
49 LookupResult[] res = new LookupResult[size];
50 for (int i = size - 1; i >= 0; i--) {
57 /** Build lookup from a dictionary. Some implementations may require sorted
58 * or unsorted keys from the dictionary's iterator - use
59 * {@link SortedTermFreqIteratorWrapper} or
60 * {@link UnsortedTermFreqIteratorWrapper} in such case.
62 public void build(Dictionary dict) throws IOException {
63 Iterator<String> it = dict.getWordsIterator();
64 TermFreqIterator tfit;
65 if (it instanceof TermFreqIterator) {
66 tfit = (TermFreqIterator)it;
68 tfit = new TermFreqIterator.TermFreqIteratorWrapper(it);
73 public abstract void build(TermFreqIterator tfit) throws IOException;
76 * Persist the constructed lookup data to a directory. Optional operation.
77 * @param storeDir directory where data can be stored.
78 * @return true if successful, false if unsuccessful or not supported.
79 * @throws IOException when fatal IO error occurs.
81 public abstract boolean store(File storeDir) throws IOException;
84 * Discard current lookup data and load it from a previously saved copy.
86 * @param storeDir directory where lookup data was stored.
87 * @return true if completed successfully, false if unsuccessful or not supported.
88 * @throws IOException when fatal IO error occurs.
90 public abstract boolean load(File storeDir) throws IOException;
93 * Look up a key and return possible completion for this key.
94 * @param key lookup key. Depending on the implementation this may be
95 * a prefix, misspelling, or even infix.
96 * @param onlyMorePopular return only more popular results
97 * @param num maximum number of results to return
98 * @return a list of possible completions, with their relative weight (e.g. popularity)
100 public abstract List<LookupResult> lookup(String key, boolean onlyMorePopular, int num);
103 * Modify the lookup data by recording additional data. Optional operation.
104 * @param key new lookup key
105 * @param value value to associate with this key
106 * @return true if new key is added, false if it already exists or operation
109 public abstract boolean add(String key, Object value);
112 * Get value associated with a specific key.
113 * @param key lookup key
114 * @return associated value
116 public abstract Object get(String key);