需要neo4j语法,在w3c里面:neo4j 教程_w3cschool
连接到neo4j:
class MedicalExtractor(object):
def __init__(self):
super(MedicalExtractor, self).__init__()
self.graph = Graph('http://localhost:7474/', auth=("neo4j", "lixiqqq123"))
这个是新版本的连接方法,老版本会报错
写入关系操作
for head,relation,tail in tqdm(triples,ncols=80):
cql = """MATCH(p:{head_type}),(q:{tail_type})
WHERE p.name='{head}' AND q.name='{tail}'
MERGE (p)-[r:{relation}]->(q)""".format(
head_type=head_type,tail_type=tail_type,head=head.replace("'",""),
tail=tail.replace("'",""),relation=relation)
try:
self.graph.run(cql)
except Exception as e:
print(e)
print(cql)
写入实体属性之类只要在上面代码稍加修改即可