/** * /// Deserializes an Fst from an ObjectInputStream * /// * /// @param in * /// the ObjectInputStream. It should be already be initialized by * /// the caller. * /// @return * /// @throws IOException * /// @throws ClassNotFoundException */ private static Fst ReadFst(Stream inStream) { var _is = ReadStringMap(inStream); var os = ReadStringMap(inStream); var serializer = new BinaryFormatter(); var startid = (int)serializer.Deserialize(inStream); var semiring = (Semiring)serializer.Deserialize(inStream); var numStates = (int)serializer.Deserialize(inStream); var res = new Fst(numStates); res.Isyms = _is; res.Osyms = os; res.Semiring = semiring; for (var i = 0; i < numStates; i++) { var numArcs = (int)serializer.Deserialize(inStream); var s = new State(numArcs + 1); var f = (float)serializer.Deserialize(inStream); if (f == res.Semiring.Zero) { f = res.Semiring.Zero; } else if (f == res.Semiring.One) { f = res.Semiring.One; } s.FinalWeight = f; s.Id = (int)serializer.Deserialize(inStream); res._states.Add(s); } res.SetStart(res._states[startid]); numStates = res.GetNumStates(); for (var i = 0; i < numStates; i++) { var s1 = res.GetState(i); for (var j = 0; j < s1.InitialNumArcs - 1; j++) { var a = new Arc(); a.Ilabel = (int)serializer.Deserialize(inStream); a.Olabel = (int)serializer.Deserialize(inStream); a.Weight = (float)serializer.Deserialize(inStream); a.NextState = res._states[(int)serializer.Deserialize(inStream)]; s1.AddArc(a); } } return(res); }
public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Input and output files are not provided"); Console.WriteLine("You need to provide both the input serialized java fst model"); Console.WriteLine("and the output binary openfst model."); Environment.Exit(1); } Fst fst = Fst.LoadModel(args[0]); Console.WriteLine("Saving as openfst text model..."); Convert.Export(fst, args[1]); }
private static void ExportFst(Fst fst, string filename) { StreamWriter streamWriter = new StreamWriter(filename); // print start first State start = fst.Start; streamWriter.WriteLine(start.GetId() + "\t" + start.FinalWeight); // print all states int numStates = fst.GetNumStates(); for (int i = 0; i < numStates; i++) { State s = fst.GetState(i); if (s.GetId() != fst.Start.GetId()) { streamWriter.WriteLine(s.GetId() + "\t" + s.FinalWeight); } } string[] isyms = fst.Isyms; string[] osyms = fst.Osyms; numStates = fst.GetNumStates(); for (int i = 0; i < numStates; i++) { State s = fst.GetState(i); int numArcs = s.GetNumArcs(); for (int j = 0; j < numArcs; j++) { Arc arc = s.GetArc(j); string isym = (isyms != null) ? isyms[arc.Ilabel] : Integer.ToString(arc.Ilabel); string osym = (osyms != null) ? osyms[arc.Olabel] : Integer.ToString(arc.Olabel); streamWriter.WriteLine(s.GetId() + "\t" + arc.NextState.GetId() + "\t" + isym + "\t" + osym + "\t" + arc.Weight); } } streamWriter.Close(); }
public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Input and output files not provided"); Console.WriteLine("You need to provide both the input binary openfst model"); Console.WriteLine("and the output serialized java fst model."); Environment.Exit(1); } //Serialize the java fst model to disk Fst fst = Convert.ImportFst(args[0], new TropicalSemiring()); Console.WriteLine("Saving as binar java fst model..."); try { fst.SaveModel(args[1]); } catch (IOException ex) { Console.WriteLine("Cannot write to file " + args[1]); Environment.Exit(1); } }
public static void Export(Fst fst, string basename) { ExportSymbols(fst.Isyms, basename + ".input.syms"); ExportSymbols(fst.Osyms, basename + ".output.syms"); ExportFst(fst, basename + ".fst.txt"); }
public static Fst ImportFst(string basename, Semiring semiring) { Fst fst = new Fst(semiring); HashMap <string, Integer> isyms = ImportSymbols(basename + ".input.syms"); if (isyms == null) { isyms = new HashMap <string, Integer>(); isyms.Put("<eps>", 0); } HashMap <string, Integer> osyms = ImportSymbols(basename + ".output.syms"); if (osyms == null) { osyms = new HashMap <string, Integer>(); osyms.Put("<eps>", 0); } HashMap <string, Integer> ssyms = ImportSymbols(basename + ".states.syms"); // Parse input FileStream fis = new FileStream(basename + ".fst.txt", FileMode.Open); TextReader br = new StreamReader(fis); bool firstLine = true; string strLine; HashMap <Integer, State> stateMap = new HashMap <Integer, State>(); while ((strLine = br.ReadLine()) != null) { string[] tokens = strLine.Split("\\t"); Integer inputStateId; if (ssyms == null) { inputStateId = Integer.ParseInt(tokens[0]); } else { inputStateId = ssyms.Get(tokens[0]); } State inputState = stateMap.Get(inputStateId); if (inputState == null) { inputState = new State(semiring.Zero); fst.AddState(inputState); stateMap.Put(inputStateId, inputState); } if (firstLine) { firstLine = false; fst.SetStart(inputState); } if (tokens.Length > 2) { Integer nextStateId; if (ssyms == null) { nextStateId = Integer.ParseInt(tokens[1]); } else { nextStateId = ssyms.Get(tokens[1]); } State nextState = stateMap.Get(nextStateId); if (nextState == null) { nextState = new State(semiring.Zero); fst.AddState(nextState); stateMap.Put(nextStateId, nextState); } // Adding arc if (isyms.Get(tokens[2]) == null) { isyms.Put(tokens[2], isyms.Size()); } int?iLabel = isyms.Get(tokens[2]); if (osyms.Get(tokens[3]) == null) { osyms.Put(tokens[3], osyms.Size()); } int?oLabel = osyms.Get(tokens[3]); float arcWeight; if (tokens.Length > 4) { arcWeight = Float.ParseFloat(tokens[4]); } else { arcWeight = 0; } Arc arc = new Arc(iLabel.Value, oLabel.Value, arcWeight, nextState); inputState.AddArc(arc); } else { if (tokens.Length > 1) { float finalWeight = Float.ParseFloat(tokens[1]); inputState.FinalWeight = finalWeight; } else { inputState.FinalWeight = 0.0f; } } } br.Close(); fst.Isyms = Utils.Utils.ToStringArray(isyms); fst.Osyms = Utils.Utils.ToStringArray(osyms); return(fst); }