forked from yaph/programming-languages-influence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
langs.py
63 lines (54 loc) · 1.96 KB
/
langs.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
# -*- coding: utf-8 -*-
# Get programming languages data from freebase and save in json format
# appropriate for generating a network graph with sigma.js.
import requests, requests_cache, json
import networkx as nx
requests_cache.configure('freebase')
langs = []
paradigms = {}
plin = 'Programming Languages Influence Network'
graph_file = 'plin.gexf'
with open('query.json') as f:
query = f.read()
r = requests.get('https://www.googleapis.com/freebase/v1/mqlread', params={'query': query})
res = json.loads(r.text)['result']
for index, lang in enumerate(res):
paras = []
for i in lang['language_paradigms']:
pid = i['id']
name = i['name']
paras.append({'id': pid, 'name': name})
if pid not in paradigms:
paradigms[pid] = {'count': 1, 'name': name, 'id': pid}
else:
paradigms[pid]['count'] += 1
langs.append({
'index': index,
'size': len(lang['influenced']),
'influenced': [{'id': i['id'], 'name': i['name']} for i in lang['influenced']],
'influencedby': [{'id': i['id'], 'name': i['name']} for i in lang['influenced_by']],
'paradigms': paras,
'id': lang['id'],
'label': lang['name']
})
data = {
'paradigms': [i[1] for i in sorted(paradigms.items(), key=lambda x: x[1]['count'], reverse=True)],
'langs': langs
}
with open('data.json', 'w') as f:
json.dump(data, f)
# create graph and save to file
G = nx.DiGraph()
for index, l in enumerate(langs):
attr = {}
attr['label'] = l['label']
if l['paradigms']:
attr['paradigms'] = '|'.join([p['name'] for p in l['paradigms']])
if l['influenced']:
attr['influenced'] = '|'.join([p['name'] for p in l['influenced']])
if l['influencedby']:
attr['influencedby'] = '|'.join([p['name'] for p in l['influencedby']])
G.add_node(l['id'], attr)
for i in l['influenced']:
G.add_edge(l['id'], i['id'])
nx.write_gexf(G, graph_file, version='1.2draft')