简单的neo4j三元组增量插入-通过py2neo实现

news/2024/7/4 10:02:31

今天写了一个简单增量插入三元组的程序

1、查找实体类型对应的实例,放入list中,用于实例查重:

# 查找实体类型对应实例,返回list
def get_all_entities_of_ent_typ(graph, ent_typ):
    matcher = NodeMatcher(graph)
    ent_list = list(matcher.match(ent_typ))
    ent_list = [ent['name'] for ent in ent_list]
    return ent_list

2、对三元组中头实体已存在、尾实体已存在、三元组存在、三元组不存在来对实体进行处理,如果存在当前实体类型对应的实例则合并,不存在则插入。

3、关系通过简单的设定是否允许存在1对多和多对1的关系来提前设定。

# 三元组插入neo4j
def triples2neo4j(graph, triples, one2many=False, many2one=False): # 允许一对多关系,允许多对一关系
    for triple in triples:
        # 取出头实体、尾实体、关系
        ent_1, ent_2, rel = triple
        head, head_typ = ent_1
        head_node = Node(head_typ, name=head)
        tail, tail_typ = ent_2
        tail_node = Node(tail_typ, name=tail)
        # head类型list
        head_list = get_all_entities_of_ent_typ(graph, head_typ)
        # tail类型list
        tail_list = get_all_entities_of_ent_typ(graph, tail_typ)
        # 头实体和尾实体都存在
        if head in head_list and tail in tail_list:
            graph.merge(head_node, head_typ, "name")
            graph.merge(tail_node, tail_typ, "name")
            if list(RelationshipMatcher(graph).match((head_node, tail_node), r_type = rel)):
                print(f'三元组 ({head} ,{tail} ,{rel}) 已存在于图谱中,插入失败!')
            else:
                graph.create(Relationship(head_node, rel, tail_node))
                print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')
        # 头实体已存在
        elif head in head_list and tail not in tail_list:
            graph.merge(head_node, head_typ, "name")
            if list(RelationshipMatcher(graph).match((head_node, None), r_type = rel)):
                if one2many == False:
                    print(f'头实体 {head} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')
                    continue
            graph.create(tail_node)
            graph.create(Relationship(head_node, rel, tail_node))
            print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')
        # 尾实体已存在
        elif head not in head_list and tail in tail_list:
            graph.merge(tail_node, tail_typ, "name")
            if list(RelationshipMatcher(graph).match((None, tail_node), r_type = rel)):
                if many2one == False:
                    print(f'尾实体 {tail} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')   
                    continue             
            graph.create(head_node)
            graph.create(Relationship(head_node, rel, tail_node))
            print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')
        # 头实体、尾实体均不存在
        else:                    
            graph.create(head_node)
            graph.create(tail_node)
            graph.create(Relationship(head_node, rel, tail_node))
            print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')

所有代码如下:

# 查找实体类型对应实例,返回list
def get_all_entities_of_ent_typ(graph, ent_typ):
    matcher = NodeMatcher(graph)
    ent_list = list(matcher.match(ent_typ))
    ent_list = [ent['name'] for ent in ent_list]
    return ent_list

# 三元组插入neo4j
def triples2neo4j(graph, triples, one2many=False, many2one=False): # 允许一对多关系,允许多对一关系
    for triple in triples:
        # 取出头实体、尾实体、关系
        ent_1, ent_2, rel = triple
        head, head_typ = ent_1
        head_node = Node(head_typ, name=head)
        tail, tail_typ = ent_2
        tail_node = Node(tail_typ, name=tail)
        # head类型list
        head_list = get_all_entities_of_ent_typ(graph, head_typ)
        # tail类型list
        tail_list = get_all_entities_of_ent_typ(graph, tail_typ)
        # 头实体和尾实体都存在
        if head in head_list and tail in tail_list:
            graph.merge(head_node, head_typ, "name")
            graph.merge(tail_node, tail_typ, "name")
            if list(RelationshipMatcher(graph).match((head_node, tail_node), r_type = rel)):
                print(f'三元组 ({head} ,{tail} ,{rel}) 已存在于图谱中,插入失败!')
            else:
                graph.create(Relationship(head_node, rel, tail_node))
                print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')
        # 头实体已存在
        elif head in head_list and tail not in tail_list:
            graph.merge(head_node, head_typ, "name")
            if list(RelationshipMatcher(graph).match((head_node, None), r_type = rel)):
                if one2many == False:
                    print(f'头实体 {head} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')
                    continue
            graph.create(tail_node)
            graph.create(Relationship(head_node, rel, tail_node))
            print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')
        # 尾实体已存在
        elif head not in head_list and tail in tail_list:
            graph.merge(tail_node, tail_typ, "name")
            if list(RelationshipMatcher(graph).match((None, tail_node), r_type = rel)):
                if many2one == False:
                    print(f'尾实体 {tail} 已存在关系 {rel} 对应的三元组 ({head} ,{tail} ,{rel}),插入失败!')   
                    continue             
            graph.create(head_node)
            graph.create(Relationship(head_node, rel, tail_node))
            print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')
        # 头实体、尾实体均不存在
        else:                    
            graph.create(head_node)
            graph.create(tail_node)
            graph.create(Relationship(head_node, rel, tail_node))
            print(f'三元组 ({head} ,{tail} ,{rel}) 插入成功!')

triples = [
    (['李沐','Per'], ['CMU', 'Sch'], '毕业于'),
    (['李沐', 'Per'], ['沐神的小迷弟', 'Per'], '迷弟'),
    (['李沐','Per'], ['中国', 'Cou'], '出生于'),
    (['李沐','Per'], ['亚马逊', 'Com'], '就职于'),
    (['沐神的小迷弟', 'Per'], ['西安交通大学', 'Sch'], '就读于'),
    (['李沐','Per'], ['上海交通大学', 'Sch'], '毕业于'),
    (['李沐','Per'], ['百度', 'Com'], '就职于'),
        ]
triples2neo4j(graph, triples, one2many=False, many2one=False)

运行结果:


http://www.niftyadmin.cn/n/4411451.html

相关文章

Steve Jobs的十句金玉良言

http://blog.csdn.net/zero8500/archive/2008/09/27/2987613.aspx

论文阅读《KnowPrompt: Knowledge-aware Prompt-tuning withSynergistic Optimization for Relation Extractio》

论文链接KnowPrompt: Knowledge-aware Prompt-tuning with Synergistic Optimization for Relation Extraction Introduction 现有关系抽取存在的问题: 基于微调的关系抽取方法: (1)性能严重依赖耗时和劳动密集型的注释数据,难以很好地泛…

IE浏览器无法查看源文件的8大原因

问:无论是使用Outlook还是IE,点击鼠标右键,在弹出的快捷菜单中都会有“查看源文件”这一选项,奇怪的是,在我的电脑上竟然无法显示该邮件或网页的源文件,虽然这并没有影响到该软件的正常使用。请问&#xff…

论文阅读《Knowledge Collaborative Fine-tuning for Low-resource Knowledge GraphCompletion》

论文链接 基于知识协同微调的低资源知识图谱补全方法 2022年3月发表于软件学报 是浙大prompt系列的一个延续 本文之前的工作: AdaPrompt: Adaptive Prompt-based Finetuning for Relation Extraction 本文之后的工作: Knowledge-aware Prompt-tun…

北京几个区组成

北京由几个区组成北京,中华人民共和国首都,中央人民政府直辖市。建国后,北京市的行政区属有过多次变动,2006年,北京市辖16个市辖区、2个县。 市辖区 东城区 西城区 崇文区 宣武区 朝阳区 海淀区 丰台区 石景山区 门头沟…

论文阅读《Does William Shakespeare REALLY Write Hamlet? Knowledge RepresentationLearning with Confidenc》

Does William Shakespeare REALLY Write Hamlet? Knowledge Representation Learning with Confidence INTRODUCTION Q:可信的知识图谱构建方法去哪找? ①、传统的知识图谱构建方法通常需要大量的人工监督或专家标注,费时费力。 ②、自动化机制和众包在知识构…

北京的火车站

北京的火车站北京有4个火车站,分别是:北京站,北京西站,北京南站,北京北站。 下面是各个站的介绍: 北京站:北京站主要负责京沪线、京哈线的客运列车。 http://baike.baidu.com/view/49121.htm…

论文阅读《Relation Extraction as Open-book Examination:Retrieval-enhanced Prompt Tuning》

Relation Extraction as Open-book Examination: Retrieval-enhanced Prompt Tuning 浙大基于prompt的关系抽取最新论文,刷新prompt方法的SOTA。 因为之前对prompt类关系抽取方法已经做了很多介绍,所以现在直接看方法。 BACKGROUND 对于难度较大的实…