bool RecurseForwardsAndFinalise(CMUSphinx_FSGState n)
        {
            //check for loops
            if(!doneStates.Add(n)) return true;

            n.StateID = nextStateID++;

            //Calculate Probabilities
            int nOut = 0;
            foreach (CMUSphinx_FSGTransition t in n.TransitionsOut)
            {
                //nOut += t.To.EquivalentCount;
                nOut += t.Count;
            }
            foreach (CMUSphinx_FSGTransition t in n.TransitionsOut)
            {
                //t.Probability = ((float)1.0 * t.To.EquivalentCount) / nOut;
                t.Probability = ((float)1.0 * t.Count) / nOut;
                outTransitions.Add(t);
                RecurseForwardsAndFinalise(t.To);
            }

            return true;
        }
Exemple #2
0
 bool FSGActionsFromCapabilities(DeviceCapabilities caps, CMUSphinx_GrammarDict cgd, 
     ref CMUSphinx_FSGState startState, ref CMUSphinx_FSGState endState)
 {
     string capsName = "<caps_" + caps.CapsAsIntString + ">";
     if (caps_rules_fsg.Keys.Contains(caps.Caps))
     {
         startState = caps_rules_fsg[caps.Caps].Item1;
         endState = caps_rules_fsg[caps.Caps].Item2;
         return true;
     }
     List<string> capsAsString = caps.Actions;
     if (capsAsString == null || capsAsString.Count == 0)
     {
         startState = null;
         endState = null;
         return false;
     }
     CMUSphinx_FSGState start = cgd.FSGCreateOrphanState();
     CMUSphinx_FSGState end = cgd.FSGCreateOrphanState();
     foreach (string s in capsAsString)
     {
         cgd.FSGLinkStates(start, end, s);
     }
     caps_rules_fsg.Add(caps.Caps, new Tuple<CMUSphinx_FSGState,CMUSphinx_FSGState>(start, end));
     startState = start;
     endState = end;
     //return new SrgsRuleRef(r, "action");
     return true;
 }
 bool OptimiseBackwards(CMUSphinx_FSGState n)
 {
     //TODO: equivalent if graph to the endNode is the same
     return false;
 }
 bool OptimiseForwards(CMUSphinx_FSGState n)
 {
     //TODO: equivalent if the previous state was the same and the transition string was the same
     return false;
 }
        public bool FSGLinkStates(CMUSphinx_FSGState from, CMUSphinx_FSGState to, string onString)
        {
            //need to split and uppercase the onString
            CMUSphinx_FSGState tempFrom = from;
            onString = onString.ToUpper();
            string[] toks = onString.Split(new Char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            int i = 0;
            for (i = 0; i < toks.Length - 1; i++)
            {
                _AddToWordList(toks[i]);
                tempFrom = FSGTransitionToNewState(tempFrom, toks[i]);
            }

            string endTok;
            if (toks.Length <= i) endTok = "";
            else endTok = toks[i];

                _AddToWordList(endTok);
                CMUSphinx_FSGTransition tr = new CMUSphinx_FSGTransition(tempFrom, to, endTok);
                if (tr != null) return true;

            return false;
        }
 public CMUSphinx_FSGState FSGTransitionToNewState(CMUSphinx_FSGState from, string onString)
 {
     CMUSphinx_FSGState ret = new CMUSphinx_FSGState(-1);
     allFSGNodes.Add(ret);
     FSGLinkStates(from, ret, onString);
     return ret;
 }
 public CMUSphinx_FSGState FSGCreateOrphanState()
 {
     CMUSphinx_FSGState ret = new CMUSphinx_FSGState(-1);
     allFSGNodes.Add(ret);
     return ret;
 }
 public CMUSphinx_FSGState FSGGroupStates(CMUSphinx_FSGState State1, CMUSphinx_FSGState State2)
 {
     if (State1 == null || State2 == null) return null;
     State2.EquivalentTo = State1;
     return State1;
 }
 public CMUSphinx_FSGState FSGCreate(string name)
 {
     GrammarName = name;
     allFSGNodes = new HashSet<CMUSphinx_FSGState>();
     rootFSGNode = new CMUSphinx_FSGState(-1);
     allFSGNodes.Add(rootFSGNode);
     endFSGNode = new CMUSphinx_FSGState(-1);
     allFSGNodes.Add(endFSGNode);
     return rootFSGNode;
 }
        public bool BuildFSGGrammarAndDict()
        {
            //0) Insert blank entry leading to endNode - get crashes without this
            CMUSphinx_FSGState tempState = new CMUSphinx_FSGState(-1);
            allFSGNodes.Add(tempState);
            FSGLinkStates(endFSGNode, tempState, "");
            endFSGNode = tempState;

            //1) Optimise graph
            //optimise graph, check for loose ends, be careful of loops
            // states are equivalent if the previous state was the same and the transition string was the same
            //             or if graph to the endNode is the same
            //1a) Equiv lookforward
            OptimiseForwards(rootFSGNode);
            //1b) Equiv lookback
            OptimiseBackwards(endFSGNode.TransitionsIn[0].From);
            //1c) Remove equivalents
            RemoveEquivalents();

            //2) Number and count states, set probs, and compile list of transitions (out only)
            outTransitions = new HashSet<CMUSphinx_FSGTransition>();
            nextStateID = 0;
            doneStates = new HashSet<CMUSphinx_FSGState>(); //loop protection
            RecurseForwardsAndFinalise(rootFSGNode);

            //3) create output file header
            StringBuilder b = new StringBuilder();
            b.AppendLine("FSG_BEGIN <"+GrammarName+">");
            b.AppendLine("NUM_STATES " + nextStateID.ToString());
            b.AppendLine("START_STATE " + rootFSGNode.StateID.ToString());
            b.AppendLine("FINAL_STATE " + endFSGNode.StateID.ToString());

            //4) Dump transitions
            foreach (CMUSphinx_FSGTransition t in outTransitions)
            {
                b.AppendFormat("TRANSITION {0} {1} {2} {3}{4}",
                    t.From.StateID, t.To.StateID, t.Probability, t.OnString, Environment.NewLine);
            }

            //5)Footer and close out
            b.AppendLine("FSG_END");
            Grammar = b.ToString();
            GrammarType = "FSG";
            _validGrammar = true;
            return _BuildDict();
        }
 public CMUSphinx_FSGTransition(CMUSphinx_FSGState from, CMUSphinx_FSGState to, string on)
 {
     From = from;
     To = to;
     OnString = on;
     from.TransitionsOut.Add(this);
     to.TransitionsIn.Add(this);
     Probability = 1.0f;
     Count = 1;
 }
 public CMUSphinx_FSGState(int stateID)
 {
     StateID = stateID;
     TransitionsOut = new List<CMUSphinx_FSGTransition>();
     TransitionsIn = new List<CMUSphinx_FSGTransition>();
     EquivalentTo = null;
     EquivalentCount = 1;
 }
Exemple #13
0
        CMUSphinx_FSGState CreateNumberFSG(CMUSphinx_FSGState startState, CMUSphinx_GrammarDict cgd)
        {
            CMUSphinx_FSGState start_special = cgd.FSGCreateOrphanState();
            CMUSphinx_FSGState start_digits = cgd.FSGCreateOrphanState();
            CMUSphinx_FSGState start_tens = cgd.FSGCreateOrphanState();
            CMUSphinx_FSGState end = cgd.FSGCreateOrphanState();
            CMUSphinx_FSGState end_tens = cgd.FSGCreateOrphanState();

            //fillers
            cgd.FSGLinkStates(startState, start_digits, "");
            cgd.FSGLinkStates(startState, start_special, "");
            cgd.FSGLinkStates(startState, start_tens, "");

            //digits
            cgd.FSGLinkStates(start_digits, end, "one");
            cgd.FSGLinkStates(start_digits, end, "two");
            cgd.FSGLinkStates(start_digits, end, "seven");
            //TODO

            //specials
            cgd.FSGLinkStates(start_special, end, "ten");
            cgd.FSGLinkStates(start_special, end, "eleven");
            cgd.FSGLinkStates(start_special, end, "twelve");
            //TODO: Finish these

            //tens
            cgd.FSGLinkStates(start_tens, end_tens, "twenty");
            cgd.FSGLinkStates(start_tens, end_tens, "thirty");
            //TODO: Finish the tens
            cgd.FSGLinkStates(end_tens, start_digits, "");
            cgd.FSGLinkStates(end_tens, end, "");

            return end;
        }