示例#1
0
    def to_hgraph(self):
        from common.hgraph.hgraph import Hgraph
        hgraph = Hgraph()
        hgraph.my_hyper_graph = self

        for node in self.nodes:  # type: GraphNode
            label = ""
            ext_id = None
            ident = "_" + node.name

            # Insert a node into the AMR
            ignoreme = hgraph[ident]  # Initialize dictionary for this node
            hgraph.node_to_concepts[ident] = label
            if ext_id is not None:
                if ident in hgraph.external_nodes and hgraph.external_nodes[
                        ident] != ext_id:
                    raise Exception(
                        "Incompatible external node IDs for node %s." % ident)
                hgraph.external_nodes[ident] = ext_id
                hgraph.rev_external_nodes[ext_id] = ident
            if node.is_root:
                hgraph.roots.append(ident)

        for edge in self.edges:  # type: HyperEdge
            hyperchild = tuple("_" + node.name for node in edge.nodes[1:])
            ident = "_" + edge.nodes[0].name
            new_edge = edge.label
            hgraph._add_triple(ident, new_edge, hyperchild)

        return hgraph
示例#2
0
def delete_nodes(graph, nodes):
    g = Hgraph()
    for p, r, ch in graph.triples():
        if (p not in nodes) and (not (len(ch) == 1 and ch[0] in nodes)):
            g._add_triple(p, r, ch, warn=False)
        else:
            if (p not in nodes) and (p not in g):
                g[p] = ListMap()
            if len(ch) == 1 and (ch[0] not in nodes) and (ch[0] not in g):
                g[ch[0]] = ListMap()
    g.roots = g.find_roots(warn=False)
    return g
示例#3
0
def delete_nodes(graph, nodes):
    g = Hgraph()
    for p, r, ch in graph.triples():
        if (p not in nodes) and (not (len(ch) == 1 and ch[0] in nodes)):
            g._add_triple(p, r, ch, warn=False)
        else:
            if (p not in nodes) and (p not in g):
                g[p] = ListMap()
            if len(ch) == 1 and (ch[0] not in nodes) and (ch[0] not in g):
                g[ch[0]] = ListMap()
    g.roots = g.find_roots(warn=False)
    return g
示例#4
0
def get_line_graph(graph):

    lgraph = Hgraph()
    edges_for_node = defaultdict(list)

    for p, r, ch in graph.triples():
        edges_for_node[p].append(str((p, r, ch)))
        for c in ch:
            edges_for_node[c].append(str((p, r, ch)))

    for r in edges_for_node:
        for p, c in itertools.combinations(edges_for_node[r], 2):
            lgraph._add_triple(p, r, (c,), warn=False)
            lgraph._add_triple(c, r, (p,), warn=False)
    lgraph.roots = lgraph.find_roots()
    return lgraph
示例#5
0
def get_line_graph(graph):

    lgraph = Hgraph()
    edges_for_node = defaultdict(list)

    for p, r, ch in graph.triples():
        edges_for_node[p].append(str((p, r, ch)))
        for c in ch:
            edges_for_node[c].append(str((p, r, ch)))

    for r in edges_for_node:
        for p, c in itertools.combinations(edges_for_node[r], 2):
            lgraph._add_triple(p, r, (c, ), warn=False)
            lgraph._add_triple(c, r, (p, ), warn=False)
    lgraph.roots = lgraph.find_roots()
    return lgraph
示例#6
0
    def rhs_to_hgraph(self):
        from common.cfg import NonterminalLabel
        from common.hgraph.hgraph import Hgraph
        nt_id_count = 0
        hgraph = Hgraph()

        for node in self.rhs.nodes:  # type: GraphNode
            label = ""
            try:
                ext_id = self.lhs.nodes.index(node)
            except ValueError:
                ext_id = None
            ident = "_" + node.name

            # Insert a node into the AMR
            ignoreme = hgraph[ident]  # Initialize dictionary for this node
            hgraph.node_to_concepts[ident] = label
            if ext_id is not None:
                if ident in hgraph.external_nodes and hgraph.external_nodes[
                        ident] != ext_id:
                    raise Exception(
                        "Incompatible external node IDs for node %s." % ident)
                hgraph.external_nodes[ident] = ext_id
                hgraph.rev_external_nodes[ext_id] = ident
            if ext_id == 0:
                hgraph.roots.append(ident)

        for edge in self.rhs.edges:  # type: HyperEdge
            hyperchild = tuple("_" + node.name for node in edge.nodes[1:])
            ident = "_" + edge.nodes[0].name
            if "_" not in edge.label and not edge.label.startswith("ARG") \
                    and not edge.label.startswith("BV"):
                # this is a nonterminal Edge
                new_edge = NonterminalLabel(edge.label)
                if not new_edge.index:
                    new_edge.index = "_%i" % nt_id_count
                    nt_id_count = nt_id_count + 1
            else:
                new_edge = edge.label

            hgraph._add_triple(ident, new_edge, hyperchild)

        return hgraph