-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathknowledge_graph.py
More file actions
80 lines (74 loc) · 2.77 KB
/
knowledge_graph.py
File metadata and controls
80 lines (74 loc) · 2.77 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import json
prev = './knowledge_graph_data/'
info = {}
def get_concept_number(data):
return len(data.split('"name"')) - 1
def preload():
global info
with open(prev + 'basic_info.json', 'r', encoding='utf-8') as f:
tmp = f.read().split('\n')
for line in tmp:
if line == '':
continue
a = json.loads(line)
info[a['field']] = a
with open(prev + 'data.json', 'r', encoding='utf-8') as f:
tmp = f.read().split('\n')
for line in tmp:
if line == '':
continue
a = json.loads(line)
field = a['name']
info[field]['data'] = a
info[field]['concept-number'] = get_concept_number(line)
def load_info(field):
if field not in info:
return None
res = {}
res['title'] = field
pre = info[field]
if 'data' not in pre:
file = prev + 'data/' + field + '.json'
if not os.path.exists(file):
return None
with open(file, 'r', encoding='utf-8') as f:
raw = f.read()
data = json.loads(raw)
pre['data'] = data
if 'intro' not in pre:
file = prev + 'intro/'+ field + '.html'
if os.path.exists(file):
with open(file, 'r', encoding='utf-8') as f:
code = f.read()
else:
code = ''
text = []
text.append('当前为{}级领域'.format(len(pre['routes'])))
if len(pre['routes']) > 0:
fa = pre['routes'][0]
text.append('上位领域:<a href="/mooc_knowledge_graph/{}">{}</a>'.format(fa, fa))
if len(pre['subfields']) > 0:
text.append('直接子领域有{}个,二级子领域有{}个'.format(len(pre['subfields']), pre['grand_son_number']))
text.append('包含领域/概念总数{}个'.format(pre['all_son_number']))
text = '<hr /><p><b>当前领域的信息:</b> <br /> 本领域为 {} <br /> {}</p>'.format(field, '<br />'.join(text))
pre['intro'] = code + text
res['intro'] = pre['intro']
if len(pre['subfields']) > 0:
html = '<hr /><div id="buttons"><p><b>下位领域:</b></p>'
for button in pre['subfields']:
#labeled = False if button not in info else info[button]['is_labeled']
#color = 'btn-success' if labeled else 'btn-light'
color = 'btn-light'
html += '<a href="/mooc_knowledge_graph/{}"><button type="button" class="btn {} my-button">{}</button></a>'.format(button, color, button)
html += '</div>'
else:
html = ''
res['buttons_html'] = html
return res
def load_data(field):
global info
if field not in info or 'data' not in info[field]:
return None
return info[field]['data']
preload()