利用python构建知识图谱,在neo4j里显示不了关系,但是代码运行没有问题,下面是代码部分
'''读取文件'''
def read_nodes(self):
author = []
article = []
passage_infos = []
# 构建节点实体关系
rels_writer = []
count = 0
for data in open(self.data_path, encoding='utf-8'):
passage_dict = {}
count += 1
print(count)
data_json = json.loads(data)
passage = data_json['article']
passage_dict['article'] = passage
article.append(passage)
if 'author' in data_json:
author.append(data_json['author'])
rels_writer.append([passage, data_json['author']])
passage_infos.append(passage_dict)
print(author)
return set(author), rels_writer, passage_infos, set(article)
'''建立节点'''
def create_node(self, label, nodes):
count = 0
for node_name in nodes:
node = Node(label, name=node_name)
self.g.create(node)
count += 1
print(count, len(nodes))
return
'''创建知识图谱中心文章的节点'''
def create_passage_nodes(self, passage_infos):
count = 0
for passage_dict in passage_infos:
node = Node("Article", name=passage_dict['article'])
self.g.create(node)
count += 1
print(count)
return
'''创建知识图谱实体节点类型schema'''
def create_graphnodes(self):
Author, rels_writer, passage_infos, Article = self.read_nodes()
self.create_passage_nodes(passage_infos)
self.create_node('Author', Author)
print(len(Author))
return
'''创建实体关系边'''
def create_graphrels(self):
Author, rels_writer, passage_infos, Article, = self.read_nodes()
print(rels_writer)
self.create_relationship('Author', 'Article', rels_writer, 'first_author', '第一作者')
'''创建实体关联边'''
def create_relationship(self, start_node, end_node, edges, rel_type, rel_name):
count = 0
# 去重处理
set_edges = []
for edge in edges:
set_edges.append('###'.join(edge))
all = len(set(set_edges))
for edge in set(set_edges):
edge = edge.split('###')
p = edge[0]
q = edge[1]
query = "match(p:%s),(q:%s) where p.name='%s'and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)" % (
start_node, end_node, p, q, rel_type, rel_name)
try:
self.g.run(query)
count += 1
print(rel_type, count, all)
except Exception as e:
print(e)
return
'''导出数据'''
def export_data(self):
author, rels_writer, passage_infos, article = self.read_nodes()
f_author = open('author.txt', 'w+')
f_article = open('article.txt', 'w+')
f_author.write('\n'.join(list(author)))
f_article.write('\n'.join(list(article)))
f_author.close()
f_article.close()
return
if __name__ == '__main__':
handler = ProcessGraph()
handler.create_graphnodes() # 建立节点
handler.create_graphrels() # 建立实体关系边
# handler.export_data()
恳请各位前辈指导