报错
1、py2neo.database.work.ClientError: [Statement.SyntaxError] Invalid input 'g': expected 'd/D' (line 1, column 36 (offset: 35)) "MATCH (_:object) WHERE _.name='L'Engle' RETURN _"
不知道什么原因
构建代码
2、整体代码
import json
import pandas as pd
from py2neo import Graph,Node,Relationship,NodeMatcher
graph = Graph("http://localhost:7474", username="neo4j", password="neoj4")
graph.delete_all()
matcher = NodeMatcher(graph)
'''
node1 = Node('animal',name = 'cat')
r1 = Relationship(node2,'has',node1)
graph.create(node1)
'''
def data():
# 去重之后的 665
triplea = []
# 去重之前的 1591
datas = json.load(open('test_triples.json'))
for data in datas:
for triples in data['triple_list']:
if triples in triplea:
pass
else:
triplea.append(triples)
links_dict = {}
subject_list = []
relation_list = []
object_list = []
for s, r, o in triplea:
subject_list.append(s)
relation_list.append(r)
object_list.append(o)
# print(len(subject_list)) #665
# print(len(relation_list))#665
# print(len(object_list)) #665
subject_list = [str(i) for i in subject_list]
relation_list = [str(i) for i in relation_list]
object_list = [str(i) for i in object_list]
# 将提取的三元组信息存到字典中,构成dataframe
links_dict['subject'] = subject_list
links_dict['relation'] = relation_list
links_dict['object'] = object_list
df_data = pd.DataFrame(links_dict)
# print(df_data)
# 只有结点需要去重
node_sub_key = list(set(subject_list))
node_obj_key = list(set(object_list))
return node_sub_key, node_obj_key, df_data
def create_node(nodes,nodeo):
for name in nodes:
s_node = Node('subject',name=name)
graph.create(s_node)
for name in nodeo:
o_node = Node('object',name=name)
graph.create(o_node)
def create_relation(df_data):
for m in range(0,len(df_data)):
try:
rel = Relationship(matcher.match('subject').where("_.name="+"'"+df_data['subject'][m]+"'").first(),
df_data['relation'][m],
matcher.match('object').where("_.name="+"'"+df_data['object'][m]+"'").first())
print('创建第%d个关系'%(m))
graph.create(rel)
except AttributeError as e:
print(e,m)
if __name__ == '__main__':
node_sub_key,node_obj_key,df_data = data()
create_node(node_sub_key,node_obj_key)
print('结点创建完毕')
create_relation(df_data)
网友评论