private void addToCurrent(JSGFRule jsgfrule, JSGFRule rule) { if (jsgfrule == null) { return; } if (this.currentRule == null) { this.currentRule = jsgfrule; return; } if (this.currentRule is JSGFRuleSequence) { JSGFRuleSequence jsgfruleSequence = (JSGFRuleSequence)this.currentRule; jsgfruleSequence.append(rule); jsgfrule.parent = this.currentRule; this.currentRule = jsgfrule; } else if (this.currentRule is JSGFRuleAlternatives) { JSGFRuleAlternatives jsgfruleAlternatives = (JSGFRuleAlternatives)this.currentRule; jsgfruleAlternatives.append(rule); jsgfrule.parent = this.currentRule; this.currentRule = jsgfrule; } }
/** * 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); }
private void AddToCurrent(JSGFRule newRule, JSGFRule topRule) { if (newRule == null) { return; } if (_currentRule == null) { _currentRule = newRule; return; } if (_currentRule is JSGFRuleSequence) { JSGFRuleSequence ruleSequence = (JSGFRuleSequence)_currentRule; ruleSequence.Append(topRule); newRule.Parent = _currentRule; _currentRule = newRule; } else if (_currentRule is JSGFRuleAlternatives) { JSGFRuleAlternatives ruleAlternatives = (JSGFRuleAlternatives)_currentRule; ruleAlternatives.Append(topRule); newRule.Parent = _currentRule; _currentRule = newRule; } }
private JSGFGrammar.GrammarGraph processRuleAlternatives(JSGFRuleAlternatives jsgfruleAlternatives) { this.logger.fine(new StringBuilder().append("parseRuleAlternatives: ").append(jsgfruleAlternatives).toString()); JSGFGrammar.GrammarGraph grammarGraph = new JSGFGrammar.GrammarGraph(this); List rules = jsgfruleAlternatives.getRules(); List normalizedWeights = this.getNormalizedWeights(jsgfruleAlternatives.getWeights()); for (int i = 0; i < rules.size(); i++) { JSGFRule jsgfrule = (JSGFRule)rules.get(i); float logProbability = 0f; if (normalizedWeights != null) { logProbability = ((Float)normalizedWeights.get(i)).floatValue(); } this.logger.fine(new StringBuilder().append("Alternative: ").append(jsgfrule).toString()); JSGFGrammar.GrammarGraph grammarGraph2 = this.processRule(jsgfrule); grammarGraph.getStartNode().add(grammarGraph2.getStartNode(), logProbability); grammarGraph2.getEndNode().add(grammarGraph.getEndNode(), 0f); } return(grammarGraph); }
public override void startElement(string uri, string localName, string qName, Attributes attributes) { JSGFRule jsgfrule = null; JSGFRule jsgfrule2 = null; this.logger.fine(new StringBuilder().append("Starting element ").append(qName).toString()); if (String.instancehelper_equals(qName, "rule")) { string value = attributes.getValue("id"); if (value != null) { jsgfrule = new JSGFRuleSequence(new ArrayList()); this.__topRuleMap.put(value, (JSGFRuleSequence)jsgfrule); jsgfrule2 = (JSGFRuleSequence)jsgfrule; } } if (String.instancehelper_equals(qName, "item")) { string value = attributes.getValue("repeat"); if (value != null) { jsgfrule = new JSGFRuleSequence(new ArrayList()); JSGFRuleCount jsgfruleCount = new JSGFRuleCount((JSGFRuleSequence)jsgfrule, 3); jsgfrule2 = jsgfruleCount; } else { jsgfrule = new JSGFRuleSequence(new ArrayList()); jsgfrule2 = (JSGFRuleSequence)jsgfrule; } } if (String.instancehelper_equals(qName, "one-of")) { jsgfrule = new JSGFRuleAlternatives(new ArrayList()); jsgfrule2 = (JSGFRuleAlternatives)jsgfrule; } this.addToCurrent(jsgfrule, jsgfrule2); }
public override void StartElement(URL uri, string localName, string qName, Attributes attributes) { JSGFRule newRule = null; JSGFRule topRule = null; this.LogInfo("Starting element " + qName); if (qName.Equals("rule")) { String id = attributes.getValue("id"); if (id != null) { newRule = new JSGFRuleSequence(new List <JSGFRule>()); TopRuleMap.Put(id, newRule); topRule = newRule; } } if (qName.Equals("item")) { String repeat = attributes.getValue("repeat"); if (repeat != null) { newRule = new JSGFRuleSequence(new List <JSGFRule>()); JSGFRuleCount ruleCount = new JSGFRuleCount(newRule, JSGFRuleCount.OnceOrMore); topRule = ruleCount; } else { newRule = new JSGFRuleSequence(new List <JSGFRule>()); topRule = newRule; } } if (qName.Equals("one-of")) { newRule = new JSGFRuleAlternatives(new List <JSGFRule>()); topRule = newRule; } AddToCurrent(newRule, topRule); }
public JSGFRuleAlternatives Alternatives() { var ruleList = new List <JSGFRule>(); JSGFRule r; float w; List <Float> weights = new List <Float>(); switch ((_jj_ntk == -1) ? jj_ntk() : _jj_ntk) { case Grammar: case Import: case Public: case IntegerLiteral: case FloatingPointLiteral: case StringLiteral: case Identifier: case 28: case 36: case 38: r = Sequence(); ruleList.Add(r); //label_4: var label_4 = false; while (true) { switch ((_jj_ntk == -1) ? jj_ntk() : _jj_ntk) { case 33: ; break; default: jj_la1[8] = jj_gen; label_4 = true; break; //goto label_4; } if (label_4) { break; } jj_consume_token(33); r = Sequence(); ruleList.Add(r); } break; case 34: w = Weight(); r = Sequence(); ruleList.Add(r); weights.Add(w); //label_5: var label_5 = false; while (true) { jj_consume_token(33); w = Weight(); r = Sequence(); ruleList.Add(r); weights.Add(w); switch ((_jj_ntk == -1) ? jj_ntk() : _jj_ntk) { case 33: ; break; default: jj_la1[9] = jj_gen; label_5 = true; break; } if (label_5) { break; } } break; default: jj_la1[10] = jj_gen; jj_consume_token(-1); throw new ParseException(); } JSGFRuleAlternatives ra = new JSGFRuleAlternatives(ruleList); if (weights.Count > 0) { ra.SetWeights(weights); } { if (true) { return(ra); } } }