forked from cgmartin/iTunes-Library-Network-Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikilog_gen_json.py
executable file
·79 lines (67 loc) · 2.27 KB
/
wikilog_gen_json.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
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
"""
wikilog_gen_json.py by BillSeitz Apr'2013
to generate JSON file usable with http://cgmartin.com/itunes-library-network-graph/
but from WikiLog WikiGraph data stored in WikiGraphSampleRaw.txt
"""
import os
nodes = []
node_ids = {}
next_id = 0
links = []
def input_read(input_file = 'WikiGraphSampleRaw.txt'): # read file of raw WikiGraph mentions
path = os.getcwd()
input_full = os.path.join(path, input_file)
input_lines = open(input_full).readlines()
return input_lines
def output_write(output_file = 'input_graph.json'): # write JSON out to file
path = os.getcwd()
output_full = os.path.join(path, 'js', output_file)
out_f = open(output_full, 'w')
output = {}
output['nodes'] = nodes
output['links'] = links
out_f.write(str(output))
def node_append(node_name): # add a pagename to nodes if not already there
global next_id
if node_name not in node_ids:
node_ids[node_name] = next_id
node = {}
node['name'] = node_name
if use_int_id:
node['id'] = next_id
else:
node['id'] = node_name
if node_name.startswith('z'):
node['type'] = 'b'
elif node_name in ('FractallyGenerativePatternLanguage', 'EconomicTransition', 'MakingALiving', 'MeaningfulLife'):
node['type'] = 'k'
else:
node['type'] = 'w'
next_id = next_id + 1
nodes.append(node)
def links_append(link_fields): # add a link to the links structure
link = {}
if use_int_id:
link['source'] = node_ids[link_fields[0]]
link['target'] = node_ids[link_fields[1]]
else:
link['source'] = link_fields[0]
link['target'] = link_fields[1]
links.append(link)
def json_create(use_int_id = 1):
input_lines = input_read()
for line in input_lines:
line = line.rstrip('\n')
print line
fields = line.split('|')
node_append(fields[0])
node_append(fields[1])
links_append(fields)
print ('num nodes: %d' % (len(nodes)))
print ('num links: %d' % (len(links)))
print ("Remember to change single- to double-quotes in JSON file before using!")
output_write()
if __name__ == '__main__':
global use_int_id
use_int_id = 0
json_create(use_int_id)