Beispiel #1
0
 /*
  * /// (non-Javadoc)
  * ///
  * /// @see edu.cmu.sphinx.fst.State#addArc(edu.cmu.sphinx.fst.Arc)
  */
 public override void AddArc(Arc arc)
 {
     throw new InvalidOperationException("You cannot modify an ImmutableState.");
 }
Beispiel #2
0
 /**
  * /// Set an arc at the specified position in the arcs' array.
  * ///
  * /// @param index the position to the arcs' array
  * /// @param arc the arc value to set
  */
 public override void SetArc(int index, Arc arc)
 {
     _arcs[index] = arc;
 }
Beispiel #3
0
        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);
        }