首页 > 其他分享 >创建医疗知识图谱:导入数据

创建医疗知识图谱:导入数据

时间:2024-06-02 21:56:05浏览次数:26  
标签:rels 图谱 self disease drug json 导入 创建 data

 的代码修改成这样:

#!/usr/bin/env python3
# coding: utf-8
# File: MedicalGraph.py
# Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>
# Date: 18-10-3
# -*- coding: utf-8 -*-
import os
import json
from py2neo import Graph,Node

class MedicalGraph:
    def __init__(self):
        cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
        self.data_path = os.path.join(cur_dir, 'data/medical.json')
        self.g =Graph("neo4j://localhost:7687", auth=("neo4j", "13666871023"))


    '''读取文件'''
    def read_nodes(self):
        # 共7类节点
        print("读入数据")
        drugs = [] # 药品
        foods = [] # 食物
        checks = [] # 检查
        departments = [] #科室
        producers = [] #药品大类
        diseases = [] #疾病
        symptoms = []#症状

        disease_infos = []#疾病信息

        # 构建节点实体关系
        rels_department = [] # 科室-科室关系
        rels_noteat = [] # 疾病-忌吃食物关系
        rels_doeat = [] # 疾病-宜吃食物关系
        rels_recommandeat = [] # 疾病-推荐吃食物关系
        rels_commonddrug = [] # 疾病-通用药品关系
        rels_recommanddrug = [] # 疾病-热门药品关系
        rels_check = [] # 疾病-检查关系
        rels_drug_producer = [] # 厂商-药物关系

        rels_symptom = [] #疾病症状关系
        rels_acompany = [] # 疾病并发关系
        rels_category = [] # 疾病与科室之间的关系


        count = 0
        for data in open(self.data_path, encoding='utf-8', errors='ignore'):
            disease_dict = {}
            count += 1
            print(count)

            data_json = json.loads(data, strict=False)
            disease = data_json['name']
            disease_dict['name'] = disease
            diseases.append(disease)
            disease_dict['desc'] = ''
            disease_dict['prevent'] = ''
            disease_dict['cause'] = ''
            disease_dict['easy_get'] = ''
            disease_dict['cure_department'] = ''
            disease_dict['cure_way'] = ''
            disease_dict['cure_lasttime'] = ''
            disease_dict['symptom'] = ''
            disease_dict['cured_prob'] = ''

            if 'symptom' in data_json:
                symptoms += data_json['symptom']
                for symptom in data_json['symptom']:
                    rels_symptom.append([disease, symptom])

            if 'acompany' in data_json:
                for acompany in data_json['acompany']:
                    rels_acompany.append([disease, acompany])

            if 'desc' in data_json:
                disease_dict['desc'] = data_json['desc']

            if 'prevent' in data_json:
                disease_dict['prevent'] = data_json['prevent']

            if 'cause' in data_json:
                disease_dict['cause'] = data_json['cause']

            if 'get_prob' in data_json:
                disease_dict['get_prob'] = data_json['get_prob']

            if 'easy_get' in data_json:
                disease_dict['easy_get'] = data_json['easy_get']

            if 'cure_department' in data_json:
                cure_department = data_json['cure_department']
                if len(cure_department) == 1:
                     rels_category.append([disease, cure_department[0]])
                if len(cure_department) == 2:
                    big = cure_department[0]
                    small = cure_department[1]
                    rels_department.append([small, big])
                    rels_category.append([disease, small])

                disease_dict['cure_department'] = cure_department
                departments += cure_department

            if 'cure_way' in data_json:
                disease_dict['cure_way'] = data_json['cure_way']

            if  'cure_lasttime' in data_json:
                disease_dict['cure_lasttime'] = data_json['cure_lasttime']

            if 'cured_prob' in data_json:
                disease_dict['cured_prob'] = data_json['cured_prob']

            if 'common_drug' in data_json:
                common_drug = data_json['common_drug']
                for drug in common_drug:
                    rels_commonddrug.append([disease, drug])
                drugs += common_drug

            if 'recommand_drug' in data_json:
                recommand_drug = data_json['recommand_drug']
                drugs += recommand_drug
                for drug in recommand_drug:
                    rels_recommanddrug.append([disease, drug])

            if 'not_eat' in data_json:
                not_eat = data_json['not_eat']
                for _not in not_eat:
                    rels_noteat.append([disease, _not])

                foods += not_eat
                do_eat = data_json['do_eat']
                for _do in do_eat:
                    rels_doeat.append([disease, _do])

                foods += do_eat
                recommand_eat = data_json['recommand_eat']

                for _recommand in recommand_eat:
                    rels_recommandeat.append([disease, _recommand])
                foods += recommand_eat

            if 'check' in data_json:
                check = data_json['check']
                for _check in check:
                    rels_check.append([disease, _check])
                checks += check
            if 'drug_detail' in data_json:
                drug_detail = data_json['drug_detail']
                producer = [i.split('(')[0] for i in drug_detail]
                rels_drug_producer += [[i.split('(')[0], i.split('(')[-1].replace(')', '')] for i in drug_detail]
                producers += producer
            disease_infos.append(disease_dict)
        return set(drugs), set(foods), set(checks), set(departments), set(producers), set(symptoms), set(diseases), disease_infos,\
               rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,\
               rels_symptom, rels_acompany, rels_category

    '''建立节点'''
    def create_node(self, label, nodes):
        count = 0
        for node_name in nodes:
            node = Node(label, name=node_name)
            self.g.create(node)
            count += 1
            print(count, len(nodes))
        return

    '''创建知识图谱中心疾病的节点'''
    def create_diseases_nodes(self, disease_infos):
        count = 0
        for disease_dict in disease_infos:
            node = Node("Disease", name=disease_dict['name'], desc=disease_dict['desc'],
                        prevent=disease_dict['prevent'] ,cause=disease_dict['cause'],
                        easy_get=disease_dict['easy_get'],cure_lasttime=disease_dict['cure_lasttime'],
                        cure_department=disease_dict['cure_department']
                        ,cure_way=disease_dict['cure_way'] , cured_prob=disease_dict['cured_prob'])
            self.g.create(node)
            count += 1
            print(count)
        return

    '''创建知识图谱实体节点类型schema'''
    def create_graphnodes(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos,rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,rels_symptom, rels_acompany, rels_category = self.read_nodes()
        self.create_diseases_nodes(disease_infos)
        self.create_node('Drug', Drugs)
        print(len(Drugs))
        self.create_node('Food', Foods)
        print(len(Foods))
        self.create_node('Check', Checks)
        print(len(Checks))
        self.create_node('Department', Departments)
        print(len(Departments))
        self.create_node('Producer', Producers)
        print(len(Producers))
        self.create_node('Symptom', Symptoms)
        return


    '''创建实体关系边'''
    def create_graphrels(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug,rels_symptom, rels_acompany, rels_category = self.read_nodes()
        self.create_relationship('Disease', 'Food', rels_recommandeat, 'recommand_eat', '推荐食谱')
        self.create_relationship('Disease', 'Food', rels_noteat, 'no_eat', '忌吃')
        self.create_relationship('Disease', 'Food', rels_doeat, 'do_eat', '宜吃')
        self.create_relationship('Department', 'Department', rels_department, 'belongs_to', '属于')
        self.create_relationship('Disease', 'Drug', rels_commonddrug, 'common_drug', '常用药品')
        self.create_relationship('Producer', 'Drug', rels_drug_producer, 'drugs_of', '生产药品')
        self.create_relationship('Disease', 'Drug', rels_recommanddrug, 'recommand_drug', '好评药品')
        self.create_relationship('Disease', 'Check', rels_check, 'need_check', '诊断检查')
        self.create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', '症状')
        self.create_relationship('Disease', 'Disease', rels_acompany, 'acompany_with', '并发症')
        self.create_relationship('Disease', 'Department', rels_category, 'belongs_to', '所属科室')

    '''创建实体关联边'''
    def create_relationship(self, start_node, end_node, edges, rel_type, rel_name):
        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.g.run(query)
                count += 1
                print(rel_type, count, all)
            except Exception as e:
                print(e)
        return

    '''导出数据'''
    def export_data(self):
        Drugs, Foods, Checks, Departments, Producers, Symptoms, Diseases, disease_infos, rels_check, rels_recommandeat, rels_noteat, rels_doeat, rels_department, rels_commonddrug, rels_drug_producer, rels_recommanddrug, rels_symptom, rels_acompany, rels_category = self.read_nodes( )
        f_drug = open('drug.txt', 'w+')
        f_food = open('food.txt', 'w+')
        f_check = open('check.txt', 'w+')
        f_department = open('department.txt', 'w+')
        f_producer = open('producer.txt', 'w+')
        f_symptom = open('symptoms.txt', 'w+')
        f_disease = open('disease.txt', 'w+')

        f_drug.write('\n'.join(list(Drugs)))
        f_food.write('\n'.join(list(Foods)))
        f_check.write('\n'.join(list(Checks)))
        f_department.write('\n'.join(list(Departments)))
        f_producer.write('\n'.join(list(Producers)))
        f_symptom.write('\n'.join(list(Symptoms)))
        f_disease.write('\n'.join(list(Diseases)))

        f_drug.close()
        f_food.close()
        f_check.close()
        f_department.close()
        f_producer.close()
        f_symptom.close()
        f_disease.close()

        return



if __name__ == '__main__':
    print("开始创建")

    handler = MedicalGraph()
    print("创建结束")
    handler.export_data()
    handler.create_graphnodes()
    handler.create_graphrels()

 

标签:rels,图谱,self,disease,drug,json,导入,创建,data
From: https://www.cnblogs.com/222wan/p/18227681

相关文章

  • Microsoft.Extensions.DependencyInjection会自动释放通过DI(依赖注入)创建的对象
    Microsoft.Extensions.DependencyInjection中(下面简称DI),在调用ServiceProvider和IServiceScope对象的Dispose()方法时,也会自动调用ServiceProvider和IServiceScope对象通过DI创建的对象的Dispose()方法(前提是,通过DI创建的对象实现了IDisposable接口)。我们新建一个.NETCore控制台......
  • plsql中导入excel数据
    方法一:1、点击“工具”—“ODBC导入器…”菜单,打开导入功能 2、选择要连接类型和导入的Excel文件 3、选择所要导入数据所在的“Sheet”表 4、根据Sheet表结构创建Oracle数据表   5、完成数据导入 方法二:1、execl文件另存为“文本文件(制表符分隔)(*.txt)......
  • 创建和管理一个 CA 及证书的生命周期
    1创建一个根CA,包括生成私钥和根证书。分析证书和0015,0034标准的符合情况生成根CA私钥:生成根CA的证书签署请求(CSR):生成根CA的证书:分析符合标准:2为一台服务器生成一个私钥和证书签署请求(CSR)。生成服务器私钥:生成服务器的证书签署请求(CSR)3使用根......
  • 创建和管理一个 CA 及证书的生命周期
    任务详情0使用openssl或者gmssl,提交markdown格式文档和转化后的pdf1创建一个根CA,包括生成私钥和根证书。分析证书和0015,0034标准的符合情况2为一台服务器生成一个私钥和证书签署请求(CSR)。3使用根CA对服务器的CSR进行签名,生成服务器证书。4吊销该服务器的证书。5......
  • 导入ZIP压缩包比较图片的hash值重复
    项目中碰到需要在导入过程中和当前目录中的图片进行比较,判断是否存在相同的图片,相同则把导入的图片删除掉该内容较多:需要仔细分析每部分代码,结合你需要内容获取对应代码!!!!!!!!首先把工具类导入进来:对应hash比较工具类我是参考该作者博客:java通过哈希比较图片相似度_jav......
  • 创建和管理一个 CA 及证书的生命周期
    任务详情0使用openssl或者gmssl,提交markdown格式文档和转化后的pdf1创建一个根CA,包括生成私钥和根证书。分析证书和0015,0034标准的符合情况2为一台服务器生成一个私钥和证书签署请求(CSR)。3使用根CA对服务器的CSR进行签名,生成服务器证书。4吊销该服务器的证书。5......
  • SQLServer2022创建表及字段增加备注
    SQLServer2022创建表及字段增加备注,导入Oracle12c示例数据库HRschema下的表及数据。在SQLServer中,可以使用系统视图sys.extended_properties来查看表或字段的描述官方文档地址https://learn.microsoft.com/en-us/sql/ssms/visual-db-tools/column-properties-visual-dat......
  • 编写程序,创建一个集合Set,其中含有100个元素,每个元素是0~1000之间的一个随机数。打印
    实验代码: importrandomasrSet=set()foriinrange(100):num=r.randint(0,1000)Set.add(num)#使用列表推导式和join方法来输出结果print('{'+','.join(str(i)foriinSet)+'}')iflen(Set)==100:print("随机数没有出现重复!")......
  • 如何使用 Apache 和 Nginx 创建临时和永久重定向
    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。简介HTTP重定向,或者URL重定向,是一种将一个域名或地址指向另一个的技术。重定向有许多用途,也有几种不同的重定向方式需要考虑。当一个站点需要将请求一个地址的人重定向......
  • 如何使用 Nginx 创建临时和永久重定向
    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。简介HTTP重定向是将一个域名或地址指向另一个的方法。有几种不同类型的重定向,每种对客户端浏览器意味着不同的事情。最常见的两种类型是临时重定向和永久重定向。临时重......