1.启动在12服务器上的neo4j
neo4j status
neo4j start
启动服务器上的neo4j
启动成功
2.数据集样例(知识库中的三元组)
数据形式:头实体\t关系\t尾实体 ,并将该数据放在train.txt文件中
Andrei_Tarkovsky birthPlace Soviet_Union
Ayn_Rand citizenship Soviet_Union
Albert_Einstein deathPlace Princeton,_New_Jersey
Albert_Einstein award Nobel_Prize_in_Physics
Amsterdam part Amsterdam-Zuid
ABBA recordLabel RCA_Records
ABBA associatedBand Benny_Anderssons_orkester
ABBA associatedMusicalArtist Benny_Anderssons_orkester
Aarhus governmentType Magistrate
Aarhus isPartOf Central_Denmark_Region
Albert,_Duke_of_Prussia birthPlace Ansbach
Abadan,_Iran country Iran
3.三元组写入neo4j代码
# -*- encoding: utf-8 -*-
import os
from py2neo import *
class EstablishConceptGraph(object):
"""
Build graph
Node and Relation
"""
def __init__(self,data_file='train.txt',data_name='dbpedia50'):
self.graph = Graph(host='localhost', auth=('neo4j', '123'))
self.dataf = data_file
self.dataname = data_name
self.conceptNodeDict = self.freading()
def freading(self):
nodeSet = set()
triples = []
with open(self.dataf,'r',encoding='utf8') as f:
line = f.readline().strip()
while line:
t = line.split('\t')
nodeSet.add(t[0])
nodeSet.add(t[2])
triples.append(t)
line=f.readline().strip()
entity_node = list(nodeSet)
entity_node.sort()
conceptNodeDict = dict()
for i in range(len(entity_node)):
# print('concept'+str(i+1),entity_node[i])
idStr = 'concept'+str(i+1)
conceptNodeDict[entity_node[i]] = idStr
for t in triples:
# print(t[0], t[1], t[2])
# print(conceptNodeDict[t[0]],conceptNodeDict[t[2]])
n1 = Node(self.dataname, name=t[0], id=conceptNodeDict[t[0]])
n2 = Node(self.dataname, name=t[2], id=conceptNodeDict[t[2]])
tx = self.graph.begin()
rel = Relationship(n1,t[1],n2)
tx.merge(n1, self.dataname, "id") # node,label,primary key
tx.merge(n2, self.dataname, "id") # node,label,pirmary key
tx.merge(rel)
tx.commit()
return conceptNodeDict
if __name__ == "__main__":
conceptGraph = EstablishConceptGraph()
写入成功!
4.清除所有内容
MATCH (cc: dbpedia50)-[r]-(e:dbpedia50)
DELETE cc,e,r
刚才添加的数据清除成功
网友评论