[知识图谱]利用py2neo从Neo4j数据库获取数据
1 # -*- coding: utf-8 -*- 2 3 from py2neo import Graph 4 import json 5 import re 6 7 8 class Neo4jToJson(object): 9 """知识图谱数据接口""" 10 11 def __init__(self): 12 """初始化数据""" 13 # 与neo4j服务器建立连接 14 self.graph = Graph("http://IP//:7474", username="neo4j", password="xxxxx") 15 self.links = [] 16 self.nodes = [] 17 18 def post(self): 19 """与前端交互""" 20 # 前端传过来的数据 21 select_name = '南京审计大学' 22 # 取出所有节点数据 23 nodes_data_all = self.graph.run("MATCH (n) RETURN n").data() 24 # node名存储 25 nodes_list = [] 26 for node in nodes_data_all: 27 nodes_list.append(node['n']['name']) 28 # 根据前端的数据,判断搜索的关键字是否在nodes_list中存在,如果存在返回相应数据,否则返回全部数据 29 if select_name in nodes_list: 30 # 获取知识图谱中相关节点数据 31 nodes_data = self.graph.run("MATCH (n)--(b) where n.name='" + select_name + "' return n,b").data() 32 links_data = self.graph.run("MATCH (n)-[r]-(b) where n.name='" + select_name + "' return r").data() 33 self.get_select_nodes(nodes_data) 34 else: 35 # 获取知识图谱中所有节点数据 36 links_data = self.graph.run("MATCH ()-[r]->() RETURN r").data() 37 nodes_data = self.graph.run("MATCH (n) RETURN n").data() 38 self.get_all_nodes(nodes_data) 39 40 self.get_links(links_data) 41 42 # 数据格式转换 43 neo4j_data = {'links': self.links, 'nodes': self.nodes} 44 neo4j_data_json = json.dumps(neo4j_data, ensure_ascii=False).replace(u'\xa0', u'') 45 return neo4j_data_json 46 47 def get_links(self, links_data): 48 """知识图谱关系数据获取""" 49 links_data_str = str(links_data) 50 links = [] 51 i = 1 52 dict = {} 53 # 正则匹配 54 links_str = re.sub("[\!\%\[\]\,\。\{\}\-\:\'\(\)\>]", " ", links_data_str).split(' ') 55 for link in links_str: 56 if len(link) > 1: 57 if i == 1: 58 dict['source'] = link 59 elif i == 2: 60 dict['name'] = link 61 elif i == 3: 62 dict['target'] = link 63 self.links.append(dict) 64 dict = {} 65 i = 0 66 i += 1 67 return self.links 68 69 def get_select_nodes(self, nodes_data): 70 """获取知识图谱中所选择的节点数据""" 71 dict_node = {} 72 for node in nodes_data: 73 name = node['n']['name'] 74 tag = node['n']['tag'] 75 dict_node['name'] = name 76 dict_node['tag'] = tag 77 self.nodes.append(dict_node) 78 dict_node = {} 79 break 80 for node in nodes_data: 81 name = node['b']['name'] 82 tag = node['b']['tag'] 83 dict_node['name'] = name 84 dict_node['tag'] = tag 85 self.nodes.append(dict_node) 86 dict_node = {} 87 88 def get_all_nodes(self, nodes_data): 89 """获取知识图谱中所有节点数据""" 90 dict_node = {} 91 for node in nodes_data: 92 name = node['n']['name'] 93 tag = node['n']['tag'] 94 dict_node['name'] = name 95 dict_node['tag'] = tag 96 self.nodes.append(dict_node) 97 dict_node = {} 98 return self.nodes 99 100 101 102 if __name__ == '__main__': 103 data_neo4j = Neo4jToJson() 104 print(data_neo4j.post())
数据驱动变革-云将skyell。用Flask+Nginx+uWsgi搭建的个人博客:http://www.skyell.cn/