public class FSTCompletion
extends java.lang.Object
FSTCompletionBuilder
Modifier and Type | Class and Description |
---|---|
static class |
FSTCompletion.Completion
A single completion for a given key.
|
Modifier and Type | Field and Description |
---|---|
private FST<java.lang.Object> |
automaton
Finite state automaton encoding all the lookup terms.
|
static int |
DEFAULT_BUCKETS
Default number of buckets.
|
private static java.util.ArrayList<FSTCompletion.Completion> |
EMPTY_RESULT
An empty result.
|
private boolean |
exactFirst |
private boolean |
higherWeightsFirst |
private FST.Arc<java.lang.Object>[] |
rootArcs
An array of arcs leaving the root automaton state and encoding weights of
all completions in their sub-trees.
|
Constructor and Description |
---|
FSTCompletion(FST<java.lang.Object> automaton)
Defaults to higher weights first and exact first.
|
FSTCompletion(FST<java.lang.Object> automaton,
boolean higherWeightsFirst,
boolean exactFirst)
Constructs an FSTCompletion, specifying higherWeightsFirst and exactFirst.
|
Modifier and Type | Method and Description |
---|---|
private static FST.Arc<java.lang.Object>[] |
cacheRootArcs(FST<java.lang.Object> automaton)
Cache the root node's output arcs starting with completions with the
highest weights.
|
private boolean |
checkExistingAndReorder(java.util.ArrayList<FSTCompletion.Completion> list,
BytesRef key)
Checks if the list of
Lookup.LookupResult s already has a
key . |
private boolean |
collect(java.util.List<FSTCompletion.Completion> res,
int num,
int bucket,
BytesRef output,
FST.Arc<java.lang.Object> arc)
Recursive collect lookup results from the automaton subgraph starting at
arc . |
private boolean |
descendWithPrefix(FST.Arc<java.lang.Object> arc,
BytesRef utf8)
Descend along the path starting at
arc and going through bytes
in the argument. |
int |
getBucket(java.lang.CharSequence key)
Returns the bucket assigned to a given key (if found) or
-1 if
no exact match exists. |
int |
getBucketCount()
Returns the bucket count (discretization thresholds).
|
private int |
getExactMatchStartingFromRootArc(int rootArcIndex,
BytesRef utf8)
Returns the first exact match by traversing root arcs, starting from the
arc
rootArcIndex . |
FST<java.lang.Object> |
getFST()
Returns the internal automaton.
|
java.util.List<FSTCompletion.Completion> |
lookup(java.lang.CharSequence key,
int num)
Lookup suggestions to
key . |
private java.util.List<FSTCompletion.Completion> |
lookupSortedAlphabetically(BytesRef key,
int num)
Lookup suggestions sorted alphabetically if weights are not
constant.
|
private java.util.ArrayList<FSTCompletion.Completion> |
lookupSortedByWeight(BytesRef key,
int num,
boolean collectAll)
Lookup suggestions sorted by weight (descending order).
|
public static final int DEFAULT_BUCKETS
private static final java.util.ArrayList<FSTCompletion.Completion> EMPTY_RESULT
ArrayList
to keep all the returned
lists of single type (monomorphic calls).private final FST<java.lang.Object> automaton
private final FST.Arc<java.lang.Object>[] rootArcs
private boolean exactFirst
FSTCompletion(FST, boolean, boolean)
private boolean higherWeightsFirst
FSTCompletion(FST, boolean, boolean)
public FSTCompletion(FST<java.lang.Object> automaton, boolean higherWeightsFirst, boolean exactFirst)
automaton
- Automaton with completions. See FSTCompletionBuilder
.higherWeightsFirst
- Return most popular suggestions first. This is the default
behavior for this implementation. Setting it to false
has no effect (use constant term weights to sort alphabetically
only).exactFirst
- Find and push an exact match to the first position of the result
list if found.public FSTCompletion(FST<java.lang.Object> automaton)
FSTCompletion(FST, boolean, boolean)
private static FST.Arc<java.lang.Object>[] cacheRootArcs(FST<java.lang.Object> automaton)
private int getExactMatchStartingFromRootArc(int rootArcIndex, BytesRef utf8)
rootArcIndex
.rootArcIndex
- The first root arc index in rootArcs
to consider when
matching.utf8
- The sequence of utf8 bytes to follow.-1
if no
match was found.public java.util.List<FSTCompletion.Completion> lookup(java.lang.CharSequence key, int num)
key
.key
- The prefix to which suggestions should be sought.num
- At most this number of suggestions will be returned.private java.util.List<FSTCompletion.Completion> lookupSortedAlphabetically(BytesRef key, int num) throws java.io.IOException
java.io.IOException
private java.util.ArrayList<FSTCompletion.Completion> lookupSortedByWeight(BytesRef key, int num, boolean collectAll) throws java.io.IOException
collectAll
- If true
, the routine terminates immediately when
num
suggestions have been collected. If
false
, it will collect suggestions from all weight
arcs (needed for lookupSortedAlphabetically(org.apache.lucene.util.BytesRef, int)
.java.io.IOException
private boolean checkExistingAndReorder(java.util.ArrayList<FSTCompletion.Completion> list, BytesRef key)
Lookup.LookupResult
s already has a
key
. If so, reorders that
Lookup.LookupResult
to the first
position.true if and only if list
contained
key
.
private boolean descendWithPrefix(FST.Arc<java.lang.Object> arc, BytesRef utf8) throws java.io.IOException
arc
and going through bytes
in the argument.arc
- The starting arc. This argument is modified in-place.utf8
- The term to descend along.true
, arc
will be set to the arc
matching last byte of term
. false
is
returned if no such prefix exists.java.io.IOException
private boolean collect(java.util.List<FSTCompletion.Completion> res, int num, int bucket, BytesRef output, FST.Arc<java.lang.Object> arc) throws java.io.IOException
arc
.num
- Maximum number of results needed (early termination).java.io.IOException
public int getBucketCount()
public int getBucket(java.lang.CharSequence key)
-1
if
no exact match exists.public FST<java.lang.Object> getFST()