Python连接neo4j,只显示节点,不显示关系,代码debug中关系没有问题,了解的请指点一下!万分感谢
下面是代码,debug时已经创建出关系列表了
#!/usr/bin/env python3
coding: utf-8
from py2neo import Graph, Node, Relationship
import pandas as pd
import re
import os
class MedicalGraph:
def init(self):
cur_dir = '/'.join(os.path.abspath(file).split('/')[:-1])
self.data_path = os.path.join(cur_dir, '\data.xls')
self.graph = Graph("http://localhost:7474", auth=("neo4j","wangwang"),name="neo4j")
def read_file(self):
"""
读取文件,获得实体,实体关系
:return:
"""
# cols = ["Company", "EstablishingTime", "Person", "Business", "Address","Intro"]
# 实体
Companies = [] # 公司
EstablishingTIME = [] # 建立时间
Est_Address = [] # 地址
Persons = [] # 法人
Businesses = [] # 经营范围
# 公司的属性:Intro简介
Companies_infos = []
# 关系
Company_to_Address = [] # 公司与地址关系
Company_to_EstablishingTime = [] # 公司与建立时间关系
Companies_to_Person = [] # 公司与法人关系
Company_to_Business = [] # 公司与经营范围关系
all_data = pd.read_excel(self.data_path, encoding='utf-8').loc[:, :].values #gb18030
for data in all_data:
Company_dict = {} # 公司信息
# 公司
Company = str(data[1]).replace("...", " ").strip() #默认删除字符串头和尾的空白字符 (包括n,r,t这些)
Company_dict["Company"] = Company
# 建立时间
line = re.sub("[,、;,.;]", " ", str(data[0])) if str(data[0]) else "未知"
for EstablishingTime in line.strip().split():
EstablishingTIME.append(EstablishingTime)
Company_to_EstablishingTime.append([Company, EstablishingTime])
# 法人
Person_list = re.split('、', str(data[3]).strip()) if str(data[3]) else "未知"
for Person in Person_list:
Persons.append(Person)
Companies_to_Person.append([Company, Person])
#经营范围
Business_list = re.split(',|;|;|,',str(data[4]).replace("。", " ").strip()) if str(data[4]) else "未知"
for Business in Business_list:
Businesses.append(Business)
Company_to_Business.append([Company, Business])
# 地址
Address_list = re.split(';', str(data[2]).strip()) if str(data[2]) else "未知"
for Address in Address_list:
Est_Address.append(Address)
Company_to_Address.append([Company, Address])
# 简介
Intro_list = str(data[5]).strip() if str(data[5]) else "未知"
Company_dict["Intro"] = Intro_list
Companies_infos.append(Company_dict)
# print
# x
return set(Companies), set(Est_Address), set(EstablishingTIME), set(Persons), set(Businesses), \
Company_to_EstablishingTime, Company_to_Address, Companies_to_Person, Company_to_Business, \
Companies_infos
def create_node(self, label, nodes):
"""
创建节点
:param label: 标签
:param nodes: 节点
:return:
"""
count = 0
for node_name in nodes:
node = Node(label, name=node_name)
self.graph.create(node)
count += 1
print(count, len(nodes))
return
def create_Companies_nodes(self, Company_info):
"""
创建公司节点的属性
:param Company_info: list(Dict)
:return:
"""
count = 0
for Company_dict in Company_info:
node = Node("COMPANY", Intro=Company_dict['Intro']) #Company_dict["Intro"] = Intro_list
self.graph.create(node)
count += 1
print(count)
return
def create_graphNodes(self):
"""
创建知识图谱实体
:return:
"""
Company, Address, EstablishingTime, Person, Business, rel_EstablishingTime, rel_Address, rel_Person, \
rel_Business, rel_infos = self.read_file() #drug,, rel_drug,rel_infos, Intro,rel_Intro,
self.create_Companies_nodes(rel_infos) #创建公司属性
self.create_node("ADDRESS", Address)
self.create_node("ESTABLISHING_TIME", EstablishingTime)
self.create_node("PERSON", Person)
self.create_node("BUSINESS", Business)
return
def create_graphRels(self):
Company, Address, EstablishingTime, Person, Business, rel_EstablishingTime, rel_Address, rel_Person, \
rel_Business, rel_infos = self.read_file()
self.create_relationship("COMPANY", "ESTABLISHING_TIME", rel_EstablishingTime, "ESTABLISHING_TIME_IS", "建立时间")
self.create_relationship("COMPANY", "ADDRESS", rel_Address, "ADDRESS_IS", "地址")
self.create_relationship("COMPANY", "PERSON", rel_Person, "PERSON_IS", "法人")
self.create_relationship("COMPANY", "BUSINESS", rel_Business, "BUSINESS_IS", "经营范围")
# print
# x
return
def create_relationship(self, start_node, end_node, edges, rel_type, rel_name):
"""
创建实体关系边
:param start_node:
:param end_node:
:param edges:
:param rel_type:
:param rel_name:
:return:
"""
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.graph.run(query)
count += 1
print(rel_type, count, all)
except Exception as e:
print(e)
return
if name == "main":
handler = MedicalGraph()
handler.create_graphNodes()
handler.create_graphRels()