创建一个基于《西游记》中"孙悟空"的黑神话版本的知识图谱。这个图谱将会包括《西游记》中的一些主要角色、地点、事件以及它们之间的关系。我们将创建至少10个节点和20个关系,并提供相应的Cypher语句。
数据模型定义
实体类型(节点)
- 角色 (Character)
- 地点 (Location)
- 事件 (Event)
- 武器 (Weapon)
- 技能 (Skill)
关系类型
- 居住于 (LivesIn)
- 参与 (ParticipatesIn)
- 使用 (Uses)
- 拥有 (Has)
- 敌对 (FoesWith)
- 朋友 (FriendsWith)
数据准备
假设我们有如下数据:
- 角色: "孙悟空","唐僧","猪八戒","沙僧","白骨精","牛魔王"
- 地点: "花果山","五行山","天宫","火焰山"
- 事件: "取经之路","大闹天宫","三打白骨精"
- 武器: "金箍棒","九齿钉耙","降妖杖"
- 技能: "七十二变","筋斗云","定身法"
创建知识图谱
创建节点
CREATE (s1:Character {name: "孙悟空", role: "主角"})
CREATE (s2:Character {name: "唐僧", role: "师傅"})
CREATE (s3:Character {name: "猪八戒", role: "徒弟"})
CREATE (s4:Character {name: "沙僧", role: "徒弟"})
CREATE (s5:Character {name: "白骨精", role: "妖怪"})
CREATE (s6:Character {name: "牛魔王", role: "妖怪"})
CREATE (l1:Location {name: "花果山", description: "孙悟空的家乡"})
CREATE (l2:Location {name: "五行山", description: "孙悟空被压之地"})
CREATE (l3:Location {name: "天宫", description: "神仙居住的地方"})
CREATE (l4:Location {name: "火焰山", description: "牛魔王的领地"})
CREATE (e1:Event {name: "取经之路", description: "唐僧师徒四人的旅途"})
CREATE (e2:Event {name: "大闹天宫", description: "孙悟空与天庭的冲突"})
CREATE (e3:Event {name: "三打白骨精", description: "孙悟空与白骨精的战斗"})
CREATE (w1:Weapon {name: "金箍棒", description: "孙悟空的法宝"})
CREATE (w2:Weapon {name: "九齿钉耙", description: "猪八戒的武器"})
CREATE (w3:Weapon {name: "降妖杖", description: "沙僧的武器"})
CREATE (sk1:Skill {name: "七十二变", description: "孙悟空的技能"})
CREATE (sk2:Skill {name: "筋斗云", description: "孙悟空的技能"})
CREATE (sk3:Skill {name: "定身法", description: "孙悟空的技能"})
创建关系
MATCH (s1:Character), (l1:Location)
WHERE s1.name = "孙悟空" AND l1.name = "花果山"
CREATE (s1)-[:LivesIn]->(l1)
MATCH (s1:Character), (e1:Event)
WHERE s1.name = "孙悟空" AND e1.name = "取经之路"
CREATE (s1)-[:ParticipatesIn]->(e1)
MATCH (s1:Character), (e2:Event)
WHERE s1.name = "孙悟空" AND e2.name = "大闹天宫"
CREATE (s1)-[:ParticipatesIn]->(e2)
MATCH (s1:Character), (e3:Event)
WHERE s1.name = "孙悟空" AND e3.name = "三打白骨精"
CREATE (s1)-[:ParticipatesIn]->(e3)
MATCH (s1:Character), (w1:Weapon)
WHERE s1.name = "孙悟空" AND w1.name = "金箍棒"
CREATE (s1)-[:Uses]->(w1)
MATCH (s1:Character), (sk1:Skill)
WHERE s1.name = "孙悟空" AND sk1.name = "七十二变"
CREATE (s1)-[:Has]->(sk1)
MATCH (s1:Character), (sk2:Skill)
WHERE s1.name = "孙悟空" AND sk2.name = "筋斗云"
CREATE (s1)-[:Has]->(sk2)
MATCH (s1:Character), (sk3:Skill)
WHERE s1.name = "孙悟空" AND sk3.name = "定身法"
CREATE (s1)-[:Has]->(sk3)
MATCH (s2:Character), (l2:Location)
WHERE s2.name = "唐僧" AND l2.name = "五行山"
CREATE (s2)-[:LivesIn]->(l2)
MATCH (s2:Character), (e1:Event)
WHERE s2.name = "唐僧" AND e1.name = "取经之路"
CREATE (s2)-[:ParticipatesIn]->(e1)
MATCH (s3:Character), (l1:Location)
WHERE s3.name = "猪八戒" AND l1.name = "花果山"
CREATE (s3)-[:LivesIn]->(l1)
MATCH (s3:Character), (e1:Event)
WHERE s3.name = "猪八戒" AND e1.name = "取经之路"
CREATE (s3)-[:ParticipatesIn]->(e1)
MATCH (s3:Character), (w2:Weapon)
WHERE s3.name = "猪八戒" AND w2.name = "九齿钉耙"
CREATE (s3)-[:Uses]->(w2)
MATCH (s4:Character), (l1:Location)
WHERE s4.name = "沙僧" AND l1.name = "花果山"
CREATE (s4)-[:LivesIn]->(l1)
MATCH (s4:Character), (e1:Event)
WHERE s4.name = "沙僧" AND e1.name = "取经之路"
CREATE (s4)-[:ParticipatesIn]->(e1)
MATCH (s4:Character), (w3:Weapon)
WHERE s4.name = "沙僧" AND w3.name = "降妖杖"
CREATE (s4)-[:Uses]->(w3)
MATCH (s5:Character), (l4:Location)
WHERE s5.name = "白骨精" AND l4.name = "火焰山"
CREATE (s5)-[:LivesIn]->(l4)
MATCH (s5:Character), (e3:Event)
WHERE s5.name = "白骨精" AND e3.name = "三打白骨精"
CREATE (s5)-[:ParticipatesIn]->(e3)
MATCH (s6:Character), (l4:Location)
WHERE s6.name = "牛魔王" AND l4.name = "火焰山"
CREATE (s6)-[:LivesIn]->(l4)
MATCH (s1:Character), (s5:Character)
WHERE s1.name = "孙悟空" AND s5.name = "白骨精"
CREATE (s1)-[:FoesWith]->(s5)
MATCH (s1:Character), (s2:Character)
WHERE s1.name = "孙悟空" AND s2.name = "唐僧"
CREATE (s1)-[:FriendsWith]->(s2)
查询及推理
查询示例
-
查找所有居住在花果山的角色
MATCH (c:Character)-[r:LivesIn]->(l:Location) WHERE l.name = "花果山"
RETURN c.name
找出孙悟空拥有的所有技能
*
MATCH (s1:Character)-[r:Has]->(sk:Skill) WHERE s1.name = "孙悟空"
RETURN sk.name
分析参与了"取经之路"事件的所有角色及其武器
*
MATCH (c:Character)-[r:ParticipatesIn]->(e:Event)<-[r2:Uses]-(w:Weapon) WHERE e.name = "取经之路"
RETURN c.name, w.name
推理示例
- 寻找所有与孙悟空敌对的角色及其居住地点
MATCH (s1:Character)-[r:FoesWith]->(s2:Character)-[r2:LivesIn]->(l:Location) WHERE s1.name = "孙悟空"
RETURN s2.name, l.name
找出参与了"大闹天宫"事件的角色以及他们拥有的技能
*
MATCH (c:Character)-[r:ParticipatesIn]->(e:Event)<-[r2:Has]-(sk:Skill) WHERE e.name = "大闹天宫"
RETURN c.name, sk.name