py2neo介绍
介绍
Py2neo是一个客户端库和工具包,用于从Python应用程序和命令行使用Neo4j(Neo4j Graph Data Platform | Graph Database Management System)。该库同时支持 Bolt 和 HTTP,并提供高级 API、OGM、管理工具、交互式控制台、Pygments 的 Cypher 词法分析器以及许多其他功能。
安装
pip install py2neo
注:Neo4j也需要安装,地址https://neo4j.com/
py2neo基本使用
启动Neo4j
连接代码
from py2neo import Graph, Node, Relationship
graph = Graph("http://localhost:7474//browser", name='neo4j', auth=("neo4j", "12345678"))
print(graph.run("show databases"))
基于xsls文件的demo
demo效果
![py2neo,知识图谱,python,知识图谱,Powered by 金山文档](https://imgs.yssmx.com/Uploads/2023/08/668897-1.png)
xls文件内容
sheet 结构
![py2neo,知识图谱,python,知识图谱,Powered by 金山文档](https://imgs.yssmx.com/Uploads/2023/08/668897-2.png)
"关系"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依赖
pip install pandas
pip install openpyxl
Neo4j和py2neo也是需要的。
demo主要代码块
import os
import pandas as pd
from py2neo import Graph, Node, Relationship
sheetNameList = [
{'sheet':'恒山派', 'type':'Node'},
{'sheet':'华山派', 'type':'Node'},
{'sheet':'日月教', 'type':'Node'},
{'sheet':'关系', 'type':'Relationship'},
]
def __init__(self, filename=None):
self.filename = filename
self.nodeList = []
self.relationshipList = []
self._loadfile()
self._buildGraph()
def _loadfile(self):
if not os.path.exists(self.filename):
print('_loadfile', 'file not exist')
return False
for sheetName in sheetNameList:
sheetData = pd.read_excel(self.filename, index_col=None, comment='#', sheet_name=sheetName['sheet'])
if sheetName['type'] == 'Node':
keys=[]
for index, row in sheetData.iterrows():
if not row['title']:
continue
keys.append(row['title'])
keyval = dict(zip(keys, keys))
node = Node(sheetName['sheet'], name=sheetName['sheet'], **keyval)
self.nodeList.append(node)
for index, row in sheetData.iterrows():
if not row['title']:
continue
subNode = Node(sheetName['sheet'], name=row['title'])
self.nodeList.append(subNode)
rel = Relationship(node, '属性', subNode)
self.relationshipList.append(rel)
elif sheetName['type'] == 'Relationship':
startNode = None
endNode = None
for index, row in sheetData.iterrows():
if not row['from'] or not row['to']:
continue
for node in self.nodeList:
if node['name'] == row['from']:
startNode = node
elif node['name'] == row['to']:
endNode = node
else:
pass
rel = Relationship(startNode, row['type'], endNode)
self.relationshipList.append(rel)
else:
pass
def _buildGraph(self):
graph = Graph("http://localhost:7474//browser", name='neo4j', auth=("neo4j", "12345678"))
graph.delete_all()
for node in self.nodeList:
graph.create(node)
for rel in self.relationshipList:
graph.merge(rel)
pass
graph.begin()
参考
The Py2neo Handbook — py2neo 2021.1
Python创建知识图谱_python知识图谱 md_趋吉避凶的博客-CSDN博客
Python neo4j建立知识图谱,药品知识图谱,neo4j知识图谱,知识图谱的建立过程,智能用药知识图谱,智能问诊必备知识图谱_医学小达人的博客-CSDN博客
Neo4j简介及Py2Neo的用法_neo4j py2neo_yayun_kg的博客-CSDN博客文章来源:https://www.toymoban.com/news/detail-668897.html
pandas处理Excel基本方法_<编程路上>的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-668897.html
到了这里,关于知识图谱之py2neo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!