Search in sources :

Example 11 with Transition

use of org.apache.lucene.util.automaton.Transition in project lucene-solr by apache.

the class TermAutomatonQuery method toDot.

/** Returns the dot (graphviz) representation of this automaton.
   *  This is extremely useful for visualizing the automaton. */
public String toDot() {
    // TODO: refactor & share with Automaton.toDot!
    StringBuilder b = new StringBuilder();
    b.append("digraph Automaton {\n");
    b.append("  rankdir = LR\n");
    final int numStates = det.getNumStates();
    if (numStates > 0) {
        b.append("  initial [shape=plaintext,label=\"0\"]\n");
        b.append("  initial -> 0\n");
    }
    Transition t = new Transition();
    for (int state = 0; state < numStates; state++) {
        b.append("  ");
        b.append(state);
        if (det.isAccept(state)) {
            b.append(" [shape=doublecircle,label=\"" + state + "\"]\n");
        } else {
            b.append(" [shape=circle,label=\"" + state + "\"]\n");
        }
        int numTransitions = det.initTransition(state, t);
        for (int i = 0; i < numTransitions; i++) {
            det.getNextTransition(t);
            assert t.max >= t.min;
            for (int j = t.min; j <= t.max; j++) {
                b.append("  ");
                b.append(state);
                b.append(" -> ");
                b.append(t.dest);
                b.append(" [label=\"");
                if (j == anyTermID) {
                    b.append('*');
                } else {
                    b.append(idToTerm.get(j).utf8ToString());
                }
                b.append("\"]\n");
            }
        }
    }
    b.append('}');
    return b.toString();
}
Also used : Transition(org.apache.lucene.util.automaton.Transition)

Example 12 with Transition

use of org.apache.lucene.util.automaton.Transition in project lucene-solr by apache.

the class GraphTokenStreamFiniteStrings method articulationPoints.

/**
   * Returns the articulation points (or cut vertices) of the graph:
   * https://en.wikipedia.org/wiki/Biconnected_component
   */
public int[] articulationPoints() {
    if (det.getNumStates() == 0) {
        return new int[0];
    }
    //
    Automaton.Builder undirect = new Automaton.Builder();
    undirect.copy(det);
    for (int i = 0; i < det.getNumStates(); i++) {
        int numT = det.initTransition(i, transition);
        for (int j = 0; j < numT; j++) {
            det.getNextTransition(transition);
            undirect.addTransition(transition.dest, i, transition.min);
        }
    }
    int numStates = det.getNumStates();
    BitSet visited = new BitSet(numStates);
    int[] depth = new int[det.getNumStates()];
    int[] low = new int[det.getNumStates()];
    int[] parent = new int[det.getNumStates()];
    Arrays.fill(parent, -1);
    List<Integer> points = new ArrayList<>();
    articulationPointsRecurse(undirect.finish(), 0, 0, depth, low, parent, visited, points);
    Collections.reverse(points);
    return points.stream().mapToInt(p -> p).toArray();
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) TokenStream(org.apache.lucene.analysis.TokenStream) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) BytesRef(org.apache.lucene.util.BytesRef) Automaton(org.apache.lucene.util.automaton.Automaton) Transition(org.apache.lucene.util.automaton.Transition) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) HashMap(java.util.HashMap) PositionLengthAttribute(org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute) FiniteStringsIterator(org.apache.lucene.util.automaton.FiniteStringsIterator) ArrayList(java.util.ArrayList) IntsRef(org.apache.lucene.util.IntsRef) List(java.util.List) Operations(org.apache.lucene.util.automaton.Operations) Map(java.util.Map) BytesTermAttribute(org.apache.lucene.analysis.tokenattributes.BytesTermAttribute) DEFAULT_MAX_DETERMINIZED_STATES(org.apache.lucene.util.automaton.Operations.DEFAULT_MAX_DETERMINIZED_STATES) BitSet(java.util.BitSet) Collections(java.util.Collections) TermToBytesRefAttribute(org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute) Automaton(org.apache.lucene.util.automaton.Automaton) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList)

Example 13 with Transition

use of org.apache.lucene.util.automaton.Transition in project lucene-solr by apache.

the class GraphTokenStreamFiniteStrings method articulationPointsRecurse.

private static void articulationPointsRecurse(Automaton a, int state, int d, int[] depth, int[] low, int[] parent, BitSet visited, List<Integer> points) {
    visited.set(state);
    depth[state] = d;
    low[state] = d;
    int childCount = 0;
    boolean isArticulation = false;
    Transition t = new Transition();
    int numT = a.initTransition(state, t);
    for (int i = 0; i < numT; i++) {
        a.getNextTransition(t);
        if (visited.get(t.dest) == false) {
            parent[t.dest] = state;
            articulationPointsRecurse(a, t.dest, d + 1, depth, low, parent, visited, points);
            childCount++;
            if (low[t.dest] >= depth[state]) {
                isArticulation = true;
            }
            low[state] = Math.min(low[state], low[t.dest]);
        } else if (t.dest != parent[state]) {
            low[state] = Math.min(low[state], depth[t.dest]);
        }
    }
    if ((parent[state] != -1 && isArticulation) || (parent[state] == -1 && childCount > 1)) {
        points.add(state);
    }
}
Also used : Transition(org.apache.lucene.util.automaton.Transition)

Aggregations

Transition (org.apache.lucene.util.automaton.Transition)13 Automaton (org.apache.lucene.util.automaton.Automaton)7 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)3 TokenStreamToAutomaton (org.apache.lucene.analysis.TokenStreamToAutomaton)3 Term (org.apache.lucene.index.Term)3 IntsRefBuilder (org.apache.lucene.util.IntsRefBuilder)3 List (java.util.List)2 BytesRef (org.apache.lucene.util.BytesRef)2 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)2 IntsRef (org.apache.lucene.util.IntsRef)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 BitSet (java.util.BitSet)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Analyzer (org.apache.lucene.analysis.Analyzer)1