Package edu.cmu.sphinx.fst

Examples of edu.cmu.sphinx.fst.State


        Semiring semiring = fst.getSemiring();
        ArrayList<State> fStates = new ArrayList<State>();

        int numStates = fst.getNumStates();
        for (int i = 0; i < numStates; i++) {
            State s = fst.getState(i);
            if (s.getFinalWeight() != semiring.zero()) {
                fStates.add(s);
            }
        }

        // Add a new single final
        State newFinal = new State(semiring.one());
        fst.addState(newFinal);
        for (State s : fStates) {
            // add epsilon transition from the old final to the new one
            s.addArc(new Arc(0, 0, s.getFinalWeight(), newFinal));
            // set old state's weight to zero
View Full Code Here


    /**
     * Undo of the extend operation
     */
    public static void undo(Fst fst) {
        State f = null;
        int numStates = fst.getNumStates();
        for (int i = 0; i < numStates; i++) {
            State s = fst.getState(i);
            if (s.getFinalWeight() != fst.getSemiring().zero()) {
                f = s;
                break;
            }
        }

        if (f == null) {
            System.err.println("Final state not found.");
            return;
        }
        for (int i = 0; i < numStates; i++) {
            State s = fst.getState(i);
            for (int j = 0; j < s.getNumArcs(); j++) {
                Arc a = s.getArc(j);
                if (a.getIlabel() == 0 && a.getOlabel() == 0
                        && a.getNextState().getId() == f.getId()) {
                    s.setFinalWeight(a.getWeight());
                }
            }
        }
        fst.deleteState(f);
    }
View Full Code Here

        d[reversed.getStart().getId()] = semiring.one();
        r[reversed.getStart().getId()] = semiring.one();

        while (!queue.isEmpty()) {
            State q = queue.iterator().next();
            queue.remove(q);

            float rnew = r[q.getId()];
            r[q.getId()] = semiring.zero();

            for (int i = 0; i < q.getNumArcs(); i++) {
                Arc a = q.getArc(i);
                State nextState = a.getNextState();
                float dnext = d[a.getNextState().getId()];
                float dnextnew = semiring.plus(dnext,
                        semiring.times(rnew, a.getWeight()));
                if (dnext != dnextnew) {
                    d[a.getNextState().getId()] = dnextnew;
                    r[a.getNextState().getId()] = semiring.plus(r[a
                            .getNextState().getId()], semiring.times(rnew,
                            a.getWeight()));
                    if (!queue.contains(nextState.getId())) {
                        queue.add(nextState);
                    }
                }
            }
        }
View Full Code Here

        HashMap<Pair<State, Float>, Pair<State, Float>> previous = new HashMap<Pair<State, Float>, Pair<State, Float>>(
                fst.getNumStates());
        HashMap<Pair<State, Float>, State> stateMap = new HashMap<Pair<State, Float>, State>(
                fst.getNumStates());

        State start = fstdet.getStart();
        Pair<State, Float> item = new Pair<State, Float>(start, semiring.one());
        queue.add(item);
        previous.put(item, null);

        while (!queue.isEmpty()) {
            Pair<State, Float> pair = queue.remove();
            State p = pair.getLeft();
            Float c = pair.getRight();

            State s = new State(p.getFinalWeight());
            res.addState(s);
            stateMap.put(pair, s);
            if (previous.get(pair) == null) {
                // this is the start state
                res.setStart(s);
            } else {
                // add the incoming arc from previous to current
                State previouState = stateMap.get(previous.get(pair));
                State previousOldState = previous.get(pair).getLeft();
                for (int j = 0; j < previousOldState.getNumArcs(); j++) {
                    Arc a = previousOldState.getArc(j);
                    if (a.getNextState().equals(p)) {
                        previouState.addArc(new Arc(a.getIlabel(), a
                                .getOlabel(), a.getWeight(), s));
                    }
                }
View Full Code Here

    private static ArrayList<Integer> getUniqueLabels(Fst fst,
            ArrayList<Pair<State, Float>> pa) {
        ArrayList<Integer> res = new ArrayList<Integer>();

        for (Pair<State, Float> p : pa) {
            State s = p.getLeft();

            int numArcs = s.getNumArcs();
            for (int j = 0; j < numArcs; j++) {
                Arc arc = s.getArc(j);
                if (!res.contains(arc.getIlabel())) {
                    res.add(arc.getIlabel());
                }
            }
        }
View Full Code Here

        // stores the queue (item in index 0 is next)
        Queue<ArrayList<Pair<State, Float>>> queue = new LinkedList<ArrayList<Pair<State, Float>>>();

        HashMap<String, State> stateMapper = new HashMap<String, State>();

        State s = new State(semiring.zero());
        String stateString = "(" + fst.getStart() + "," + semiring.one() + ")";
        queue.add(new ArrayList<Pair<State, Float>>());
        queue.peek().add(new Pair<State, Float>(fst.getStart(), semiring.one()));
        res.addState(s);
        stateMapper.put(stateString, s);
        res.setStart(s);

        while (!queue.isEmpty()) {
            ArrayList<Pair<State, Float>> p = queue.remove();
            State pnew = getStateLabel(p, stateMapper);
            queue.remove(0);
            ArrayList<Integer> labels = getUniqueLabels(fst, p);
            for (int label : labels) {
                Float wnew = semiring.zero();
                // calc w'
                for (Pair<State, Float> ps : p) {
                    State old = ps.getLeft();
                    Float u = ps.getRight();
                    int numArcs = old.getNumArcs();
                    for (int j = 0; j < numArcs; j++) {
                        Arc arc = old.getArc(j);
                        if (label == arc.getIlabel()) {
                            wnew = semiring.plus(wnew,
                                    semiring.times(u, arc.getWeight()));
                        }
                    }
                }

                // calc new states
                // keep residual weights to variable forQueue
                ArrayList<Pair<State, Float>> forQueue = new ArrayList<Pair<State, Float>>();
                for (Pair<State, Float> ps : p) {
                    State old = ps.getLeft();
                    Float u = ps.getRight();
                    Float wnewRevert = semiring.divide(semiring.one(), wnew);
                    int numArcs = old.getNumArcs();
                    for (int j = 0; j < numArcs; j++) {
                        Arc arc = old.getArc(j);
                        if (label == arc.getIlabel()) {
                            State oldstate = arc.getNextState();
                            Pair<State, Float> pair = getPair(forQueue,
                                    oldstate, semiring.zero());
                            pair.setRight(semiring.plus(
                                    pair.getRight(),
                                    semiring.times(wnewRevert,
                                            semiring.times(u, arc.getWeight()))));
                        }
                    }
                }

                // build new state's id and new elements for queue
                String qnewid = "";
                for (Pair<State, Float> ps : forQueue) {
                    State old = ps.getLeft();
                    Float unew = ps.getRight();
                    if (!qnewid.equals("")) {
                        qnewid = qnewid + ",";
                    }
                    qnewid = qnewid + "(" + old + "," + unew + ")";
                }

                if (stateMapper.get(qnewid) == null) {
                    State qnew = new State(semiring.zero());
                    res.addState(qnew);
                    stateMapper.put(qnewid, qnew);
                    // update new state's weight
                    Float fw = qnew.getFinalWeight();
                    for (Pair<State, Float> ps : forQueue) {
                        fw = semiring.plus(fw, semiring.times(ps.getLeft()
                                .getFinalWeight(), ps.getRight()));
                    }
                    qnew.setFinalWeight(fw);

                    queue.add(forQueue);
                }
                pnew.addArc(new Arc(label, label, wnew, stateMapper.get(qnewid)));
            }
View Full Code Here

    /**
     * Calculate the epsilon closure
     */
    private static void calcClosure(Fst fst, State state,
            HashMap<State, Float>[] cl, Semiring semiring) {
        State s = state;

        float pathWeight;
        int numArcs = s.getNumArcs();
        for (int j = 0; j < numArcs; j++) {
            Arc a = s.getArc(j);
            if ((a.getIlabel() == 0) && (a.getOlabel() == 0)) {
                if (cl[a.getNextState().getId()] == null) {
                    calcClosure(fst, a.getNextState(), cl, semiring);
                }
                if (cl[a.getNextState().getId()] != null) {
View Full Code Here

     */
    private Fst entryToFSA(ArrayList<String> entry) {
        TropicalSemiring ts = new TropicalSemiring();
        Fst efst = new Fst(ts);

        State s = new State(ts.zero());
        efst.addState(s);
        efst.setStart(s);

        // Build the basic FSA
        for (int i = 0; i < entry.size() + 1; i++) {
            s = new State(ts.zero());
            efst.addState(s);
            if (i >= 1) {
                int symIndex = Utils.getIndex(g2pmodel.getIsyms(),
                        entry.get(i - 1));
                efst.getState(i).addArc(new Arc(symIndex, symIndex, 0.f, s));
            } else if (i == 0) {
                int symIndex = Utils.getIndex(g2pmodel.getIsyms(), sb);
                efst.getStart().addArc(new Arc(symIndex, symIndex, 0.f, s));
            }

            if (i == entry.size()) {
                State s1 = new State(ts.zero());
                efst.addState(s1);
                int symIndex = Utils.getIndex(g2pmodel.getIsyms(), se);
                s.addArc(new Arc(symIndex, symIndex, 0.f, s1));
                s1.setFinalWeight(0.f);
            }
        }

        // Add any cluster arcs
        for (int value = 0; value < clusters.length; value++) {
            ArrayList<String> cluster = clusters[value];
            if (cluster != null) {
                int start = 0;
                int k = 0;
                while (k != -1) {
                    k = Utils.search(entry, cluster, start);
                    if (k != -1) {
                        State from = efst.getState(start + k + 1);
                        from.addArc(new Arc(value, value, 0.f, efst
                                .getState(start + k + cluster.size() + 1)));
                        start = start + k + cluster.size();
                    }
                }
            }
View Full Code Here

        queue.add(fst.getStart());

        String[] osyms = fst.getOsyms();
        while (!queue.isEmpty()) {
            State s = queue.remove();
            Path currentPath = paths.get(s);

            if (s.getFinalWeight() != semiring.zero()) {
                String pathString = currentPath.getPath().toString();
                if (finalPaths.containsKey(pathString)) {
                    // path already exist. update its cost
                    Path old = finalPaths.get(pathString);
                    if (old.getCost() > currentPath.getCost()) {
                        finalPaths.put(pathString, currentPath);
                    }
                } else {
                    finalPaths.put(pathString, currentPath);
                }
            }

            int numArcs = s.getNumArcs();
            for (int j = 0; j < numArcs; j++) {
                Arc a = s.getArc(j);
                p = new Path(fst.getSemiring());
                Path cur = paths.get(s);
                p.setCost(cur.getCost());
                p.setPath((ArrayList<String>) cur.getPath().clone());

                String sym = osyms[a.getOlabel()];

                String[] symsArray = sym.split("\\" + tie);

                for (int i = 0; i < symsArray.length; i++) {
                    String phone = symsArray[i];
                    if (!skipSeqs.contains(phone)) {
                        p.getPath().add(phone);
                    }
                }
                p.setCost(semiring.times(p.getCost(), a.getWeight()));
                State nextState = a.getNextState();
                paths.put(nextState, p);
                if (!queue.contains(nextState)) {
                    queue.add(nextState);
                }
            }
View Full Code Here

     */
    private Fst createFst() {
        TropicalSemiring ts = new TropicalSemiring();
        Fst fst = new Fst(ts);

        State s1 = new State(ts.zero());
        State s2 = new State(ts.zero());
        State s3 = new State(ts.zero());
        State s4 = new State(2.f);

        // State 0
        fst.addState(s1);
        s1.addArc(new Arc(1, 5, 1.f, s2));
        s1.addArc(new Arc(2, 4, 3.f, s2));
View Full Code Here

TOP

Related Classes of edu.cmu.sphinx.fst.State

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.