Fraser-Greenlee commited on
Commit
9569e58
·
1 Parent(s): b710504
Files changed (3) hide show
  1. visualise/__init__.py +0 -0
  2. visualise/flatten.py +37 -0
  3. visualise/main.py +37 -0
visualise/__init__.py ADDED
File without changes
visualise/flatten.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ - parse the AST for every line of code
3
+ - add this to a graph of all possibly trees with frequency counts
4
+ - plot this tree
5
+ '''
6
+ import json
7
+
8
+
9
+ with open('visualise/trie.json', 'r') as f:
10
+ trie = json.load(f)
11
+
12
+
13
+ def flatten_children(root):
14
+ children = []
15
+ for name, vals in root.items():
16
+ if name[0] == ' ':
17
+ continue
18
+ rr = {'name': name, 'value': vals[' count']}
19
+ rr['children'] = flatten_children(vals)
20
+ if not rr['children']:
21
+ del rr['children']
22
+ children.append(rr)
23
+
24
+ tot = sum(child['value'] for child in children)
25
+ for child in children:
26
+ child['value'] = child['value'] / tot
27
+
28
+ return children
29
+
30
+
31
+ trie = {
32
+ "name": "Module",
33
+ "children": flatten_children(trie)
34
+ }
35
+
36
+ with open('visualise/trie.flat.json', 'w') as f:
37
+ f.write(json.dumps(trie))
visualise/main.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ - parse the AST for every line of code
3
+ - add this to a graph of all possibly trees with frequency counts
4
+ - plot this tree
5
+ '''
6
+ import ast
7
+ import json
8
+ from tqdm import tqdm
9
+
10
+
11
+ trie = {}
12
+
13
+
14
+ class Reader:
15
+ def __init__(self, trie, code):
16
+ self.trie = trie
17
+ self.root = ast.parse(code)
18
+
19
+ def run(self):
20
+ return self.add_code(self.trie, self.root)
21
+
22
+ def add_code(self, cursor, node):
23
+ for child in ast.iter_child_nodes(node):
24
+ name = type(child).__name__
25
+ cursor[name] = cursor.get(name, {' count': 0})
26
+ cursor[name][' count'] += 1
27
+ cursor[name] = self.add_code(cursor[name], child)
28
+ return cursor
29
+
30
+
31
+ with open('data.jsonl', 'r', encoding="utf-8") as f:
32
+ for id_, line in tqdm(enumerate(f), total=8968897):
33
+ trie = Reader(trie, json.loads(line)['code']).run()
34
+
35
+
36
+ with open('visualise/trie.json', 'w') as f:
37
+ f.write(json.dumps(trie))