Package org.apache.lucene.util.fst
package org.apache.lucene.util.fst
Finite state transducers
This package implements Finite State Transducers with the following characteristics:
- Fast and low memory overhead construction of the minimal FST (but inputs must be provided in sorted order)
- Low object overhead and quick deserialization (byte[] representation)
- Optional two-pass compression:
FST.pack() Lookup-by-outputwhen the outputs are in sorted order (e.g., ordinals or file pointers)- Pluggable
Outputsrepresentation N-shortest-pathssearch by weight- Enumerators (
IntsRefandBytesRef) that behave likeSortedMapiterators
FST Construction example:
// Input values (keys). These must be provided to Builder in Unicode sorted order!
String inputValues[] = {"cat", "dog", "dogs"};
long outputValues[] = {5, 7, 12};
PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton();
Builder<Long> builder = new Builder<Long>(INPUT_TYPE.BYTE1, outputs);
BytesRef scratchBytes = new BytesRef();
IntsRef scratchInts = new IntsRef();
for (int i = 0; i < inputValues.length; i++) {
scratchBytes.copyChars(inputValues[i]);
builder.add(Util.toIntsRef(scratchBytes, scratchInts), outputValues[i]);
}
FST<Long> fst = builder.finish();
Retrieval by key:
Long value = Util.get(fst, new BytesRef("dog"));
System.out.println(value); // 7
Retrieval by value:
// Only works because outputs are also in sorted order
IntsRef key = Util.getByOutput(fst, 12);
System.out.println(Util.toBytesRef(key, scratchBytes).utf8ToString()); // dogs
Iterate over key-value pairs in sorted order:
// Like TermsEnum, this also supports seeking (advance)
BytesRefFSTEnum<Long> iterator = new BytesRefFSTEnum<Long>(fst);
while (iterator.next() != null) {
InputOutput<Long> mapEntry = iterator.current();
System.out.println(mapEntry.input.utf8ToString());
System.out.println(mapEntry.output);
}
N-shortest paths by weight:
Comparator<Long> comparator = new Comparator<Long>() {
public int compare(Long left, Long right) {
return left.compareTo(right);
}
};
Arc<Long> firstArc = fst.getFirstArc(new Arc<Long>());
MinResult<Long> paths[] = Util.shortestPaths(fst, firstArc, comparator, 2);
System.out.println(Util.toBytesRef(paths[0].input, scratchBytes).utf8ToString()); // cat
System.out.println(paths[0].output); // 5
System.out.println(Util.toBytesRef(paths[1].input, scratchBytes).utf8ToString()); // dog
System.out.println(paths[1].output); // 7
-
ClassDescriptionBuilder<T>Builds a minimal FST (maps an IntsRef term to an arbitrary output) from pre-sorted terms with outputs.Builder.Arc<T>Expert: holds a pending (seen but not yet serialized) arc.Expert: this is invoked by Builder whenever a suffix is serialized.Expert: holds a pending (seen but not yet serialized) Node.An FST
Outputsimplementation where each output is a sequence of bytes.Enumerates all input (BytesRef) + output pairs in an FST.Holds a single input (BytesRef) + output pair.An FSTOutputsimplementation where each output is a sequence of characters.FST<T>Represents an finite state machine (FST), using a compact byte[] format.FST.Arc<T>Represents a single arc.Reads bytes stored in an FST.Specifies allowed range of each int input label for this FST.An FSTOutputsimplementation where each output is a sequence of ints.Enumerates all input (IntsRef) + output pairs in an FST.Holds a single input (IntsRef) + output pair.Wraps another Outputs implementation and encodes one or more of its output values.A null FSTOutputsimplementation; use this if you just want to build an FSA.Outputs<T>Represents the outputs for an FST, providing the basic algebra required for building and traversing the FST.PairOutputs<A,B> An FSTOutputsimplementation, holding two other outputs.PairOutputs.Pair<A,B> Holds a single pair of two outputs.An FSTOutputsimplementation where each output is a non-negative long value.An FSTOutputsimplementation where each output is one or two non-negative long values.Holds two long outputs.Static helper methods.Util.FSTPath<T>Represents a path in TopNSearcher.Holds a single input (IntsRef) + output, returned byshortestPaths().Utility class to find top N shortest paths from start point(s).