コード例 #1
0
def update_merge_relation_to_relation(ungrounded_graph):
    '''
    relation -> relation    ==>     {relation} -{class mention} -> {relation}
    add blank node'''
    ungrounded_graph_nodes = ungrounded_graph.nodes
    remove_edges = []
    add_edges = []
    for edge in ungrounded_graph.edges:
        start_node = parsing_utils.search_one_node_in_nodes_by_nid(ungrounded_graph_nodes, edge.start)
        end_node = parsing_utils.search_one_node_in_nodes_by_nid(ungrounded_graph_nodes, edge.end)
        if parsing_utils.is_relation_node(start_node) \
                and parsing_utils.is_relation_node(end_node):
            #look for max nid
            max_nid = -1
            for temp_node in ungrounded_graph.nodes:
                if max_nid < temp_node.nid:
                    max_nid = temp_node.nid
            blank_node_nid = max_nid+1
            blank_node = _generate_ungrounded_node(node_type='class', nid=blank_node_nid)
            ungrounded_graph_nodes.append(blank_node)
            add_edges.append( UngroundedEdge(start=start_node.nid, end=blank_node.nid, friendly_name='blank_edge') )
            add_edges.append( UngroundedEdge(start=blank_node.nid, end=end_node.nid, friendly_name='blank_edge') )
            # add_edges.append(UngroundedEdge(start=start_node.nid, end=blank_node.nid, friendly_name='blank_edge', score=1.0))
            # add_edges.append(UngroundedEdge(start=blank_node.nid, end=end_node.nid, friendly_name='#blank_edge', score=1.0))
            remove_edges.append(edge)
    new_ungrounded_edges = []
    for edge in ungrounded_graph.edges:
        if edge not in remove_edges:
            new_ungrounded_edges.append(copy.deepcopy(edge))
    for edge in add_edges:
        new_ungrounded_edges.append(copy.deepcopy(edge))
    ungrounded_graph.edges = new_ungrounded_edges

    return ungrounded_graph
コード例 #2
0
def _generate_ungrounded_edge(start=-1,
                              end=-1,
                              friendly_name='blank_edge',
                              score=1.0):
    '''ungrounded edge'''
    ungrounded_edge = UngroundedEdge(start=start,
                                     end=end,
                                     friendly_name=friendly_name,
                                     score=score)
    return ungrounded_edge
コード例 #3
0
def expand(head_index_in_dep=None,
           end_index_in_dep=None,
           ungrounded_nodes=None,
           dependency_graph=None):
    # Expand(head, u)
    # 	visit <- u
    # 	if u 属于 V:
    # 		connect head and u
    # 		return
    # 	for each vertex v connected with u in Y do:
    # 		if v 不属于visit:
    # 			Expand(head, v)
    visit_set.add(end_index_in_dep)
    if parsing_utils.is_contained_one_node_from_nodes(end_index_in_dep - 1, ungrounded_nodes) \
            and not parsing_utils.is_contained_one_node(head_index_in_dep - 1, end_index_in_dep - 1, ungrounded_nodes):
        # do not in one same node 属于一个node, 但是不是当前的node
        start_node = parsing_utils.look_for_one_node_from_nodes(
            head_index_in_dep - 1, ungrounded_nodes)
        end_node = parsing_utils.look_for_one_node_from_nodes(
            end_index_in_dep - 1, ungrounded_nodes)
        friendly_name = parsing_utils.get_friendly_name_by_dependency(
            dependency_graph=dependency_graph, path=path)
        # connect head an u
        temp_edge = UngroundedEdge(start=start_node.nid,
                                   end=end_node.nid,
                                   friendly_name=friendly_name,
                                   score=1.0)
        if not parsing_utils.is_exist_edge_in_edges(
                ungrounded_edges=ungrounded_edges, ungrounded_edge=temp_edge):
            # 如果ungrounded_edges不存在这样的edge, 则追加
            ungrounded_edges.append(temp_edge)
        return

    # 递归扩张邻居顶点
    adj_vertexs = parsing_utils.adj_edge_nodes_update(end_index_in_dep,
                                                      dependency_graph)
    if adj_vertexs is not None:
        for adj_vertex_index in adj_vertexs:
            if adj_vertex_index not in visit_set:
                path.append(adj_vertex_index)
                expand(head_index_in_dep=head_index_in_dep,
                       end_index_in_dep=adj_vertex_index,
                       ungrounded_nodes=ungrounded_nodes,
                       dependency_graph=dependency_graph)
                path.pop()
コード例 #4
0
def read_ungrounded_graph(ungrounded_graph_json):
    '''
        function: read ungrounded_graph data
        :param ungrounded_graph_json: ungrounded_graph json file
        :return: ungrounded_graph structure list
    '''
    ungrounded_query_id = ungrounded_graph_json['ungrounded_query_id']
    blag = ''
    if 'blag' in ungrounded_graph_json.keys():
        blag = ungrounded_graph_json['blag']
    nodes = []
    edges = []
    for node_json in ungrounded_graph_json['nodes']:
        if 'score' in node_json.keys():
            score = node_json['score']
        else:
            score = 0.0
        if 'ordinal' in node_json.keys():
            ordinal = node_json['ordinal']
        else:
            ordinal = "none"
        nodes.append(
            UngroundedNode(
                nid=node_json["nid"],
                node_type=node_json["node_type"],
                friendly_name=node_json["friendly_name"],
                question_node=node_json["question_node"],
                function_str=node_json["function"],
                score=score,
                normalization_value=node_json["normalization_value"],
                type_class=node_json['type_class'],
                ordinal=ordinal))
    for edge_json in ungrounded_graph_json['edges']:
        if 'score' in edge_json.keys():
            score = edge_json['score']
        else:
            score = 0.0
        edges.append(
            UngroundedEdge(start=edge_json["start"],
                           end=edge_json["end"],
                           friendly_name=edge_json["friendly_name"],
                           score=score))
    important_words_list = ungrounded_graph_json['important_words_list']
    abstract_question = ungrounded_graph_json['abstract_question']
    sequence_ner_tag_dict = ungrounded_graph_json['sequence_ner_tag_dict']
    grounded_linking = None
    if 'grounded_linking' in ungrounded_graph_json.keys():
        grounded_linking = ungrounded_graph_json['grounded_linking']
    grounded_graph_list = []
    if 'grounded_graph_forest' in ungrounded_graph_json.keys():
        # add grounded_graph_forest
        for grounded_graph_json in ungrounded_graph_json[
                'grounded_graph_forest']:
            grounded_graph_list.append(
                read_grounded_graph(grounded_graph_json))
    return UngroundedGraph(ungrounded_query_id=ungrounded_query_id,
                           blag=blag,
                           nodes=nodes,
                           edges=edges,
                           important_words_list=important_words_list,
                           abstract_question=abstract_question,
                           sequence_ner_tag_dict=sequence_ner_tag_dict,
                           grounded_linking=grounded_linking,
                           grounded_graph_forest=grounded_graph_list)