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())