基于Neo4j 图数据库的知识图谱的关联对齐(实体对齐)——下篇

基于Neo4j 图数据库的知识图谱的关联对齐(实体对齐)——上篇
上一篇写道了,算出2个实体的相似性。本篇开始写算出 2个实体相似性后,用代码实现neo4j的对齐操作。
直接上代码:
huizon = (jacd+edit)/2
if 0.70 < huizon < 1: # 取一个值的区间
# 判断a 和 b 的长度,目的是留下字数长的实体,然后把字数短的实体去掉。
if len(a) > len(b): # 如果a实体比b实体要长 ,删除b
rel_a,rel_b = deleted(a, b, sd)
if rel_a == rel_b: # 如果关系一样 直接删除
session.run("match(n) where n.name=’%s’ detach delete n " % (b))
else: # 如果关系不一样先把关系拿出来迁移到另外一个相似的实体上,然后在进行删除。
rels = session.run(“MATCH (n)-[r]->(m) where n.name=’%s’ and m.name=’%s’ RETURN r.relation” % (b, sd))
rls = rels.values()[0][0]
if rls:
session.run("MATCH§,(q) where p.name=’%s’and q.name=’%s’ create unique §-[:%s {relation:’%s’}]->(q) " % (a, sd, rls, rls))
session.run("match(n) where n.name=’%s’ detach delete n " % (b))
else: # 如果b实体比a实体要长 ,删除a
rel_a, rel_b = deleted(a, b, sd)
if rel_a == rel_b:
session.run("match(n) where n.name=’%s’ detach delete n " % (a))
else:
rels = session.run(“MATCH (n)-[r]->(m) where n.name=’%s’ and m.name=’%s’ RETURN r.relation” % (a, sd))
rlsd = rels.values()[0][0]
if rlsd:
session.run("MATCH§,(q) where p.name=’%s’and q.name=’%s’ create unique §-[:%s {relation:’%s’}]->(q) " % (b, sd, rlsd, rlsd))
session.run("match(n) where n.name=’%s’ detach delete n " % (a))

发布了25 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/for_yayun/article/details/100971894