烧~ 2023-02-03 23:46 采纳率: 100%
浏览 118
已结题

Neo4j 不显示关系

Python连接neo4j,只显示节点,不显示关系,代码debug中关系没有问题,了解的请指点一下!万分感谢

img

下面是代码,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()

  • 写回答

5条回答 默认 最新

  • Oops_GTC 2023-02-04 00:00
    关注

    可以尝试执行以下几个步骤来调试问题:

    1.检查数据模型:确保在数据模型中正确定义节点和关系,包括正确的属性名称、数据类型和约束。
    2.验证数据:使用 Cypher 查询验证数据库中是否存在节点和关系以及是否具有正确的数据
    3.调试 Python 代码:检查连接到 Neo4j 的 Python 代码,确保它正确查询数据库以检索节点和关系。确保正确构造查询并正确处理结果。
    4.查看输出:确保 Python 代码的输出格式正确并显示,并且包含节点和关系。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日
  • 修改了问题 2月4日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确