数据集
import py2neo
import pandas as pd
from py2neo import Graph, Node, Relationship, Subgraph
g=Graph('http://localhost:7474',user='neo4j',password='neo4j')
df = pd.read_csv('triples.csv',encoding='gbk')
a = df[['entity','entitytag']]
b = df[['tail','tail-tag']]
b.columns = ['entity','entitytag']
entity = pd.concat([a,b])
# 因为头实体尾实体有些重复的,对实体去重
entity.drop_duplicates(inplace=True)
print(entity)
node_lis = []
# 创建节点
for i in entity.values:
node = Node(i[1], name = i[0])
node_lis.append(node)
nodes=Subgraph(node_lis)
g.create(nodes)
# triple去重
df.drop_duplicates(inplace=True)
# 创建关系集合,为第一步创建的节点之间添加边
lis = []
count = 0
for i in df.values:
count += 1
print(count)
c = g.nodes.match(i[1], name=i[0]).first()
d = g.nodes.match(i[3], name=i[2]).first()
rel_a = Relationship(c, str(i[4]), d)
lis.append(rel_a)
# 导入关系
nodes = Subgraph(relationships=lis)
g.create(nodes)
参考资料(12条消息) Python neo4j建立知识图谱,药品知识图谱,neo4j知识图谱,知识图谱的建立过程,智能用药知识图谱,智能问诊必备知识图谱_neo4j 知识图谱_医学小达人的博客-CSDN博客