protected internal virtual bool AddOneUnaryRule(UnaryRule rule, IDictionary <string, TransducerGraph> graphs) { string parentString = stateIndex.Get(rule.parent); string childString = stateIndex.Get(rule.child); if (IsSyntheticState(parentString)) { string topcat = GetTopCategoryOfSyntheticState(parentString); TransducerGraph graph = GetGraphFromMap(graphs, topcat); double output = SmartNegate(rule.Score()); graph.AddArc(graph.GetStartNode(), parentString, childString, output); return(true); } else { if (IsSyntheticState(childString)) { // need to add Arc from synthetic state to endState TransducerGraph graph = GetGraphFromMap(graphs, parentString); double output = SmartNegate(rule.Score()); graph.AddArc(childString, parentString, End, output); // parentString should the the same as endState graph.SetEndNode(parentString); return(true); } else { return(false); } } }
private static double ComputeLocalTreeScore(Tree localTree, IIndex <string> stateIndex, LexicalizedParser pd) { try { string parent = localTree.Value(); int parentState = stateIndex.IndexOf(parent); // System.out.println("parentState: " + parentState); Tree[] children = localTree.Children(); // let's find the unary to kick things off with the left child (since we assume a left to right grammar // first we create the synthetic parent of the leftmost child string nextChild = children[0].Value(); // childState = stateIndex.indexOf(nextChild); string current = "@" + parent + "| [ [" + nextChild + "] "; int currentState = stateIndex.IndexOf(current); IList <UnaryRule> rules = pd.ug.RulesByParent(currentState); UnaryRule ur = rules[0]; // System.out.println("rule: " + ur); double localTreeScore = ur.Score(); // go through rest of rules for (int i = 1; i < children.Length; i++) { // find rules in BinaryGrammar that can extend this state // System.out.println("currentState: " + currentState); nextChild = children[i].Value(); int childState = stateIndex.IndexOf(nextChild); // System.out.println("childState: " + childState); IList <BinaryRule> l = pd.bg.RuleListByLeftChild(currentState); BinaryRule foundBR = null; if (i < children.Length - 1) { // need to the rewrite that doesn't rewrite to the parent foreach (BinaryRule br in l) { // System.out.println("\t\trule: " + br + " parent: " + br.parent + " right: " + br.rightChild); if (br.rightChild == childState && br.parent != parentState) { foundBR = br; break; } } } else { // this is the last rule, need to find the rewrite to the parent of the whole local tree foreach (BinaryRule br in l) { // System.out.println("\t\trule: " + br + " parent: " + br.parent + " right: " + br.rightChild); if (br.rightChild == childState && br.parent == parentState) { foundBR = br; break; } } } if (foundBR == null) { // we never found a matching rule! // System.out.println("broke on " + nextChild); return(double.NegativeInfinity); } // System.out.println("rule: " + foundBR); currentState = foundBR.parent; localTreeScore += foundBR.score; } // end loop through children return(localTreeScore); } catch (NoSuchElementException) { // we couldn't find a state for one of the needed categories // System.out.println("no state found: " + e.toString()); // List tempRules = pd.ug.rulesByChild(childState); // for (Iterator iter = tempRules.iterator(); iter.hasNext();) { // UnaryRule ur = (UnaryRule) iter.next(); // System.out.println("\t\t\trule with child: " + ur); // } return(double.NegativeInfinity); } }