private void BuildHyperGraph( PhrasalNode node, HyperGraph g, out HyperVertex v) { v = null; if (node == null) { return; } if (node.Children.Count == 0) { int pt = tagset.GetPTID(node.Tag); int wid = vocab.GetId(node.Lex, node.Start == 0); HyperVertex wv = new HyperVertex(true, wid, 1); HyperVertex pv = new HyperVertex(false, pt, rules.GetSubTagCount(pt)); HyperEdge pe = new HyperEdge(pv, wv, rules.GetTerminalRuleScores(pt, wid), rules.GetTerminalPosteriorCounts(pt, wid)); g.Es.Add(pe); g.Vs.Add(wv); g.Vs.Add(pv); v = pv; return; } else if (node.Children.Count == 1) { HyperVertex cv; BuildHyperGraph(node.Children [0], g, out cv); int pt = tagset.GetID(node.Tag); HyperVertex pv = new HyperVertex(false, pt, rules.GetSubTagCount(pt)); HyperEdge pe = new HyperEdge( pv, cv, rules.GetRuleScores(pt, cv.tag), rules.GetPosteriorCounts(pt, cv.tag)); g.Es.Add(pe); g.Vs.Add(pv); v = pv; return; } else if (node.Children.Count == 2) { HyperVertex lv; HyperVertex rv; BuildHyperGraph(node.Children [0], g, out lv); BuildHyperGraph(node.Children [1], g, out rv); int pt = tagset.GetID(node.Tag); HyperVertex pv = new HyperVertex(false, pt, rules.GetSubTagCount(pt)); HyperEdge pe = new HyperEdge(pv, lv, rv, rules.GetRuleScores(pv.tag, lv.tag, rv.tag), rules.GetPosteriorCounts(pv.tag, lv.tag, rv.tag)); g.Es.Add(pe); g.Vs.Add(pv); v = pv; return; } else { throw new Exception("tree node can only have at most 2 children"); } }
public HyperGraph BuildHyperGraph(PhrasalTree tree) { HyperGraph g = new HyperGraph(); int maxTag = rules.subTagCounts.Max(); g.lbuf = new double[maxTag + 1]; HyperVertex root; BuildHyperGraph(tree.Root, g, out root); g.Root = root; return g; }