使用py2neo进行知识图谱构建

from py2neo import Graph, Node, Relationship

# Graph()中第一个为local host链接,auth为认证,包含 username 和 password
gragh = Graph('http://localhost:7474', auth = ('neo4j', '123456'))

#删除数据库原来的节点及联系
gragh.delete_all()

#创建节点
test_node_1 = Node("Comunity",name="腾讯")
test_node_2 = Node("Comunity",name="网易")
test_node_3 = Node("Comunity",name="华为")
test_node_4 = Node("Comunity",name="微软")
test_node_5 = Node("Comunity",name="谷歌")
test_node_6 = Node("Comunity",name="苹果")

gragh.create(test_node_1)
gragh.create(test_node_2)
gragh.create(test_node_3)
gragh.create(test_node_4)
gragh.create(test_node_5)
gragh.create(test_node_6)

使用py2neo进行知识图谱构建_第1张图片

title_node_1 = Node("title",name="互联网公司")
gragh.create(title_node_1)

games_node_1 = Node("games",name="王者荣耀")
games_node_2 = Node("games",name="和平精英")
games_node_3 = Node("games",name="决战!平安原")
games_node_4 = Node("games",name="阴阳师")
gragh.create(games_node_1)
gragh.create(games_node_2)
gragh.create(games_node_3)
gragh.create(games_node_4)


lei_node_1 = Node("lei",name="中国企业")
lei_node_2 = Node("lei",name="国外企业")
gragh.create(lei_node_1)
gragh.create(lei_node_2)

使用py2neo进行知识图谱构建_第2张图片

#建立关系
title_1_call_lei_1 = Relationship(title_node_1,"拥有企业",lei_node_1)
title_1_call_lei_2 = Relationship(title_node_1,"拥有企业",lei_node_2)

gragh.create(title_1_call_lei_1)
gragh.create(title_1_call_lei_2)

lei_1_call_test_1 = Relationship(lei_node_1,"隶属企业",test_node_1)
lei_1_call_test_2 = Relationship(lei_node_1,"隶属企业",test_node_2)
lei_1_call_test_3 = Relationship(lei_node_1,"隶属企业",test_node_3)

lei_2_call_test_4 = Relationship(lei_node_2,"隶属企业",test_node_4)
lei_2_call_test_5 = Relationship(lei_node_2,"隶属企业",test_node_5)
lei_2_call_test_6 = Relationship(lei_node_2,"隶属企业",test_node_6)

gragh.create(lei_1_call_test_1)
gragh.create(lei_1_call_test_2)
gragh.create(lei_1_call_test_3)
gragh.create(lei_2_call_test_4)
gragh.create(lei_2_call_test_5)
gragh.create(lei_2_call_test_6)

test_1_call_games_1 = Relationship(test_node_1,"拥有游戏",games_node_1)
test_1_call_games_2 = Relationship(test_node_1,"拥有游戏",games_node_2)
test_2_call_games_3 = Relationship(test_node_2,"拥有游戏",games_node_3)
test_2_call_games_4 = Relationship(test_node_2,"拥有游戏",games_node_4)
gragh.create(test_1_call_games_1)
gragh.create(test_1_call_games_2)
gragh.create(test_2_call_games_3)
gragh.create(test_2_call_games_4)

使用py2neo进行知识图谱构建_第3张图片

#创建二者间存在的竞争关系
games_1_call_games_3 = Relationship(games_node_1,"竞争关系",games_node_3)
games_3_call_games_1 = Relationship(games_node_3,"竞争关系",games_node_1)

gragh.create(games_1_call_games_3)
gragh.create(games_3_call_games_1)

竞争关系

#创建操作系统节点
System_node_1 = Node("System",name="操作系统")
gragh.create(System_node_1)

#拥有操作系统
#华为,微软,谷歌,苹果
test_3_call_System_1 = Relationship(test_node_3,"拥有操作系统",System_node_1)
gragh.create(test_3_call_System_1)

test_4_call_System_1 = Relationship(test_node_4,"拥有操作系统",System_node_1)
gragh.create(test_4_call_System_1)

test_5_call_System_1 = Relationship(test_node_5,"拥有操作系统",System_node_1)
gragh.create(test_5_call_System_1)

test_6_call_System_1 = Relationship(test_node_6,"拥有操作系统",System_node_1)
gragh.create(test_6_call_System_1)

使用py2neo进行知识图谱构建_第4张图片

#删除王者荣耀
gragh.delete(games_node_1 )

使用py2neo进行知识图谱构建_第5张图片

你可能感兴趣的:(自然语言处理,知识图谱,neo4j)