知识图谱在工商企业、交往圈模型、系统架构、血缘关系、关联聚合场景、区域最短路径上都能发挥很大的作用,本笔记也只是简单的介绍了一下,介绍到此为止。
上述的场景的数据来源都来自于关系型数据,想让知识图谱发挥更大的作用,就必须靠自然语言处理,挖掘文本内部的关系,从而把非结构化数据转换为半结构化或结构化数据;目前还在学习中。
创建电影相关的演员、导演、制片商、作家和相关关系,这些数据来自于neo4j的movie数据
ACTED_IN(角色扮演)关系,共172条,源数据为演员,目标数据为电影,属性包括 roles,属性值为数组
DIRECTED(导演)关系,共44条,源数据为导演,目标数据为电影
PRODUCED(制片)关系,共15条,源数据为制片商,目标数据为电影
WROTE(写作)关系,共10条,源数据为作家,目标数据为电影
FOLLOWS(估计为回复)关系,共3条,源数据为影评人,目标数据为影评人
REVIEWED(影评)关系,共9条,源数据为影评人,目标数据为电影,属性包括 summary和rating
CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix),
(RobR)-[:DIRECTED]->(AFewGoodMen),
(AaronS)-[:WROTE]->(AFewGoodMen)
CREATE
(JamesThompson)-[:FOLLOWS]->(JessicaThompson),
(PaulBlythe)-[:FOLLOWS]->(AngelaScope)
CREATE
(JessicaThompson)-[:REVIEWED {summary:'An amazing journey', rating:95}]->(CloudAtlas),
(JessicaThompson)-[:REVIEWED {summary:'You had me at Jerry', rating:92}]->(JerryMaguire)
1、查找所有相关电影、人物、关系
MATCH (n) RETURN n
2、查找名为“Tom Hanks”的人物
MATCH (person{name:"Tom Hanks"}) RETURN person
查找名为“Cloud Atlas”的电影
MATCH (movie{title:"Cloud Atlas"}) RETURN movie
3、随机查找10个人物的人名
MATCH (people:Person) RETURN people.name LIMIT 10
4、随机查找10个人物
MATCH (people:Person) RETURN people LIMIT 10
5、查找1990-2000年之间发行的电影名称
MATCH (nineties:Movie)
WHERE nineties.released>1990 AND nineties.released<2000
RETURN nineties.title,nineties.released
ORDER BY nineties.released
6、查找"Tom Hanks"参演过的电影名称
MATCH (people:Person{name:"Tom Hanks"})-[:ACTED_IN]->(actorMovies)
RETURN people,actorMovies
7、查找谁导演了电影“Cloud Atlas”
MATCH (move {title:"Cloud Atlas"}) <- [:DIRECTED] - (directors)
RETURN directors.name
8、查找与Tom Hanks一同出演过电影的人
MATCH (people:Person{name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(acActors)
RETURN acActors.name,m.title
查找与Tom Hanks一同出演过电影的人、电影
MATCH (people:Person{name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(acActors)
RETURN people,m,acActors
9、查找与电影"Cloud Atlas"相关的人
MATCH (people:Person) - [relatedTo] - (movie:Movie {title:"Cloud Atlas"})
RETURN people.name,Type(relatedTo),relatedTo,movie.title
10、查找与演员"Kevin Bacon"存在4条及以内关系的任何演员和电影
MATCH (people:Person{name:"Kevin Bacon"}) - [*1..4] - (hollywood)
RETURN DISTINCT hollywood
11、查找与演员"Kevin Bacon"与"Meg Ryan"之间的最短关系路径
MATCH p=shortestPath((people:Person{name:"Kevin Bacon"}) - [*] - (other:Person{name:"Meg Ryan"}))
RETURN p
12、查找没有与Tom Hanks合作过的演员
1、先找到Tom Hanks还没有合作过的演员,但Tom Hanks的合作伙伴曾经与其合作过
2、找到一个可以向他的潜在合作者介绍Tom Hanks的人
MATCH (people:Person{name:"Tom Hanks"}) - [:ACTED_IN]->(m)<-[:ACTED_IN]-(others),
(others)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(otherothers)
WHERE NOT (people)- [:ACTED_IN]->(m2)
RETURN otherothers.name AS Recommended,count(*) AS Strength
ORDER BY Strength DESC
13、找人将Tom Hanks介绍给Tom Cruise
MATCH
(tom:Person{name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(others),
(others)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cruise:Person{name:"Tom Cruise"})
RETURN tom,m,others,m2,cruise
14、匹配某个电影相关的演员和导演等多种关系
MATCH (movie:Movie{title:'The Matrix Reloaded'})<-[:ACTED_IN|DIRECTED]-(person)
RETURN person.name
15、单条最短路径shortestPath函数
MATCH (apeople:Person{name:'Tom Hanks'}),(bpeople:Person{name:'Tom Cruise'}),
p=shortestPath((apeople)-[*..4]-(bpeople))
RETURN p
16、所有最短路径allshortestPaths函数
MATCH (apeople:Person{name:'Tom Hanks'}),(bpeople:Person{name:'Tom Cruise'}),
p=allshortestPaths((apeople)-[*..4]-(bpeople))
RETURN p
应用场景:根据关系属性寻找最短路径
CREATE
(A:Node{name:"A"}),
(B:Node{name:"B"}),
(C:Node{name:"C"}),
(D:Node{name:"D"}),
(E:Node{name:"E"}),
(F:Node{name:"F"}),
(G:Node{name:"G"}),
(H:Node{name:"H"}),
(I:Node{name:"I"}),
(A)-[:path{dist:2}]->(B),
(A)-[:path{dist:3}]->(C),
(A)-[:path{dist:4}]->(D),
(B)-[:path{dist:3}]->(C),
(D)-[:path{dist:3}]->(C),
(C)-[:path{dist:3}]->(E),
(C)-[:path{dist:4}]->(F),
(C)-[:path{dist:11}]->(I),
(E)-[:path{dist:4}]->(F),
(E)-[:path{dist:4}]->(G),
(G)-[:path{dist:1}]->(H),
(H)-[:path{dist:2}]->(I)
找到A到I的最短路径,按照
1、单条最短路径shortestPath函数
MATCH (A:Node{name:'A'}),(I:Node{name:'I'}),
p=shortestPath((A)-[*..4]-(I))
RETURN p
2、所有最短路径allshortestPaths函数
MATCH (A:Node{name:'A'}),(I:Node{name:'I'}),
p=allshortestPaths((A)-[*..4]-(I))
RETURN p
3、根据距离计算最短路径
#根据dist值计算最短路径,得到最短路径的排序
MATCH (A:Node{name:'A'}),(I:Node{name:'I'}),p=((A)-[*1..8]->(I))
WITH REDUCE(tot=0, n IN rels(p)|tot + n.dist) AS ref, p
RETURN ref
ORDER BY ref ASC
#根据dist值计算最短路径,得到最短路径的图例,由于A-C-I有直接连通,所以这条线也出来了
MATCH (A:Node{name:'A'}),(I:Node{name:'I'}),p=((A)-[*1..8]->(I))
WITH REDUCE(tot=0, n IN rels(p)|tot + n.dist) AS ref, p
RETURN p,ref
ORDER BY ref ASC
LIMIT 1
本文分享自 python与大数据分析 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!