/** * Parses the given RuleAlternatives into a network of GrammarNodes. * * @param ruleAlternatives * the RuleAlternatives to parse * @return a grammar graph */ private GrammarGraph ProcessRuleAlternatives(JSGFRuleAlternatives ruleAlternatives) { this.LogInfo("parseRuleAlternatives: " + ruleAlternatives); GrammarGraph result = new GrammarGraph(this); List <JSGFRule> rules = ruleAlternatives.GetRules(); List <Float> weights = GetNormalizedWeights(ruleAlternatives.GetWeights()); // expand each alternative, and connect them in parallel for (int i = 0; i < rules.Count; i++) { JSGFRule rule = rules[i]; float weight = 0.0f; if (weights != null) { weight = weights[i]; } this.LogInfo("Alternative: " + rule); GrammarGraph newNodes = ProcessRule(rule); result.StartNode.Add(newNodes.StartNode, weight); newNodes.EndNode.Add(result.EndNode, 0.0f); } return(result); }
/** * Parses the given RuleCount into a network of GrammarNodes. * * @param ruleCount * the RuleCount object to parse * @return a grammar graph */ private GrammarGraph ProcessRuleCount(JSGFRuleCount ruleCount) { this.LogInfo("parseRuleCount: " + ruleCount); GrammarGraph result = new GrammarGraph(this); int count = ruleCount.Count; GrammarGraph newNodes = ProcessRule(ruleCount.Rule); result.StartNode.Add(newNodes.StartNode, 0.0f); newNodes.EndNode.Add(result.EndNode, 0.0f); // if this is optional, add a bypass arc if (count == JSGFRuleCount.ZeroOrMore || count == JSGFRuleCount.Optional) { result.StartNode.Add(result.EndNode, 0.0f); } // if this can possibly occur more than once, add a loopback if (count == JSGFRuleCount.OnceOrMore || count == JSGFRuleCount.ZeroOrMore) { newNodes.EndNode.Add(newNodes.StartNode, 0.0f); } return(result); }
/** * Parses the given RuleName into a network of GrammarNodes. * * @param initialRuleName * the RuleName rule to parse * @return a grammar graph */ private GrammarGraph ProcessRuleName(JSGFRuleName initialRuleName) { this.LogInfo("parseRuleName: " + initialRuleName); GrammarGraph result = RuleStack.Contains(initialRuleName.GetRuleName()); if (result != null) { // its a recursive call return(result); } else { result = new GrammarGraph(this); RuleStack.Push(initialRuleName.GetRuleName(), result); } JSGFRuleName ruleName = _ruleGrammar.Resolve(initialRuleName); if (ruleName == JSGFRuleName.Null) { result.StartNode.Add(result.EndNode, 0.0f); } else if (ruleName == JSGFRuleName.Void) { // no connection for void } else { if (ruleName == null) { throw new JSGFGrammarException("Can't resolve " + initialRuleName + " g " + initialRuleName.GetFullGrammarName()); } JSGFRuleGrammar rg = Manager.RetrieveGrammar(ruleName .GetFullGrammarName()); if (rg == null) { throw new JSGFGrammarException("Can't resolve grammar name " + ruleName.GetFullGrammarName()); } JSGFRule rule = rg.GetRule(ruleName.GetSimpleRuleName()); if (rule == null) { throw new JSGFGrammarException("Can't resolve rule: " + ruleName.GetRuleName()); } GrammarGraph ruleResult = ProcessRule(rule); if (result != ruleResult) { result.StartNode.Add(ruleResult.StartNode, 0.0f); ruleResult.EndNode.Add(result.EndNode, 0.0f); } } RuleStack.Pop(); return(result); }
/** * Commit changes to all loaded grammars and all changes of grammar since * the last commitChange * * @throws JSGFGrammarParseException * @throws JSGFGrammarException */ public virtual void CommitChanges() { try { if (LoadGrammar) { if (Manager == null) { GetGrammarManager(); } _ruleGrammar = LoadNamedGrammar(GrammarName); LoadImports(_ruleGrammar); LoadGrammar = false; } Manager.LinkGrammars(); RuleStack = new RuleStack(); NewGrammar(); FirstNode = CreateGrammarNode("<sil>"); GrammarNode finalNode = CreateGrammarNode("<sil>"); finalNode.SetFinalNode(true); // go through each rule and create a network of GrammarNodes // for each of them foreach (String ruleName in _ruleGrammar.GetRuleNames()) { if (_ruleGrammar.IsRulePublic(ruleName)) { String fullName = GetFullRuleName(ruleName); GrammarGraph publicRuleGraph = new GrammarGraph(this); RuleStack.Push(fullName, publicRuleGraph); JSGFRule rule = _ruleGrammar.GetRule(ruleName); GrammarGraph graph = ProcessRule(rule); RuleStack.Pop(); FirstNode.Add(publicRuleGraph.StartNode, 0.0f); publicRuleGraph.EndNode.Add(finalNode, 0.0f); publicRuleGraph.StartNode.Add(graph.StartNode, 0.0f); graph.EndNode.Add(publicRuleGraph.EndNode, 0.0f); } } PostProcessGrammar(); if (Logger.Level == LogLevel.All) { DumpGrammar(); } } catch (UriFormatException mue) { throw new IOException("bad base grammar URL " + BaseUrl + ' ' + mue); } }
/** * Commit changes to all loaded grammars and all changes of grammar since * the last commitChange * * @throws JSGFGrammarParseException * @throws JSGFGrammarException */ public override void CommitChanges() { try { if (LoadGrammar) { if (Manager == null) { GetGrammarManager(); } LoadXML(); LoadGrammar = false; } RuleStack = new RuleStack(); NewGrammar(); FirstNode = CreateGrammarNode("<sil>"); var finalNode = CreateGrammarNode("<sil>"); finalNode.SetFinalNode(true); // go through each rule and create a network of GrammarNodes // for each of them foreach (var entry in _rules) { var publicRuleGraph = new GrammarGraph(this); RuleStack.Push(entry.Key, publicRuleGraph); var graph = ProcessRule(entry.Value); RuleStack.Pop(); FirstNode.Add(publicRuleGraph.StartNode, 0.0f); publicRuleGraph.EndNode.Add(finalNode, 0.0f); publicRuleGraph.StartNode.Add(graph.StartNode, 0.0f); graph.EndNode.Add(publicRuleGraph.EndNode, 0.0f); } PostProcessGrammar(); } catch (UriFormatException mue) { throw new IOException("bad base grammar URL " + BaseUrl + ' ' + mue); } }
/** * Parses the given RuleSequence into a network of GrammarNodes. * * @param ruleSequence * the RuleSequence to parse * @return the first and last GrammarNodes of the network */ private GrammarGraph ProcessRuleSequence(JSGFRuleSequence ruleSequence) { GrammarNode startNode = null; GrammarNode endNode = null; this.LogInfo("parseRuleSequence: " + ruleSequence); List <JSGFRule> rules = ruleSequence.Rules; GrammarNode lastGrammarNode = null; // expand and connect each rule in the sequence serially for (int i = 0; i < rules.Count; i++) { JSGFRule rule = rules[i]; GrammarGraph newNodes = ProcessRule(rule); // first node if (i == 0) { startNode = newNodes.StartNode; } // last node if (i == (rules.Count - 1)) { endNode = newNodes.EndNode; } if (i > 0) { lastGrammarNode.Add(newNodes.StartNode, 0.0f); } lastGrammarNode = newNodes.EndNode; } return(new GrammarGraph(startNode, endNode, this)); }
/// <summary> /// Pushes the grammar graph on the stack /// </summary> public void Push(String name, GrammarGraph g) { _stack.Insert(0, name); _map.Put(name, g); }