def parse_string(s): """ Parse the RHS of a CFG rule. """ tokens = s.strip().split() res = [] nt_index = 0 for t in tokens: if "$" in t: new_token = NonterminalLabel.from_string(t) if not new_token.index: new_token.index = "_%i" % nt_index nt_index = nt_index + 1 else: new_token = t res.append(new_token) return res
def parse_string(s): """ Parse the RHS of a CFG rule. """ tokens = s.strip().split() res = [] nt_index = 0 for t in tokens: if "$" in t: new_token = NonterminalLabel.from_string(t) if not new_token.index: new_token.index = "_%i" % nt_index nt_index = nt_index + 1 else: new_token = t res.append(new_token) return res
def pop_and_transition(): # Create all edges in a group from the stack, attach them to the # graph and then transition to the appropriate state in the FSA edges = [] while stack[-1][0] != PNODE: # Pop all edges children = [] while stack[-1][0] == CNODE: # Pop all nodes in hyperedge itemtype, node = stack.pop() insert_node(node) children.append(node) assert stack[-1][0] == EDGE itemtype, edgelabel = stack.pop() edges.append((edgelabel, children)) # Construct the hyperedge itemtype, parentnode = stack.pop() for edgelabel, children in edges: hypertarget = [] # build hyperedge tail for ident, label, ext_id in children: hypertarget.append(ident) hypertarget.reverse() hyperchild = tuple(hypertarget) if "$" in edgelabel: # this is a nonterminal Edge new_edge = NonterminalLabel.from_string(edgelabel) if not new_edge.index: new_edge.index = "_%i" %self.nt_id_count self.nt_id_count = self.nt_id_count + 1 else: new_edge = edgelabel ident, label, ext_id = parentnode hgraph._add_triple(ident, new_edge, hyperchild) if stack: insert_node(parentnode) stack.append((CNODE, parentnode)) state = 4 else: insert_node(parentnode, root = True) state = 5
def pop_and_transition(): # Create all edges in a group from the stack, attach them to the # graph and then transition to the appropriate state in the FSA edges = [] while stack[-1][0] != PNODE: # Pop all edges children = [] while stack[-1][0] == CNODE: # Pop all nodes in hyperedge itemtype, node = stack.pop() insert_node(node) children.append(node) assert stack[-1][0] == EDGE itemtype, edgelabel = stack.pop() edges.append((edgelabel, children)) # Construct the hyperedge itemtype, parentnode = stack.pop() for edgelabel, children in edges: hypertarget = [] # build hyperedge tail for ident, label, ext_id in children: hypertarget.append(ident) hypertarget.reverse() hyperchild = tuple(hypertarget) if "$" in edgelabel: # this is a nonterminal Edge new_edge = NonterminalLabel.from_string(edgelabel) if not new_edge.index: new_edge.index = "_%i" % self.nt_id_count self.nt_id_count = self.nt_id_count + 1 else: new_edge = edgelabel ident, label, ext_id = parentnode hgraph._add_triple(ident, new_edge, hyperchild) if stack: insert_node(parentnode) stack.append((CNODE, parentnode)) state = 4 else: insert_node(parentnode, root=True) state = 5