命名体是识别后关系抽取及知识图谱扩充(第三步,知识图谱的填充)

项目场景:

前面两步,我们已经把所有的前期准备都已经完成,最后我们回归步骤一的本源,沉积学知识图谱的补充和扩建,我识别出文本内关键信息实体,提取到实体见的关系,那我就可以对知识图谱进行反向补充,这样就实现了学术界human in the loop的方案。


知识图谱的补充

这里首先我们先要连接本地的知识图谱库

__author__ = "Huzhichen"
#coding=utf-8
import json
from py2neo import Graph, Node, Relationship, cypher, NodeMatcher, RelationshipMatcher
graph = Graph("http://localhost:7474", auth=("neo4j", "admin"))

这里我举个例子,之后所有的数据填充可根据自己提取的数据自行补充,这里只展现关键的代码,第一步创建节点,在创建前我们要先进行查找如果没有才能创建,如果有就不创建,.replace(" “,”")).where(".name="+"’"+long[index]+"’").exists()这里为Neo4j 3,py2neio为2021版本,这也是目前固定写法,照抄里面数组就行
在这里插入图片描述

matcher = NodeMatcher(graph)
# 测试及查询创建实体节点
has = ['Limestone','Clasts']
long = ['clasts','micritic limestone','pink pelagic limestone']
# 创建节点
for index in range(len(long)):
    if matcher.match(long[index].replace(" ","_")).where("_.name="+"\'"+long[index]+"\'").exists() == False:
        loc_node= Node(long[index].replace(" ","_"),label=long[index].replace(" ","_"),name=long[index])
        graph.create(loc_node)
        print(long[index]+"节点不存在正在创建")
    else:
        loc_node=matcher.match(long[index].replace(" ","_")).where("_.name="+"\'"+long[index]+"\'").first()
        print(long[index]+"节点已经存在")

for index in range(len(has)):
    if matcher.match(has[index].replace(" ","_")).where("_.name="+"\'"+has[index]+"\'").exists() == False:
        has_node= Node(has[index].replace(" ","_"),label=has[index].replace(" ","_"),name=has[index])
        graph.create(has_node)
        print(has[index]+"节点不存在正在创建")
    else:
        has_node=matcher.match(has[index].replace(" ","_")).where("_.name="+"\'"+has[index]+"\'").first()
        print(has[index]+"节点已经存在")

最后创建关系,这里的关系,在数据导入Neo4j之前,已经做好了分类,肯定是前面两个关系相同的数组导入在导入关系。

# 创建节点之间的连接
for index in range(len(long)):
    for hasindex in range(len(has)):
        if (long[index].lower().find(has[hasindex].lower()) != -1):
            loc_node = matcher.match(long[index].replace(" ", "_")).where("_.name=" + "\'" + long[index] + "\'").first()
            has_node = matcher.match(has[hasindex].replace(" ", "_")).where("_.name=" + "\'" + has[hasindex] + "\'").first()
            relationshipLevel = Relationship(has_node, 'subclass', loc_node)
            graph.create(relationshipLevel)
            print("关键创建成功")

效果图如下

在这里插入图片描述

目前节点:2614 关系:2613 数量:600篇文献
GitHub代码如下:https://github.com/zhichen-roger/Relationship_extraction_and_knowledge-graph-supplementation.git

如果对你的文章有帮助请引用目前发表的文章

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

经常喝假酒的胡小臣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值