Skip navigation links

Package org.apache.lucene.util.fst

Finite state transducers

See: Description

Package org.apache.lucene.util.fst Description

Finite state transducers

This package implements Finite State Transducers with the following characteristics:

FST Construction example:

     // Input values (keys). These must be provided to Builder in Unicode code point (UTF8 or UTF32) sorted order.
     // Note that sorting by Java's String.compareTo, which is UTF16 sorted order, is not correct and can lead to
     // exceptions while building the FST:
     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();
     IntsRefBuilder scratchInts = new IntsRefBuilder();
     for (int i = 0; i < inputValues.length; 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 ( != null) {
       InputOutput<Long> mapEntry = iterator.current();
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
Skip navigation links

Copyright © 2000-2020 Apache Software Foundation. All Rights Reserved.