知识图谱:Neo4j数据库的基本使用——创建张学良的关系谱

一、知识图谱及Neo4j数据库介绍

        知识图谱(Knowledge Graph)是人工智能的重要分支技术,它在2012年由谷歌提出,是结构化的语义知识库,用于以符号形式描述物理世界中的概念及其相互关系,其基本组成单位是“实体—关系—实体”三元组,以及实体及其相关属性—值对,实体间通过关系相互联结,构成网状的知识结构。

        Neo4j 是一种开源的、基于 Graph 数据库的工具,具有简单的图形用户界面 (GUI) 和命令行界面 (CLI)

 图1 neo4j

二、知识图谱制作

首先需要启动neo4j:在cmd内输入neo4j.bat console即可启动neo4j 进入7474端口节即可进入neo4j

图2 启动neo4j 

创建节点

create (n:person{name:"张学良"})

图3 创建张学良节点

        其中create是创建操作,n没有具体意义,只代表某个标签。Person是label标签名,代表节点类型;花括号{}代表节点的属性,属性可以为name,age等等。这条语句的含义就是创建一个标签为Person的节点,该节点具有一个name属性,值是张学良。还可以给该节点定义其他属性,通常一个label包含若干个属性。单击table可以看到。

图4 查看张学良节点

create (zcg:relatives {name:"赵春桂"}),
(yfz:relatives {name:"于凤至"}),
(gry:relatives {name:"谷瑞玉"}),
(zyd:relatives {name:"赵一荻"}),
(zlying:relatives {name:"张闾瑛"}),
(zlx:relatives {name:"张闾珣"}),
(zlyv:relatives {name:"张闾玗"}),
(zlq:relatives {name:"张闾琪"}),
(zll:relatives {name:"张闾琳"}),
(zxj:relatives {name:"张学浚"}),
(zxm:relatives {name:"张学铭"}),
(zxs:relatives {name:"张学思"}),
(zxsh:relatives {name:"张学栓"}),
(zjx:relatives {name:"张居信"}),
(zzl:relatives {name:"张作霖"}),
(zxc:relatives {name:"张学曾"}),
(zxch:relatives {name:"张学成"})

图5 创建其他人物节点

由Neo4j 数据库中的关系型数据库创建的图形数据库模型,其中包含一些人物及其亲戚关系。每个节点 (人) 都包含一个名称属性,而每个边 (关系) 都包含一个属性,用于描述两个节点之间的关系。

在这个模型中,人物赵春桂、于凤至、谷瑞玉、赵一荻、张闾瑛、张闾珣、张闾玗、张闾琪、张闾琳、张学浚、张学铭、张学思、张学栓、张居信、张作霖、张学曾和张学成都有自己的亲戚关系。这些亲戚关系是通过边连接这些节点的,例如,赵春桂和张闾琳是亲戚关系,因为赵春桂是张闾琳的祖母。

创建张学良属性节点

create (sexm:property{property:"男"}),
(sexw:property{property:"女"}),
(alias:property{property:"双喜、小六子、张汉卿、张少帅"}),
(nationality:property{property:"中国"}),
(nation:property{property:"汉"}),
(Birthdate:property{property:"1901年6月3日(辛丑年)"}),
(dateOfDeath:property{property:"2001年10月14日(辛巳年)"}),(graduateInstitution:property{property:"东北陆军讲武堂"}),
(job:property{property:"军人"}),
(birthplace:property{property:"辽宁省鞍山市台安县"}),
(militaryRank:property{property:"国民党陆军一级上将"})

图6 创建属性节点

创建节点 (person) 节点表示人物。

给节点添加属性 (property),其中 sexm 表示节点为男性,sexw 表示节点为女性,alias 表示节点的别名,nationality 表示节点的国籍,nation 表示节点的民族,Birthdate 表示节点的生日,dateOfDeath 表示节点的死亡日期,graduateInstitution 表示节点的毕业院校,job 表示节点的职业,birthplace 表示节点的出生地,militaryRank 表示节点的军衔。

在节点上创建属性值 (value)。例如,在节点 nationality 上创建了一个属性值 "中国"。

创建关联 (relation)。例如,在节点 nationality 上创建了一个关联 "belongTo" 给节点 nation。

创建节点间的关系

//建立节点之间的关系
match(n:person),(sexm:property) where n.name="张学良" and sexm.property="男" create(n)-[r:sex]- >(sexm);

match(n:person),(alias:property) where n.name="张学良" and alias.property="双喜、小六子、张汉卿、张少帅" create(n)-[r:alias]- >(alias);

match(n:person),(nationality:property) where n.name="张学良" and nationality.property="中国" create(n)-[r:nationality]- >(nationality);

match(n:person),(nation:property) where n.name="张学良" and nation.property="汉" create(n)-[r:nation]->(nation);

match(n:person),(Birthdate:property) where n.name="张学良" and Birthdate.property="1901年6月3日(辛丑年)" create(n)-[r:Birthdate]->(Birthdate);

match(n:person),(dateOfDeath:property) where n.name="张学良" and dateOfDeath.property="2001年10月14日(辛巳年)" create(n)-[r:dateOfDeath]->(dateOfDeath);

match(n:person),(graduateInstitution:property) where n.name="张学良" and graduateInstitution.property="东北陆军讲武堂" create(n)-[r:graduateInstitution]->(graduateInstitution);

match(n:person),(job:property) where n.name="张学良" and job.property="军人" create(n)-[r:job]->(job);

match(n:person),(birthplace:property) where n.name="张学良" and birthplace.property="辽宁省鞍山市台安县" create(n)-[r:birthplace]->(birthplace);

match(n:person),(militaryRank:property) where n.name="张学良" and militaryRank.property="国民党陆军一级上将" create(n)-[r:militaryRank]->(militaryRank);

//张学良的亲属
match(n:person),(zcg:relatives) where n.name="张学良" and zcg.name="赵春桂" create(n)-[r:mother]- >(zcg);

match(n:person),(yfz:relatives) where n.name="张学良" and yfz.name="于凤至" create(n)-[r:ex_wife]- >(yfz);

match(n:person),(gry:relatives) where n.name="张学良" and gry.name="谷瑞玉" create(n)-[r:ex_wife]- >(gry);

match(n:person),(zyd:relatives) where n.name="张学良" and zyd.name="赵一荻" create(n)-[r:wife]- >(zyd);

match(n:person),(zly:relatives) where n.name="张学良" and zly.name="张闾瑛" create(n)-[r:daughter]- >(zly);

match(n:person),(zlx:relatives) where n.name="张学良" and zlx.name="张闾珣" create(n)-[r:son]- >(zlx);

match(n:person),(zlyv:relatives) where n.name="张学良" and zlyv.name="张闾玗" create(n)-[r:son]- >(zlyv);

match(n:person),(zlq:relatives) where n.name="张学良" and zlq.name="张闾琪" create(n)-[r:son]- >(zlq);

match(n:person),(zll:relatives) where n.name="张学良" and zll.name="张闾琳" create(n)-[r:son]- >(zll);

match(n:person),(zxj:relatives) where n.name="张学良" and zxj.name="张学浚" create(n)-[r:youngerBrother]- >(zxj);

match(n:person),(zxm:relatives) where n.name="张学良" and zxm.name="张学铭" create(n)-[r:youngerBrother]- >(zxm);

match(n:person),(zxs:relatives) where n.name="张学良" and zxs.name="张学思" create(n)-[r:youngerBrother]- >(zxs);

match(n:person),(zxsh:relatives) where n.name="张学良" and zxsh.name="张学栓" create(n)-[r:youngerBrother]- >(zxsh);

match(n:person),(zxc:relatives) where n.name="张学良" and zxc.name="张学曾" create(n)-[r:youngerBrother]- >(zxc);

match(n:person),(zjx:relatives) where n.name="张学良" and zjx.name="张居信" create(n)-[r:grandson]- >(zjx);

match(n:person),(zzl:relatives) where n.name="张学良" and zzl.name="张作霖" create(n)-[r:father]- >(zzl);

match(n:person),(zxch:relatives) where n.name="张学良" and zxch.name="张学成" create(n)-[r:cousin]- >(zxch);



match(zzl:relatives),(n:person) where zzl.name="张作霖" and n.name="张学良" create(zzl)-[r:son]- >(n);

match(zzl:relatives),(sexm:property) where zzl.name="张作霖" and sexm.property="男" create(zzl)-[r:sex]- >(sexm);

match(zzl:relatives),(nation:property) where zzl.name="张作霖" and nation.property="汉"  create(zzl)-[r:nation]- >(nation);

 

图7 最终结果

        以上就为创建neo4j知识图谱的基本应用,适合初学者使用。后续可爬取CSV文件直接导入关系与节点。

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天海一直在AI

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

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

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

打赏作者

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

抵扣说明:

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

余额充值