def edge_sequence_to_graph(self, _graph, _start_vertex, _grounded_nodes, edge_to_path_dict, id): '''sequence to grounded graph''' current_grounded_graph = GrounedGraph(nodes=list(), edges=list()) marked = defaultdict(bool) sp_online_utils.dfs(_grounded_nodes, current_grounded_graph, marked, _graph, _start_vertex, edge_to_path_dict) current_grounded_graph.set_grounded_query_id(id) return current_grounded_graph
def get_grounded_graph_by_question(question=None): gold_grounded_graph = None for data_ann in test_graph_questions_struct: if data_ann.question == question: gold_grounded_graph = GrounedGraph(nodes=data_ann.nodes, edges=data_ann.edges) break for data_ann in train_graph_questions_struct: if data_ann.question == question: gold_grounded_graph = GrounedGraph(nodes=data_ann.nodes, edges=data_ann.edges) return gold_grounded_graph
def _ungrounded_graph_to_grounded_graph(ungrounded_graph, grounding_result_list): grouned_graph_list = [] if grounding_result_list is None: return grouned_graph_list grounded_nodes = [] for ungrounded_node in ungrounded_graph.nodes: grounded_nodes.append(GroundedNode(nid=ungrounded_node.nid, node_type=ungrounded_node.node_type, type_class=ungrounded_node.type_class, friendly_name=ungrounded_node.friendly_name, question_node=ungrounded_node.question_node, function=ungrounded_node.function, score=0)) for grounded_node in grounded_nodes: for ungrounded_node, nodes_grounding in grounding_result_list: if grounded_node.nid == ungrounded_node.nid: """nodes_grounding: {'en.xtracycle':1.6, 'freebase.type_profile':1.0}""" for mid, pro in nodes_grounding.items(): grounded_node.id = mid grounded_node.score = pro break grounded_graph = GrounedGraph(grounded_query_id=ungrounded_graph.ungrounded_query_id, type='', nodes=grounded_nodes, edges=[], key_path='', sparql_query='', score=0, denotation='') grouned_graph_list.append(grounded_graph) return grouned_graph_list
def _ungrounded_to_grounded(ungrounded_graph): ''' convert ungrounded graph to basic grounded graph :param ungrounded_graph: :return: ''' nodes = [] edges = [] for ungrounded_node in ungrounded_graph.nodes: nodes.append( GroundedNode(nid=ungrounded_node.nid, node_type=ungrounded_node.node_type, type_class=ungrounded_node.type_class, friendly_name=ungrounded_node.friendly_name, question_node=ungrounded_node.question_node, function=ungrounded_node.function, score=0)) for ungrounded_edge in ungrounded_graph.edges: edges.append( GroundedEdge(start=ungrounded_edge.start, end=ungrounded_edge.end, friendly_name=ungrounded_edge.friendly_name, score=ungrounded_edge.score)) return GrounedGraph(grounded_query_id=ungrounded_graph.ungrounded_query_id, type='', nodes=nodes, edges=edges, key_path='', sparql_query='', score=0, denotation='')
def read_gold_graph_query(gold_grounded_graph_json): """ function: read grounded_graph data :param grounded_graph_json: grounded_graph_json :return: grounded_graph structure """ if gold_grounded_graph_json is None: return None grounded_query_id = -1 nodes = [] edges = [] for node_json in gold_grounded_graph_json['nodes']: type_class = None if 'type_class' in node_json.keys(): type_class = node_json['type_class'] elif 'class' in node_json.keys(): type_class = node_json['class'] nodes.append( GroundedNode( nid=node_json["nid"], node_type=node_json["node_type"], id=node_json["id"], type_class=type_class, #class friendly_name=node_json["friendly_name"], question_node=node_json["question_node"], function=node_json["function"])) for edge_json in gold_grounded_graph_json["edges"]: edges.append( GroundedEdge(start=edge_json["start"], end=edge_json["end"], relation=edge_json["relation"], friendly_name=edge_json["friendly_name"])) type = 'gold' return GrounedGraph(grounded_query_id, type, nodes, edges)
def candidate_query_to_grounded_graph(candidate_graphquerys): result = [] for candidate_graphquery in candidate_graphquerys: result.append( GrounedGraph(type=candidate_graphquery["querytype"], nodes=candidate_graphquery["nodes"], edges=candidate_graphquery["edges"], key_path=candidate_graphquery["path"], denotation=candidate_graphquery['denotation'])) return result
def read_grounded_graph(grounded_graph_json): ''' function: read grounded_graph data :param grounded_graph_json: grounded_graph_json :return: grounded_graph structure ''' grounded_query_id = grounded_graph_json['grounded_query_id'] nodes = [] edges = [] if 'nodes' in grounded_graph_json: for node_json in grounded_graph_json["nodes"]: nodes.append( GroundedNode(nid=node_json["nid"], node_type=node_json["node_type"], id=node_json["id"], type_class=node_json['type_class'], friendly_name=node_json["friendly_name"], question_node=node_json["question_node"], function=node_json["function"], score=node_json['score'], ordinal=node_json['ordinal'])) if 'edges' in grounded_graph_json: for edge_json in grounded_graph_json["edges"]: edges.append( GroundedEdge(start=edge_json["start"], end=edge_json["end"], relation=edge_json["relation"], friendly_name=edge_json["friendly_name"], score=edge_json["score"])) type = grounded_graph_json['type'] key_path = grounded_graph_json['key_path'] sparql_query = grounded_graph_json["sparql_query"] score = grounded_graph_json["score"] denotation = grounded_graph_json["denotation"] total_score = 0.0 f1_score = 0.0 if 'total_score' in grounded_graph_json.keys(): total_score = grounded_graph_json['total_score'] if 'f1_score' in grounded_graph_json.keys(): f1_score = grounded_graph_json['f1_score'] return GrounedGraph(grounded_query_id, type, nodes, edges, key_path=key_path, sparql_query=sparql_query, score=score, denotation=denotation, total_score=total_score, f1_score=f1_score)
def read_gold_graph_query(gold_grounded_graph_json): ''' function: read grounded_graph data :param grounded_graph_json: grounded_graph_json :return: grounded_graph structure ''' if gold_grounded_graph_json is None: return None grounded_query_id = -1 nodes = [] edges = [] for node_json in gold_grounded_graph_json['nodes']: type_class = None if 'type_class' in node_json.keys(): type_class = node_json['type_class'] elif 'class' in node_json.keys(): type_class = node_json['class'] nodes.append( GroundedNode( nid=node_json["nid"], node_type=node_json["node_type"], id=node_json["id"], type_class=type_class, #class friendly_name=node_json["friendly_name"], question_node=node_json["question_node"], function=node_json["function"])) for edge_json in gold_grounded_graph_json["edges"]: edges.append( GroundedEdge(start=edge_json["start"], end=edge_json["end"], relation=edge_json["relation"], friendly_name=edge_json["friendly_name"])) type = 'gold' # key_path = grounded_graph_json['key_path'] # sparql_query = grounded_graph_json["sparql_query"] # score = grounded_graph_json["score"] # denotation = grounded_graph_json["denotation"] # total_score = 0.0 # if 'total_score' in grounded_graph_json.keys(): # total_score = grounded_graph_json['total_score'] return GrounedGraph(grounded_query_id, type, nodes, edges)
def extract_grounded_graph_from_jena_freebase(file_path): ''' :argument: file path ='./2019.04_15_complexwebq_test_bgp.txt' :return qid_to_graphs_dict qid_to_grounded_graph_dict = complexwebquestion_interface.extract_grounded_graph_from_jena(globals_args.fn_cwq_file.complexwebquestion_test_bgp_dir) for qid, grounded_graph in qid_to_grounded_graph_dict.items(): print (qid, grounded_graph) ''' qid_to_graphs_dict = dict() lines = read_list_yuanshi(file_path) triples_list = [] nodes_set = [] qid = None # question = None for line in lines: if line.startswith('#QID'): qid = line.split('\t')[2] triples_list = [] nodes_set = [] # elif line.startswith('#question'): # if len(line.split('\t'))==2: # question = line.split('\t')[1] # else: # question='' elif line.startswith('#2.2_triple'): triples_list.append(line.split('\t')[1:]) elif line.startswith('#2.2_node'): nodes_set.append(line) if line.startswith('-------------------'): grounded_nodes = [] grounded_edges = [] # id = 20 for node_line in nodes_set: # id += 1 cols = node_line.split('\t') node_id = cols[1] node = GroundedNode(id=node_id, nid=node_id) if node.id == '?x': node.question_node = 1 if node_id.startswith('?'): node.node_type = 'class' elif node_id.startswith('m.') or node_id.startswith( 'g.') or node_id.startswith('en.'): node.node_type = 'entity' else: node.node_type = 'literal' if len(cols) == 3: node.friendly_name = eval( cols[2]) #set #2.2_node: m.03_dwn {'Lou Seal'} elif len(cols) == 4: node.friendly_name = eval( cols[3] ) ##2.2_node: ?x False {'m.0117q3yz': {'base.type_ontology.abstract', 'common.topic', 'base.type_ontology.inanimate', 'base.type_ontology.non_agent', 'time.event', 'sports.sports_championship_event'}} grounded_nodes.append(node) for triple in triples_list: start_node = _get_node_by_id( grounded_nodes, triple[0]) #.replace('http://rdf.freebase.com/ns/','') end_node = _get_node_by_id( grounded_nodes, triple[2]) #.replace('http://rdf.freebase.com/ns/','') if triple[1] == 'common.topic.notable_types': end_node.node_type = 'class' if end_node.nid.startswith('?'): grounded_edges.append( GroundedEdge(start=start_node.nid, end=end_node.nid, relation=triple[1], friendly_name=triple[1])) else: continue else: grounded_edges.append( GroundedEdge(start=start_node.nid, end=end_node.nid, relation=triple[1], friendly_name=triple[1])) if len(grounded_nodes) > 0: qid_to_graphs_dict[qid] = GrounedGraph(nodes=grounded_nodes, edges=grounded_edges) return qid_to_graphs_dict
def extract_grounded_graph_from_jena_dbpedia(file_path): ''' :argument: file path './2019.07.17_qald_test_bgp.txt' :return qid_to_graphs_dict for qid, grounded_graph in qid_to_grounded_graph_dict.items(): print (qid, grounded_graph) ''' qid_to_graphs_dict = dict() lines = read_list_yuanshi(file_path) triples_list = [] nodes_set = [] qid = None question = None for line in lines: if line.startswith('#Qid'): # qid = 'train_'+line.split('\t')[2] qid = line.split('\t')[2] triples_list = [] nodes_set = [] elif line.startswith('#Question'): if len(line.split('\t')) == 2: question = line.split('\t')[1] else: question = '' elif line.startswith('#2.2_triple'): triples_list.append(line.split('\t')[1:]) elif line.startswith('#2.2_node'): nodes_set.append(line) if line.startswith('-------------------'): grounded_nodes = [] grounded_edges = [] for node_line in nodes_set: # cols = node_line.split('\t') # node_id = cols[1].replace('http://dbpedia.org/resource/','') node_id = node_line.split('\t')[1] node = GroundedNode(id=node_id, nid=node_id) if node.id == '?uri': # question node node.question_node = 1 if node_id.startswith('http://dbpedia.org/resource/'): node.node_type = 'entity' else: #?x node.node_type = 'class' # if node_id.startswith('?'): # node.node_type = 'class' # elif node_id.startswith('http://dbpedia.org/resource/'): # node.node_type = 'entity' # else: # node.node_type = 'class' grounded_nodes.append(node) for triple in triples_list: start_node = _get_node_by_id(grounded_nodes, triple[0]) end_node = _get_node_by_id(grounded_nodes, triple[2]) if triple[ 1] == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': #2020.04.24 add continue grounded_edges.append( GroundedEdge(start=start_node.nid, end=end_node.nid, relation=triple[1], friendly_name=triple[1])) qid_to_graphs_dict[qid] = GrounedGraph(nodes=grounded_nodes, edges=grounded_edges) return qid_to_graphs_dict