public static float[] shortestDistance(Fst fst) { Fst fst2 = Reverse.get(fst); float[] array = new float[fst2.getNumStates()]; float[] array2 = new float[fst2.getNumStates()]; Semiring semiring = fst2.getSemiring(); Arrays.fill(array, semiring.zero()); Arrays.fill(array2, semiring.zero()); LinkedHashSet linkedHashSet = new LinkedHashSet(); linkedHashSet.add(fst2.getStart()); array[fst2.getStart().getId()] = semiring.one(); array2[fst2.getStart().getId()] = semiring.one(); while (!linkedHashSet.isEmpty()) { State state = (State)linkedHashSet.iterator().next(); linkedHashSet.remove(state); float f = array2[state.getId()]; array2[state.getId()] = semiring.zero(); for (int i = 0; i < state.getNumArcs(); i++) { Arc arc = state.getArc(i); State nextState = arc.getNextState(); float num = array[arc.getNextState().getId()]; float num2 = semiring.plus(num, semiring.times(f, arc.getWeight())); if (num != num2) { array[arc.getNextState().getId()] = num2; array2[arc.getNextState().getId()] = semiring.plus(array2[arc.getNextState().getId()], semiring.times(f, arc.getWeight())); if (!linkedHashSet.contains(nextState)) { linkedHashSet.add(nextState); } } } } return(array); }
private static void add(State state, State state2, float num, HashMap[] array, Semiring semiring) { Float pathWeight = RmEpsilon.getPathWeight(state, state2, array); if (pathWeight == null) { RmEpsilon.put(state, state2, num, array); } else { RmEpsilon.put(state, state2, semiring.plus(num, pathWeight.floatValue()), array); } }
public static Fst get(Fst fst) { if (fst == null) { return(null); } if (fst.getSemiring() == null) { return(null); } Semiring semiring = fst.getSemiring(); Fst fst2 = new Fst(semiring); HashMap[] array = new HashMap[fst.getNumStates()]; State[] array2 = new State[fst.getNumStates()]; State[] array3 = new State[fst.getNumStates()]; int numStates = fst.getNumStates(); for (int i = 0; i < numStates; i++) { State state = fst.getState(i); State state2 = new State(state.getFinalWeight()); fst2.addState(state2); array2[state.getId()] = state2; array3[state2.getId()] = state; if (state2.getId() == fst.getStart().getId()) { fst2.setStart(state2); } } for (int i = 0; i < numStates; i++) { State state = fst.getState(i); State state2 = array2[state.getId()]; int numArcs = state.getNumArcs(); for (int j = 0; j < numArcs; j++) { Arc arc = state.getArc(j); if (arc.getIlabel() != 0 || arc.getOlabel() != 0) { state2.addArc(new Arc(arc.getIlabel(), arc.getOlabel(), arc.getWeight(), array2[arc.getNextState().getId()])); } } if (array[state.getId()] == null) { RmEpsilon.calcClosure(fst, state, array, semiring); } } numStates = fst2.getNumStates(); for (int i = 0; i < numStates; i++) { State state = fst2.getState(i); State state2 = array3[state.getId()]; if (array[state2.getId()] != null) { Iterator iterator = array[state2.getId()].keySet().iterator(); while (iterator.hasNext()) { State state3 = (State)iterator.next(); State state4 = state3; if (state4.getFinalWeight() != semiring.zero()) { state.setFinalWeight(semiring.plus(state.getFinalWeight(), semiring.times(RmEpsilon.getPathWeight(state2, state4, array).floatValue(), state4.getFinalWeight()))); } int numArcs2 = state4.getNumArcs(); for (int k = 0; k < numArcs2; k++) { Arc arc2 = state4.getArc(k); if (arc2.getIlabel() != 0 || arc2.getOlabel() != 0) { Arc arc3 = new Arc(arc2.getIlabel(), arc2.getOlabel(), semiring.times(arc2.getWeight(), RmEpsilon.getPathWeight(state2, state4, array).floatValue()), array2[arc2.getNextState().getId()]); state.addArc(arc3); } } } } } fst2.setIsyms(fst.getIsyms()); fst2.setOsyms(fst.getOsyms()); Connect.apply(fst2); return(fst2); }
public static Fst get(Fst fst) { if (fst.getSemiring() == null) { return(null); } Semiring semiring = fst.getSemiring(); Fst fst2 = new Fst(semiring); fst2.setIsyms(fst.getIsyms()); fst2.setOsyms(fst.getOsyms()); LinkedList linkedList = new LinkedList(); HashMap hashMap = new HashMap(); State state = new State(semiring.zero()); string text = new StringBuilder().append("(").append(fst.getStart()).append(",").append(semiring.one()).append(")").toString(); linkedList.add(new ArrayList()); ((ArrayList)linkedList.peek()).add(new Pair(fst.getStart(), Float.valueOf(semiring.one()))); fst2.addState(state); hashMap.put(text, state); fst2.setStart(state); while (!linkedList.isEmpty()) { ArrayList arrayList = (ArrayList)linkedList.remove(); State stateLabel = Determinize.getStateLabel(arrayList, hashMap); ArrayList uniqueLabels = Determinize.getUniqueLabels(fst, arrayList); Iterator iterator = uniqueLabels.iterator(); while (iterator.hasNext()) { int num = ((Integer)iterator.next()).intValue(); Float @float = Float.valueOf(semiring.zero()); Iterator iterator2 = arrayList.iterator(); while (iterator2.hasNext()) { Pair pair = (Pair)iterator2.next(); State state2 = (State)pair.getLeft(); Float float2 = (Float)pair.getRight(); int numArcs = state2.getNumArcs(); for (int i = 0; i < numArcs; i++) { Arc arc = state2.getArc(i); if (num == arc.getIlabel()) { @float = Float.valueOf(semiring.plus(@float.floatValue(), semiring.times(float2.floatValue(), arc.getWeight()))); } } } ArrayList arrayList2 = new ArrayList(); Iterator iterator3 = arrayList.iterator(); while (iterator3.hasNext()) { Pair pair2 = (Pair)iterator3.next(); State state3 = (State)pair2.getLeft(); Float float3 = (Float)pair2.getRight(); Float float4 = Float.valueOf(semiring.divide(semiring.one(), @float.floatValue())); int numArcs2 = state3.getNumArcs(); for (int j = 0; j < numArcs2; j++) { Arc arc2 = state3.getArc(j); if (num == arc2.getIlabel()) { State nextState = arc2.getNextState(); Pair pair3 = Determinize.getPair(arrayList2, nextState, Float.valueOf(semiring.zero())); pair3.setRight(Float.valueOf(semiring.plus(((Float)pair3.getRight()).floatValue(), semiring.times(float4.floatValue(), semiring.times(float3.floatValue(), arc2.getWeight()))))); } } } string text2 = ""; Iterator iterator4 = arrayList2.iterator(); while (iterator4.hasNext()) { Pair pair4 = (Pair)iterator4.next(); State state4 = (State)pair4.getLeft(); Float float4 = (Float)pair4.getRight(); if (!String.instancehelper_equals(text2, "")) { text2 = new StringBuilder().append(text2).append(",").toString(); } text2 = new StringBuilder().append(text2).append("(").append(state4).append(",").append(float4).append(")").toString(); } if (hashMap.get(text2) == null) { State state2 = new State(semiring.zero()); fst2.addState(state2); hashMap.put(text2, state2); Float float2 = Float.valueOf(state2.getFinalWeight()); Iterator iterator5 = arrayList2.iterator(); while (iterator5.hasNext()) { Pair pair5 = (Pair)iterator5.next(); float2 = Float.valueOf(semiring.plus(float2.floatValue(), semiring.times(((State)pair5.getLeft()).getFinalWeight(), ((Float)pair5.getRight()).floatValue()))); } state2.setFinalWeight(float2.floatValue()); linkedList.add(arrayList2); } stateLabel.addArc(new Arc(num, num, @float.floatValue(), (State)hashMap.get(text2))); } } return(fst2); }