예제 #1
0
        public static void main(string[] args)
        {
            if (args.Length < 2)
            {
                java.lang.System.err.println("Input and output files not provided");
                java.lang.System.err.println("You need to provide both the input serialized java fst model");
                java.lang.System.err.println("and the output binary openfst model.");
                java.lang.System.exit(1);
            }
            Fst fst = Fst.loadModel(args[0]);

            [email protected]("Saving as openfst text model...");
            Convert.export(fst, args[1]);
        }
예제 #2
0
        public static Fst loadModel(string filename)
        {
            long                timeInMillis        = Calendar.getInstance().getTimeInMillis();
            FileInputStream     fileInputStream     = new FileInputStream(filename);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
            ObjectInputStream   objectInputStream   = new ObjectInputStream(bufferedInputStream);
            Fst result = Fst.readFst(objectInputStream);

            objectInputStream.close();
            bufferedInputStream.close();
            fileInputStream.close();
            java.lang.System.err.println(new StringBuilder().append("Load Time: ").append((double)(Calendar.getInstance().getTimeInMillis() - timeInMillis) / 1000.0).toString());
            return(result);
        }
예제 #3
0
        private static Fst readFst(ObjectInputStream objectInputStream)
        {
            string[] array    = Fst.readStringMap(objectInputStream);
            string[] array2   = Fst.readStringMap(objectInputStream);
            int      num      = objectInputStream.readInt();
            Semiring semiring = (Semiring)objectInputStream.readObject();
            int      num2     = objectInputStream.readInt();
            Fst      fst      = new Fst(num2);

            fst.isyms    = array;
            fst.osyms    = array2;
            fst.semiring = semiring;
            for (int i = 0; i < num2; i++)
            {
                int   num3  = objectInputStream.readInt();
                State state = new State(num3 + 1);
                float num4  = objectInputStream.readFloat();
                if (num4 == fst.semiring.zero())
                {
                    num4 = fst.semiring.zero();
                }
                else if (num4 == fst.semiring.one())
                {
                    num4 = fst.semiring.one();
                }
                state.setFinalWeight(num4);
                state.id = objectInputStream.readInt();
                fst.states.add(state);
            }
            fst.setStart((State)fst.states.get(num));
            num2 = fst.getNumStates();
            for (int i = 0; i < num2; i++)
            {
                State state2 = fst.getState(i);
                for (int j = 0; j < state2.initialNumArcs - 1; j++)
                {
                    Arc arc = new Arc();
                    arc.setIlabel(objectInputStream.readInt());
                    arc.setOlabel(objectInputStream.readInt());
                    arc.setWeight(objectInputStream.readFloat());
                    arc.setNextState((State)fst.states.get(objectInputStream.readInt()));
                    state2.addArc(arc);
                }
            }
            return(fst);
        }
예제 #4
0
        private static ImmutableFst readImmutableFst(ObjectInputStream objectInputStream)
        {
            string[]     isyms        = Fst.readStringMap(objectInputStream);
            string[]     osyms        = Fst.readStringMap(objectInputStream);
            int          num          = objectInputStream.readInt();
            Semiring     semiring     = (Semiring)objectInputStream.readObject();
            int          num2         = objectInputStream.readInt();
            ImmutableFst immutableFst = new ImmutableFst(num2);

            immutableFst.isyms    = isyms;
            immutableFst.osyms    = osyms;
            immutableFst.semiring = semiring;
            for (int i = 0; i < num2; i++)
            {
                int            num3           = objectInputStream.readInt();
                ImmutableState immutableState = new ImmutableState(num3 + 1);
                float          num4           = objectInputStream.readFloat();
                if (num4 == immutableFst.semiring.zero())
                {
                    num4 = immutableFst.semiring.zero();
                }
                else if (num4 == immutableFst.semiring.one())
                {
                    num4 = immutableFst.semiring.one();
                }
                immutableState.setFinalWeight(num4);
                immutableState.id = objectInputStream.readInt();
                immutableFst.states[immutableState.getId()] = immutableState;
            }
            immutableFst.setStart(immutableFst.states[num]);
            num2 = immutableFst.states.Length;
            for (int i = 0; i < num2; i++)
            {
                ImmutableState immutableState2 = immutableFst.states[i];
                for (int j = 0; j < immutableState2.initialNumArcs - 1; j++)
                {
                    Arc arc = new Arc();
                    arc.setIlabel(objectInputStream.readInt());
                    arc.setOlabel(objectInputStream.readInt());
                    arc.setWeight(objectInputStream.readFloat());
                    arc.setNextState(immutableFst.states[objectInputStream.readInt()]);
                    immutableState2.setArc(j, arc);
                }
            }
            return(immutableFst);
        }
예제 #5
0
        public static void main(string[] args)
        {
            if (args.Length < 2)
            {
                java.lang.System.err.println("Input and output files not provided");
                java.lang.System.err.println("You need to provide both the input binary openfst model");
                java.lang.System.err.println("and the output serialized java fst model.");
                java.lang.System.exit(1);
            }
            Fst fst = Convert.importFst(args[0], new TropicalSemiring());

            [email protected]("Saving as binary java fst model...");
            try
            {
                fst.saveModel(args[1]);
            }
            catch (IOException)
            {
                java.lang.System.err.println(new StringBuilder().append("Cannot write to file ").append(args[1]).toString());
                java.lang.System.exit(1);
            }
        }
예제 #6
0
        public static Fst importFst(string basename, Semiring semiring)
        {
            Fst     fst     = new Fst(semiring);
            HashMap hashMap = Convert.importSymbols(new StringBuilder().append(basename).append(".input.syms").toString());

            if (hashMap == null)
            {
                hashMap = new HashMap();
                hashMap.put("<eps>", Integer.valueOf(0));
            }
            HashMap hashMap2 = Convert.importSymbols(new StringBuilder().append(basename).append(".output.syms").toString());

            if (hashMap2 == null)
            {
                hashMap2 = new HashMap();
                hashMap2.put("<eps>", Integer.valueOf(0));
            }
            HashMap         hashMap3        = Convert.importSymbols(new StringBuilder().append(basename).append(".states.syms").toString());
            FileInputStream fileInputStream = new FileInputStream(new StringBuilder().append(basename).append(".fst.txt").toString());
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            BufferedReader  bufferedReader  = new BufferedReader(new InputStreamReader(dataInputStream, "UTF-8"));
            int             num             = 1;
            HashMap         hashMap4        = new HashMap();
            string          text;

            while ((text = bufferedReader.readLine()) != null)
            {
                string[] array = String.instancehelper_split(text, "\\t");
                Integer  integer;
                if (hashMap3 == null)
                {
                    integer = Integer.valueOf(Integer.parseInt(array[0]));
                }
                else
                {
                    integer = (Integer)hashMap3.get(array[0]);
                }
                State state = (State)hashMap4.get(integer);
                if (state == null)
                {
                    state = new State(semiring.zero());
                    fst.addState(state);
                    hashMap4.put(integer, state);
                }
                if (num != 0)
                {
                    num = 0;
                    fst.setStart(state);
                }
                if (array.Length > 2)
                {
                    Integer integer2;
                    if (hashMap3 == null)
                    {
                        integer2 = Integer.valueOf(Integer.parseInt(array[1]));
                    }
                    else
                    {
                        integer2 = (Integer)hashMap3.get(array[1]);
                    }
                    State state2 = (State)hashMap4.get(integer2);
                    if (state2 == null)
                    {
                        state2 = new State(semiring.zero());
                        fst.addState(state2);
                        hashMap4.put(integer2, state2);
                    }
                    if (hashMap.get(array[2]) == null)
                    {
                        hashMap.put(array[2], Integer.valueOf(hashMap.size()));
                    }
                    int iLabel = ((Integer)hashMap.get(array[2])).intValue();
                    if (hashMap2.get(array[3]) == null)
                    {
                        hashMap2.put(array[3], Integer.valueOf(hashMap2.size()));
                    }
                    int   oLabel = ((Integer)hashMap2.get(array[3])).intValue();
                    float weight;
                    if (array.Length > 4)
                    {
                        weight = Float.parseFloat(array[4]);
                    }
                    else
                    {
                        weight = 0f;
                    }
                    Arc arc = new Arc(iLabel, oLabel, weight, state2);
                    state.addArc(arc);
                }
                else if (array.Length > 1)
                {
                    float finalWeight = Float.parseFloat(array[1]);
                    state.setFinalWeight(finalWeight);
                }
                else
                {
                    state.setFinalWeight(0f);
                }
            }
            dataInputStream.close();
            fst.setIsyms(Utils.toStringArray(hashMap));
            fst.setOsyms(Utils.toStringArray(hashMap2));
            return(fst);
        }
예제 #7
0
 public static void export(Fst fst, string basename)
 {
     Convert.exportSymbols(fst.getIsyms(), new StringBuilder().append(basename).append(".input.syms").toString());
     Convert.exportSymbols(fst.getOsyms(), new StringBuilder().append(basename).append(".output.syms").toString());
     Convert.exportFst(fst, new StringBuilder().append(basename).append(".fst.txt").toString());
 }