def final_glue1(self): """try to cover all phrases AND glue rules""" # candidate glue nodes are glue nodes whose boxes are also phrases # candidate_glue_nodes = [] # for node in self.glue_nodes: # bin = self.chart.get((node.fi, node.fj, node.ei, node.ej)) # if bin is not None: # if PHRASE in bin: # candidate_glue_nodes.append(node) candidates = self.phrases + self.glue_rules # topo sort. root node at the end candidates.sort() roots = [] while len(candidates) > 0: root = candidates.pop() print('pop: %s' % root) roots.append(root) hg = Hypergraph(root) hg.find_reachable_nodes() candidates = [n for n in candidates if id(n) not in hg.found] top_rule = Rule() top_rule.lhs = START top_edge = PhraseHGEdge(top_rule) for root in roots: top_rule.f.append(root.nt) top_rule.e.append(root.nt) top_edge.add_tail(root) top_rule.e2f = [i for i in range(len(top_rule.f))] top_node = PhraseHGNode(START, 0, self.n1, 0, self.n2) top_node.add_incoming(top_edge) return top_node
def final_glue(self): unattached = self.phrases[:] candidates = self.phrases + self.glue_nodes # topo sort. root node at the end unattached.sort() candidates.sort() self.top_roots = [] self.other_roots = [] while len(candidates) > 0: root = candidates.pop() if (root.fi == 0 and root.fj == self.n1 and root.ei == 0 and root.ej == self.n2): self.top_roots.append(root) else: self.other_roots.append(root) hg = Hypergraph(root) hg.find_reachable_nodes() unattached = [n for n in unattached if id(n) not in hg.found] candidates = [n for n in candidates if id(n) not in hg.found and \ (n.nt == PHRASE or not n < root)] top_node = PhraseHGNode(START, 0, self.n1, 0, self.n2) # add one edge for each top root for root in self.top_roots: rule = Rule() rule.lhs = START rule.f = [root.nt] rule.e = [root.nt] rule.e2f = [0] edge = PhraseHGEdge(rule) edge.add_tail(root) top_node.add_incoming(edge) # add one edge for all other roots if ((glue_missing_phrases or len(self.top_roots) == 0) and len(self.other_roots) > 0): rule = Rule() rule.lhs = START edge = PhraseHGEdge(rule) for root in self.other_roots: rule.f.append(root.nt) rule.e.append(root.nt) edge.add_tail(root) rule.e2f = [i for i in range(len(rule.f))] top_node.add_incoming(edge) return top_node