-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathkgview.py
37 lines (29 loc) · 1.02 KB
/
kgview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
'''
* Name : kgview.py - 知识图谱api请求
* Author : Yener(Zheng Wenyu) <[email protected]>
* Version : 1.0
* Description : 从OwnThink知识图谱中获取数据,利用D3.js实现知识图谱的可视化。
数据获取https://api.ownthink.com/kg/knowledge?entity=刘德华
'''
import os
import sys
import requests
def kg_view(entity):
url = 'https://api.ownthink.com/kg/knowledge?entity=%s'%entity # 知识图谱API
sess = requests.get(url) # 请求
text = sess.text # 获取返回的数据
response = eval(text) # 转为字典类型
knowledge = response['data']
nodes = []
for avp in knowledge['avp']:
if avp[1] == knowledge['entity']:
continue
node = {'source': knowledge['entity'], 'target': avp[1], 'type': "resolved", 'rela':avp[0]}
nodes.append(node)
for node in nodes:
node = str(node)
node = node.replace("'type'", 'type').replace("'source'", 'source').replace("'target'", 'target')
print(node+',')
if __name__=='__main__':
kg_view('图灵')