用Python构造neo4j知识图谱(关于中成药的)

代码介绍

用Python先处理csv文件数据,在自动化批量在neo4j生成节点。

从文件中导出数据,并进行处理

# 数据写入
datalists = []
with open('zcydata.csv','r',encoding='utf-8') as f:
    listLists=f.readlines()
for lists in listLists:
    datalists.append(lists.replace('</P>','').replace('<P>','').replace('\n','').split(',',14))

# 用途的数据导入
Datalists = []
with open('zz_data.csv', 'r', encoding='utf-8') as f:
    ListLists = f.readlines()
for lists in ListLists:
    Datalists.append(lists.replace('</P>', '').replace('<P>', '').replace('\n', '').split(',', 14))


# 组成的数据导入
Datals = []
with open('zc_data.csv', 'r', encoding='utf-8') as f:
    ListLists=f.readlines()
for lists in ListLists:
    Datals.append(lists.replace('</P>','').replace('<P>','').replace('\n','').split(',',14))

创建节点和关系


# 创建节点关系
i = 0
tx = graph.begin()
for items in datalists:
    # 药的名称
    a = Node('zcyname', name=items[0])
    tx.create(a)

    a1 = Node('shuxing', name='信息')
    a2 = Node('shuxing', name='经济性')
    a3 = Node('shuxing', name='安全性')
    tx.create(a1)
    tx.create(a2)
    tx.create(a3)
    r1 = Relationship(a, 'xinxi', a1)
    r2 = Relationship(a, 'jingjixing', a2)
    r3 = Relationship(a, 'anquanxing', a3)
    tx.create(r1)
    tx.create(r2)
    tx.create(r3)
    # 第一层关系
    b1 = Node('wxyj', name='文献研究')
    b2 = Node('ypjj', name='药品禁忌')
    b3 = Node('blfy', name='不良反应')
    tx.create(b1)
    tx.create(b2)
    tx.create(b3)
    r11 = Relationship(a3, 'wxyj', b1)
    r12 = Relationship(a3, 'ypjj', b2)
    r13 = Relationship(a3, 'blfy', b3)
    tx.create(r11)
    tx.create(r12)
    tx.create(r13)

    # 文献研究
    if items[7] != '':
        b11 = Node('wxnum', name=items[7])
        tx.create(b11)
        r1_1 = Relationship(b1, 'num', b11)
        tx.create(r1_1)

    # 药品禁忌
    if items[12] != '':
        ypjj_list = items[12].split('。')
        for ypjj in ypjj_list:
            if ypjj !='':
                b12 = Node('ypjj' ,name=ypjj)
                tx.create(b12)
                r1_2 = Relationship(b2, 'ypjj', b12)
                tx.create(r1_2)

    # 不良反应
    if items[13] != '':
        blfy_list = items[13].split('。')
        for blfy in blfy_list:
            if blfy != '':
                b13 = Node('ypjj' ,name=blfy)
                tx.create(b13)
                r1_3 = Relationship(b3, 'blfy', b13)
                tx.create(r1_3)

    c1 = Node('xingzhuang', name='性状')
    c2 = Node('zygg', name='主要规格')
    c3 = Node('yt', name='用途')
    c4 = Node('yfyl', name='用法用量')
    c5 = Node('zc', name='组成')
    c6 = Node('cpfl', name='产品分类')
    c7 = Node('sccj', name='生产厂家')
    tx.create(c1)
    tx.create(c2)
    tx.create(c3)
    tx.create(c4)
    tx.create(c5)
    tx.create(c6)
    tx.create(c7)
    r21 = Relationship(a1, '包含', c1)
    r22 = Relationship(a1, '包含', c2)
    r23 = Relationship(a1, '包含', c3)
    r24 = Relationship(a1, '包含', c4)
    r25 = Relationship(a1, '包含', c5)
    r26 = Relationship(a1, '包含', c6)
    r27 = Relationship(a1, '包含', c7)
    tx.create(r21)
    tx.create(r22)
    tx.create(r23)
    tx.create(r24)
    tx.create(r25)
    tx.create(r26)
    tx.create(r27)

    # 性状节点
    if items[6] != '':
        c11 = Node('xingzhuang', name=items[6])
        tx.create(c11)
        r1_11 = Relationship(c1, 'xingzhuang', c11)
        tx.create(r1_11)

    # 主要规格
    if items[1] != '':
        c21 = Node('zygg', name=items[1])
        tx.create(c21)
        r2_21 = Relationship(c2, 'zygg', c21)
        tx.create(r2_21)

    # 用途
    for its in Datalists[i]:
        # print(its)
        if its != '':
            Datas = re.split(',', its)
            # print(Datas)
            for Data in Datas:
                if Data != '':
                    c31 = Node('yt', name=Data)
                    tx.create(c31)
                    r3_31 = Relationship(c3, 'yt', c31)
                    tx.create(r3_31)
                    # print(Data)
        break

    # 用法用量
    if items[3] != '':
        c41 = Node('yfyl', name='口服,一次15毫升,一日3~4次。')
        tx.create(c41)
        r4_41 = Relationship(c4, 'yfyl', c41)
        tx.create(r4_41)

    # 组成节点
    if Datals[i][1] != '':
        datas = re.split('、', Datals[i][1])
        # print(datas)
        for data in datas:
            if data != '':
                c51 = Node('zc', name=data)
                tx.create(c51)
                r5_51 = Relationship(c5, 'zc', c51)
                tx.create(r5_51)

    # 产品分类
    if items[4] != '':
        c61 = Node('cpfl', name=items[4])
        tx.create(c61)
        r6_61 = Relationship(c6, 'cpfl', c61)
        tx.create(r6_61)

    # 生产厂家
    if items[5] != '':
        c71 = Node('sccj', name=items[5])
        tx.create(c71)
        r7_71 = Relationship(c7, 'sccj', c71)
        tx.create(r7_71)

    d1 = Node('jjml', name='基药目录')
    d2 = Node('ybml', name='医保目录')
    d3 = Node('bzly', name='标准来源')
    tx.create(d1)
    tx.create(d2)
    tx.create(d3)
    r31 = Relationship(a2, 'jyml', d1)
    r32 = Relationship(a2, 'yb', d2)
    r33 = Relationship(a2, 'bzly', d3)
    tx.create(r31)
    tx.create(r32)
    tx.create(r33)

    # 医保
    if items[9] != '':
        d12 = Node('yb', name='非医保')
        tx.create(d12)
        r2_12 = Relationship(d2, 'yb', d12)
        tx.create(r2_12)

    # 基药目录
    if items[10] != '':
        d11 = Node('jyml', name=items[10])
        tx.create(d11)
        r1_11 = Relationship(d1, 'jyml', d11)
        tx.create(r1_11)

    # 标准来源
    if items[11] != '':
        d13 = Node('bzly', name=items[11])
        tx.create(d13)
        r3_13 = Relationship(d3, 'yb', d13)
        tx.create(r3_13)
    i += 1
tx.commit()

1、zc_data.csv文件
在这里插入图片描述
2、zz-data.csv文件
在这里插入图片描述
3、zcydata.csv文件
在这里插入图片描述

效果展示

在这里插入图片描述

源代码及相关文件下载

下载

  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值