add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spellchecker / src / java / org / apache / lucene / search / suggest / Lookup.java
1 package org.apache.lucene.search.suggest;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.apache.lucene.search.spell.Dictionary;
9 import org.apache.lucene.search.spell.TermFreqIterator;
10 import org.apache.lucene.util.PriorityQueue;
11
12 public abstract class Lookup {
13   /**
14    * Result of a lookup.
15    */
16   public static final class LookupResult implements Comparable<LookupResult> {
17     public final String key;
18     public final float value;
19     
20     public LookupResult(String key, float value) {
21       this.key = key;
22       this.value = value;
23     }
24     
25     @Override
26     public String toString() {
27       return key + "/" + value;
28     }
29
30     /** Compare alphabetically. */
31     public int compareTo(LookupResult o) {
32       return this.key.compareTo(o.key);
33     }
34   }
35   
36   public static final class LookupPriorityQueue extends PriorityQueue<LookupResult> {
37     
38     public LookupPriorityQueue(int size) {
39       initialize(size);
40     }
41
42     @Override
43     protected boolean lessThan(LookupResult a, LookupResult b) {
44       return a.value < b.value;
45     }
46     
47     public LookupResult[] getResults() {
48       int size = size();
49       LookupResult[] res = new LookupResult[size];
50       for (int i = size - 1; i >= 0; i--) {
51         res[i] = pop();
52       }
53       return res;
54     }
55   }
56   
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.
61    */
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;
67     } else {
68       tfit = new TermFreqIterator.TermFreqIteratorWrapper(it);
69     }
70     build(tfit);
71   }
72   
73   public abstract void build(TermFreqIterator tfit) throws IOException;
74   
75   /**
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.
80    */
81   public abstract boolean store(File storeDir) throws IOException;
82
83   /**
84    * Discard current lookup data and load it from a previously saved copy.
85    * Optional operation.
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.
89    */
90   public abstract boolean load(File storeDir) throws IOException;
91   
92   /**
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)
99    */
100   public abstract List<LookupResult> lookup(String key, boolean onlyMorePopular, int num);
101
102   /**
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
107    * is not supported.
108    */
109   public abstract boolean add(String key, Object value);
110   
111   /**
112    * Get value associated with a specific key.
113    * @param key lookup key
114    * @return associated value
115    */
116   public abstract Object get(String key);  
117 }