public final class Util
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Util.FSTPath<T>
Represents a path in TopNSearcher.
|
static class |
Util.Result<T>
Holds a single input (IntsRef) + output, returned by
shortestPaths() . |
private static class |
Util.TieBreakByInputComparator<T>
Compares first by the provided comparator, and then
tie breaks by path.input.
|
static class |
Util.TopNSearcher<T>
Utility class to find top N shortest paths from start
point(s).
|
static class |
Util.TopResults<T>
Holds the results for a top N search using
Util.TopNSearcher |
Modifier | Constructor and Description |
---|---|
private |
Util() |
Modifier and Type | Method and Description |
---|---|
private static void |
emitDotState(java.io.Writer out,
java.lang.String name,
java.lang.String shape,
java.lang.String color,
java.lang.String label)
Emit a single state in the
dot language. |
static <T> T |
get(FST<T> fst,
BytesRef input)
Looks up the output for this input, or null if the
input is not accepted
|
static <T> T |
get(FST<T> fst,
IntsRef input)
Looks up the output for this input, or null if the
input is not accepted.
|
static IntsRef |
getByOutput(FST<java.lang.Long> fst,
long targetOutput)
Reverse lookup (lookup by output instead of by input),
in the special case when your FSTs outputs are
strictly ascending.
|
static IntsRef |
getByOutput(FST<java.lang.Long> fst,
long targetOutput,
FST.BytesReader in,
FST.Arc<java.lang.Long> arc,
FST.Arc<java.lang.Long> scratchArc,
IntsRefBuilder result)
Expert: like
getByOutput(FST, long) except reusing
BytesReader, initial and scratch Arc, and result. |
private static java.lang.String |
printableLabel(int label)
Ensures an arc's label is indeed printable (dot uses US-ASCII).
|
static <T> FST.Arc<T> |
readCeilArc(int label,
FST<T> fst,
FST.Arc<T> follow,
FST.Arc<T> arc,
FST.BytesReader in)
Reads the first arc greater or equal that the given label into the provided
arc in place and returns it iff found, otherwise return
null . |
static <T> Util.TopResults<T> |
shortestPaths(FST<T> fst,
FST.Arc<T> fromNode,
T startOutput,
java.util.Comparator<T> comparator,
int topN,
boolean allowEmptyString)
Starting from node, find the top N min cost
completions to a final node.
|
static BytesRef |
toBytesRef(IntsRef input,
BytesRefBuilder scratch)
Just converts IntsRef to BytesRef; you must ensure the
int values fit into a byte.
|
static <T> void |
toDot(FST<T> fst,
java.io.Writer out,
boolean sameRank,
boolean labelStates)
Dumps an
FST to a GraphViz's dot language description
for visualization. |
static IntsRef |
toIntsRef(BytesRef input,
IntsRefBuilder scratch)
Just takes unsigned byte values from the BytesRef and
converts into an IntsRef.
|
static IntsRef |
toUTF16(java.lang.CharSequence s,
IntsRefBuilder scratch)
Just maps each UTF16 unit (char) to the ints in an
IntsRef.
|
static IntsRef |
toUTF32(char[] s,
int offset,
int length,
IntsRefBuilder scratch)
Decodes the Unicode codepoints from the provided
char[] and places them in the provided scratch
IntsRef, which must not be null, returning it.
|
static IntsRef |
toUTF32(java.lang.CharSequence s,
IntsRefBuilder scratch)
Decodes the Unicode codepoints from the provided
CharSequence and places them in the provided scratch
IntsRef, which must not be null, returning it.
|
public static <T> T get(FST<T> fst, IntsRef input) throws java.io.IOException
java.io.IOException
public static <T> T get(FST<T> fst, BytesRef input) throws java.io.IOException
java.io.IOException
public static IntsRef getByOutput(FST<java.lang.Long> fst, long targetOutput) throws java.io.IOException
NOTE: this only works with FST<Long>
, only
works when the outputs are ascending in order with
the inputs.
For example, simple ordinals (0, 1,
2, ...), or file offsets (when appending to a file)
fit this.
java.io.IOException
public static IntsRef getByOutput(FST<java.lang.Long> fst, long targetOutput, FST.BytesReader in, FST.Arc<java.lang.Long> arc, FST.Arc<java.lang.Long> scratchArc, IntsRefBuilder result) throws java.io.IOException
getByOutput(FST, long)
except reusing
BytesReader, initial and scratch Arc, and result.java.io.IOException
public static <T> Util.TopResults<T> shortestPaths(FST<T> fst, FST.Arc<T> fromNode, T startOutput, java.util.Comparator<T> comparator, int topN, boolean allowEmptyString) throws java.io.IOException
java.io.IOException
public static <T> void toDot(FST<T> fst, java.io.Writer out, boolean sameRank, boolean labelStates) throws java.io.IOException
FST
to a GraphViz's dot
language description
for visualization. Example of use:
PrintWriter pw = new PrintWriter("out.dot"); Util.toDot(fst, pw, true, true); pw.close();and then, from command line:
dot -Tpng -o out.png out.dot
Note: larger FSTs (a few thousand nodes) won't even render, don't bother.
sameRank
- If true
, the resulting dot
file will try
to order states in layers of breadth-first traversal. This may
mess up arcs, but makes the output FST's structure a bit clearer.labelStates
- If true
states will have labels equal to their offsets in their
binary format. Expands the graph considerably.java.io.IOException
private static void emitDotState(java.io.Writer out, java.lang.String name, java.lang.String shape, java.lang.String color, java.lang.String label) throws java.io.IOException
dot
language.java.io.IOException
private static java.lang.String printableLabel(int label)
public static IntsRef toUTF16(java.lang.CharSequence s, IntsRefBuilder scratch)
public static IntsRef toUTF32(java.lang.CharSequence s, IntsRefBuilder scratch)
public static IntsRef toUTF32(char[] s, int offset, int length, IntsRefBuilder scratch)
public static IntsRef toIntsRef(BytesRef input, IntsRefBuilder scratch)
public static BytesRef toBytesRef(IntsRef input, BytesRefBuilder scratch)
public static <T> FST.Arc<T> readCeilArc(int label, FST<T> fst, FST.Arc<T> follow, FST.Arc<T> arc, FST.BytesReader in) throws java.io.IOException
null
.label
- the label to ceil onfst
- the fst to operate onfollow
- the arc to follow reading the label fromarc
- the arc to read into in placein
- the fst's FST.BytesReader
java.io.IOException