Example #1
0
        /// <summary>
        /// Deserializes an ImmutableFst from disk
        /// </summary>
        /// <param name="filename">the binary model filename</param>
        /// <returns></returns>
        public new static ImmutableFst LoadModel(String filename)
        {
            ImmutableFst obj = null;

            try {
                FileStream     fis = new FileStream(filename, FileMode.Open);
                BufferedStream bis = new BufferedStream(fis);
                obj = ReadImmutableFst(bis);
                bis.Close();
                fis.Close();
            }
            catch (FileNotFoundException e) {
                Trace.Write(e);
            }
            catch (IOException e)
            {
                Trace.Write(e);
            }
            catch (Exception e)
            {
                Trace.Write(e);
            }

            //return an empty object not null
            return(obj == null?new ImmutableFst():obj);
        }
Example #2
0
        /**
         * /// Deserializes an ImmutableFst from an ObjectInputStream
         * ///
         * /// @param in
         * ///            the ObjectInputStream. It should be already be initialized by
         * ///            the caller.
         * /// @return
         * /// @throws IOException
         * /// @throws ClassNotFoundException
         */
        private static ImmutableFst ReadImmutableFst(Stream inStream)
        {
            BinaryFormatter serializer = new BinaryFormatter();

            String[]     _is       = ReadStringMap(inStream);
            String[]     os        = ReadStringMap(inStream);
            int          startid   = (int)serializer.Deserialize(inStream);
            Semiring     semiring  = (Semiring)serializer.Deserialize(inStream);
            int          numStates = (int)serializer.Deserialize(inStream);
            ImmutableFst res       = new ImmutableFst(numStates);

            res.Isyms    = _is;
            res.Osyms    = os;
            res.Semiring = semiring;
            for (int i = 0; i < numStates; i++)
            {
                int            numArcs = (int)serializer.Deserialize(inStream);
                ImmutableState s       = new ImmutableState(numArcs + 1);
                float          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[s.GetId()] = s;
            }
            res.SetStart(res._states[startid]);

            numStates = res._states.Length;
            for (int i = 0; i < numStates; i++)
            {
                ImmutableState s1 = res._states[i];
                for (int j = 0; j < s1.InitialNumArcs - 1; j++)
                {
                    Arc 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.SetArc(j, a);
                }
            }

            return(res);
        }