Esempio n. 1
0
        public static void augment(int label, Fst fst, Semiring semiring)
        {
            string[] isyms     = fst.getIsyms();
            string[] osyms     = fst.getOsyms();
            int      ilabel    = isyms.Length;
            int      iLabel    = isyms.Length + 1;
            int      oLabel    = osyms.Length;
            int      olabel    = osyms.Length + 1;
            int      numStates = fst.getNumStates();

            for (int i = 0; i < numStates; i++)
            {
                State state = fst.getState(i);
                int   num   = (!(fst is ImmutableFst)) ? state.getNumArcs() : (state.getNumArcs() - 1);
                for (int j = 0; j < num; j++)
                {
                    Arc arc = state.getArc(j);
                    if (label == 1 && arc.getOlabel() == 0)
                    {
                        arc.setOlabel(olabel);
                    }
                    else if (label == 0 && arc.getIlabel() == 0)
                    {
                        arc.setIlabel(ilabel);
                    }
                }
                if (label == 0)
                {
                    if (fst is ImmutableFst)
                    {
                        state.setArc(num, new Arc(iLabel, 0, semiring.one(), state));
                    }
                    else
                    {
                        state.addArc(new Arc(iLabel, 0, semiring.one(), state));
                    }
                }
                else if (label == 1)
                {
                    if (fst is ImmutableFst)
                    {
                        state.setArc(num, new Arc(0, oLabel, semiring.one(), state));
                    }
                    else
                    {
                        state.addArc(new Arc(0, oLabel, semiring.one(), state));
                    }
                }
            }
        }
Esempio n. 2
0
        public static void apply(Fst fst)
        {
            Semiring  semiring  = fst.getSemiring();
            ArrayList arrayList = new ArrayList();
            int       numStates = fst.getNumStates();

            for (int i = 0; i < numStates; i++)
            {
                State state = fst.getState(i);
                if (state.getFinalWeight() != semiring.zero())
                {
                    arrayList.add(state);
                }
            }
            State state2 = new State(semiring.one());

            fst.addState(state2);
            Iterator iterator = arrayList.iterator();

            while (iterator.hasNext())
            {
                State state3 = (State)iterator.next();
                state3.addArc(new Arc(0, 0, state3.getFinalWeight(), state2));
                state3.setFinalWeight(semiring.zero());
            }
        }
        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);
        }
Esempio n. 4
0
        public static Fst get(Fst fst)
        {
            if (fst.getSemiring() == null)
            {
                return(null);
            }
            ExtendFinal.apply(fst);
            Semiring semiring = fst.getSemiring();
            Fst      fst2     = new Fst(fst.getNumStates());

            fst2.setSemiring(semiring);
            fst2.setIsyms(fst.getOsyms());
            fst2.setOsyms(fst.getIsyms());
            State[] array     = new State[fst.getNumStates()];
            int     numStates = fst.getNumStates();

            for (int i = 0; i < numStates; i++)
            {
                State state  = fst.getState(i);
                State state2 = new State(semiring.zero());
                fst2.addState(state2);
                array[state.getId()] = state2;
                if (state.getFinalWeight() != semiring.zero())
                {
                    fst2.setStart(state2);
                }
            }
            array[fst.getStart().getId()].setFinalWeight(semiring.one());
            for (int i = 0; i < numStates; i++)
            {
                State state   = fst.getState(i);
                State state2  = array[state.getId()];
                int   numArcs = state.getNumArcs();
                for (int j = 0; j < numArcs; j++)
                {
                    Arc   arc    = state.getArc(j);
                    State state3 = array[arc.getNextState().getId()];
                    Arc   arc2   = new Arc(arc.getIlabel(), arc.getOlabel(), semiring.reverse(arc.getWeight()), state2);
                    state3.addArc(arc2);
                }
            }
            ExtendFinal.undo(fst);
            return(fst2);
        }
Esempio n. 5
0
        public static Fst getFilter(string[] syms, Semiring semiring)
        {
            Fst fst  = new Fst(semiring);
            int num  = syms.Length;
            int num2 = syms.Length + 1;

            fst.setIsyms(syms);
            fst.setOsyms(syms);
            State state = new State(syms.Length + 3);

            state.setFinalWeight(semiring.one());
            State state2 = new State(syms.Length);

            state2.setFinalWeight(semiring.one());
            State state3 = new State(syms.Length);

            state3.setFinalWeight(semiring.one());
            fst.addState(state);
            state.addArc(new Arc(num2, num, semiring.one(), state));
            state.addArc(new Arc(num, num, semiring.one(), state2));
            state.addArc(new Arc(num2, num2, semiring.one(), state3));
            for (int i = 1; i < syms.Length; i++)
            {
                state.addArc(new Arc(i, i, semiring.one(), state));
            }
            fst.setStart(state);
            fst.addState(state2);
            state2.addArc(new Arc(num, num, semiring.one(), state2));
            for (int i = 1; i < syms.Length; i++)
            {
                state2.addArc(new Arc(i, i, semiring.one(), state));
            }
            fst.addState(state3);
            state3.addArc(new Arc(num2, num2, semiring.one(), state3));
            for (int i = 1; i < syms.Length; i++)
            {
                state3.addArc(new Arc(i, i, semiring.one(), state));
            }
            return(fst);
        }
Esempio n. 6
0
        private ArrayList findAllPaths(Fst fst, int num, HashSet hashSet, string text)
        {
            Semiring   semiring   = fst.getSemiring();
            HashMap    hashMap    = new HashMap();
            HashMap    hashMap2   = new HashMap();
            LinkedList linkedList = new LinkedList();
            Path       path       = new Path(fst.getSemiring());

            path.setCost(semiring.one());
            hashMap2.put(fst.getStart(), path);
            linkedList.add(fst.getStart());
            string[] osyms = fst.getOsyms();
            while (!linkedList.isEmpty())
            {
                State state = (State)linkedList.remove();
                Path  path2 = (Path)hashMap2.get(state);
                if (state.getFinalWeight() != semiring.zero())
                {
                    string text2 = path2.getPath().toString();
                    if (hashMap.containsKey(text2))
                    {
                        Path path3 = (Path)hashMap.get(text2);
                        if (path3.getCost() > path2.getCost())
                        {
                            hashMap.put(text2, path2);
                        }
                    }
                    else
                    {
                        hashMap.put(text2, path2);
                    }
                }
                int i = state.getNumArcs();
                for (int j = 0; j < i; j++)
                {
                    Arc arc = state.getArc(j);
                    path = new Path(fst.getSemiring());
                    Path path4 = (Path)hashMap2.get(state);
                    path.setCost(path4.getCost());
                    path.setPath((ArrayList)path4.getPath().clone());
                    string text3 = osyms[arc.getOlabel()];
                    foreach (string text4 in String.instancehelper_split(text3, new StringBuilder().append("\\").append(text).toString()))
                    {
                        if (!hashSet.contains(text4))
                        {
                            path.getPath().add(text4);
                        }
                    }
                    path.setCost(semiring.times(path.getCost(), arc.getWeight()));
                    State nextState = arc.getNextState();
                    hashMap2.put(nextState, path);
                    if (!linkedList.contains(nextState))
                    {
                        linkedList.add(nextState);
                    }
                }
            }
            ArrayList arrayList = new ArrayList();
            Iterator  iterator  = hashMap.values().iterator();

            while (iterator.hasNext())
            {
                Path path5 = (Path)iterator.next();
                arrayList.add(path5);
            }
            Collections.sort(arrayList, new PathComparator());
            int num2 = arrayList.size();

            for (int i = num; i < num2; i++)
            {
                arrayList.remove(arrayList.size() - 1);
            }
            return(arrayList);
        }
Esempio n. 7
0
        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);
        }
        public static Fst get(Fst fst, int n, bool determinize)
        {
            if (fst == null)
            {
                return(null);
            }
            if (fst.getSemiring() == null)
            {
                return(null);
            }
            Fst fst2 = fst;

            if (determinize)
            {
                fst2 = Determinize.get(fst);
            }
            Semiring semiring = fst2.getSemiring();
            Fst      fst3     = new Fst(semiring);

            fst3.setIsyms(fst2.getIsyms());
            fst3.setOsyms(fst2.getOsyms());
            float[] array = NShortestPaths.shortestDistance(fst2);
            ExtendFinal.apply(fst2);
            int[]         array2        = new int[fst2.getNumStates()];
            PriorityQueue priorityQueue = new PriorityQueue(10, new NShortestPaths_1(array, semiring));
            HashMap       hashMap       = new HashMap(fst.getNumStates());
            HashMap       hashMap2      = new HashMap(fst.getNumStates());
            State         start         = fst2.getStart();
            Pair          pair          = new Pair(start, Float.valueOf(semiring.one()));

            priorityQueue.add(pair);
            hashMap.put(pair, null);
            while (!priorityQueue.isEmpty())
            {
                Pair  pair2  = (Pair)priorityQueue.remove();
                State state  = (State)pair2.getLeft();
                Float @float = (Float)pair2.getRight();
                State state2 = new State(state.getFinalWeight());
                fst3.addState(state2);
                hashMap2.put(pair2, state2);
                if (hashMap.get(pair2) == null)
                {
                    fst3.setStart(state2);
                }
                else
                {
                    State state3 = (State)hashMap2.get(hashMap.get(pair2));
                    State state4 = (State)((Pair)hashMap.get(pair2)).getLeft();
                    for (int i = 0; i < state4.getNumArcs(); i++)
                    {
                        Arc arc = state4.getArc(i);
                        if (arc.getNextState().equals(state))
                        {
                            state3.addArc(new Arc(arc.getIlabel(), arc.getOlabel(), arc.getWeight(), state2));
                        }
                    }
                }
                Integer integer = Integer.valueOf(state.getId());
                int[]   array3  = array2;
                int     num     = integer.intValue();
                int[]   array4  = array3;
                array4[num]++;
                if (array2[integer.intValue()] == n && state.getFinalWeight() != semiring.zero())
                {
                    break;
                }
                if (array2[integer.intValue()] <= n)
                {
                    for (int j = 0; j < state.getNumArcs(); j++)
                    {
                        Arc   arc2  = state.getArc(j);
                        float num2  = semiring.times(@float.floatValue(), arc2.getWeight());
                        Pair  pair3 = new Pair(arc2.getNextState(), Float.valueOf(num2));
                        hashMap.put(pair3, pair2);
                        priorityQueue.add(pair3);
                    }
                }
            }
            return(fst3);
        }