当前位置:   article > 正文

知识图谱之py2neo

py2neo

py2neo介绍

  • 介绍

Py2neo是一个客户端库和工具包,用于从Python应用程序和命令行使用Neo4jNeo4j Graph Data Platform | Graph Database Management System)。该库同时支持 Bolt 和 HTTP,并提供高级 API、OGM、管理工具、交互式控制台、Pygments 的 Cypher 词法分析器以及许多其他功能。

  • 安装

pip install py2neo

注:Neo4j也需要安装,地址https://neo4j.com/

py2neo基本使用

  • 启动Neo4j

  • 连接代码

  1. from py2neo import Graph, Node, Relationship
  2. graph = Graph("http://localhost:7474//browser", name='neo4j', auth=("neo4j", "12345678"))
  3. print(graph.run("show databases"))

基于xsls文件的demo

  • demo效果

  • xls文件内容

  • sheet 结构

  • "关系"sheet内容如下

id

from

to

type

1

令狐冲

宁中则

师娘

2

仪琳

令狐冲

倾慕

3

任盈盈

令狐冲

爱慕

4

令狐冲

岳林珊

小师妹

5

岳林珊

岳不群

父亲

6

任盈盈

任我行

父亲

7

岳不群

宁中则

夫妻

#8

林平之

岳林珊

夫妻

  • 下面的表格分别对应"恒山派"、"华山派"、"日月教"sheet中的内容()

id

title

1

令狐冲

2

仪清

3

仪和

4

仪琳

id

title

1

岳不群

2

宁中则

3

岳林珊

4

林平之

id

title

1

任我行

2

任盈盈

3

向问天

  • demo依赖

  1. pip install pandas
  2. pip install openpyxl

Neo4j和py2neo也是需要的。

  • demo主要代码块

  1. import os
  2. import pandas as pd
  3. from py2neo import Graph, Node, Relationship
  4. sheetNameList = [
  5. {'sheet':'恒山派', 'type':'Node'},
  6. {'sheet':'华山派', 'type':'Node'},
  7. {'sheet':'日月教', 'type':'Node'},
  8. {'sheet':'关系', 'type':'Relationship'},
  9. ]
  1. def __init__(self, filename=None):
  2. self.filename = filename
  3. self.nodeList = []
  4. self.relationshipList = []
  5. self._loadfile()
  6. self._buildGraph()
  1. def _loadfile(self):
  2. if not os.path.exists(self.filename):
  3. print('_loadfile', 'file not exist')
  4. return False
  5. for sheetName in sheetNameList:
  6. sheetData = pd.read_excel(self.filename, index_col=None, comment='#', sheet_name=sheetName['sheet'])
  7. if sheetName['type'] == 'Node':
  8. keys=[]
  9. for index, row in sheetData.iterrows():
  10. if not row['title']:
  11. continue
  12. keys.append(row['title'])
  13. keyval = dict(zip(keys, keys))
  14. node = Node(sheetName['sheet'], name=sheetName['sheet'], **keyval)
  15. self.nodeList.append(node)
  16. for index, row in sheetData.iterrows():
  17. if not row['title']:
  18. continue
  19. subNode = Node(sheetName['sheet'], name=row['title'])
  20. self.nodeList.append(subNode)
  21. rel = Relationship(node, '属性', subNode)
  22. self.relationshipList.append(rel)
  23. elif sheetName['type'] == 'Relationship':
  24. startNode = None
  25. endNode = None
  26. for index, row in sheetData.iterrows():
  27. if not row['from'] or not row['to']:
  28. continue
  29. for node in self.nodeList:
  30. if node['name'] == row['from']:
  31. startNode = node
  32. elif node['name'] == row['to']:
  33. endNode = node
  34. else:
  35. pass
  36. rel = Relationship(startNode, row['type'], endNode)
  37. self.relationshipList.append(rel)
  38. else:
  39. pass
  1. def _buildGraph(self):
  2. graph = Graph("http://localhost:7474//browser", name='neo4j', auth=("neo4j", "12345678"))
  3. graph.delete_all()
  4. for node in self.nodeList:
  5. graph.create(node)
  6. for rel in self.relationshipList:
  7. graph.merge(rel)
  8. pass
  9. graph.begin()

参考

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/647930
推荐阅读
  

闽ICP备14008679号