neo4j实体对齐知识图谱实体对齐和实体关系的属性补全

python如何写入三元组数据到neo4j
基于Neo4j 图数据库的知识图谱的关联对齐(实体对齐)——上篇
基于Neo4j 图数据库的知识图谱的关联对齐(实体对齐)——下篇
在写入图谱前先要有相关的标准库。然后就是python简单的判断+cypher语句的结合写入。
下面直接上代码:
def companys_relation():

# 在基于 公司->人名的关系中,来补全公司属性和人物属性。
dat = session.run("MATCH (m:COMPANY)-[]->(n:PERSON) RETURN m.name as me, m.cid as cid, n.name as ne")  # 获取neo4j里面的所有子图
for d in dat:
    com = d[0]
    cids = d[1]
    nn = d[2]
    for item in collect1.find({"cid": "%s" % (cids)}):  # 从neo4j 里面拿到的实体和我们建立的标准库里面的数据来查询对比。
        if item:
            org_name = item["org_name"]
            highest_edu = item["highest_edu"]
            birthday = item["birthday"]
            # cids = item["cid"]
            gender = item["gender"]
            date = str(item["dimission_date"])
            yy = date[:4]
            mm = date[4:6]
            dd = date[6:8]
            ends = yy + "-" + mm + "-" + dd
            end = str(ends).replace("None--", "-")
            person_name = item["names"]
            print("person_name: ", person_name)
            pos_type = item['pos_type']
            positions = item["position"]
            position = str(positions).replace("(", "").replace(")", "")
            # profess_title = item["profess_title"]
            dat = str(item["start"])
            ys = dat[:4]
            ms = dat[4:6]
            ds = dat[6:8]
            starts = ys + "-" + ms + "-" + ds
            start = str(starts).replace("None--", "-")
            if nn == person_name:  # 如果数据库里面的一样
                query = "merge(e{name: '%s'}) set e.company = '%s',e.relation = '%s',e.name = '%s',e.birthday = '%s',e.highest_edu = '%s',e.gender = '%s',e.pos_type = '%s',e.start = '%s',e.end = '%s' " % (
                    nn, org_name, position, nn, birthday, highest_edu, gender, pos_type, start, end)
                session.run(query)
            else:   # 如果数据库里面的不一样
                session.run("merge (a:PERSON {name:'%s'})" % (person_name))
                query = "merge(e{name: '%s'}) set e.relation = '%s',e.name = '%s',e.birthday = '%s',e.highest_edu = '%s',e.gender = '%s',e.pos_type = '%s',e.start = '%s',e.end = '%s' " % (
                    person_name, position, person_name, birthday, highest_edu, gender, pos_type, start, end)
                session.run(query)
                query = "MATCH(p:COMPANY),(q:PERSON) where p.name='%s' and q.name='%s' create unique (p)-[:%s {relation:'%s',name:'%s',birthday:'%s',highest_edu:'%s',gender:'%s',pos_type:'%s',start:'%s',end:'%s' }]->(q)" % (
                    com, person_name, position, position, person_name, birthday,highest_edu, gender, pos_type, start, end)
                session.run(query)

if name == ‘main’:
companys_relation()

你可能感兴趣的:(neo4j)