Fraser-Greenlee commited on
Commit ·
9569e58
1
Parent(s): b710504
viz
Browse filesFiles changed (3)
- visualise/__init__.pyAdded
- visualise/flatten.pyAdded+37
- visualise/main.pyAdded+37
12345678910111213141516171819202122232425262728293031323334353637'''- parse the AST for every line of code- add this to a graph of all possibly trees with frequency counts- plot this tree'''import json
with open('visualise/trie.json', 'r') as f: trie = json.load(f)
def flatten_children(root): children = [] for name, vals in root.items(): if name[0] == ' ': continue rr = {'name': name, 'value': vals[' count']} rr['children'] = flatten_children(vals) if not rr['children']: del rr['children'] children.append(rr)
tot = sum(child['value'] for child in children) for child in children: child['value'] = child['value'] / tot
return children
trie = { "name": "Module", "children": flatten_children(trie)}
with open('visualise/trie.flat.json', 'w') as f: f.write(json.dumps(trie))12345678910111213141516171819202122232425262728293031323334353637'''- parse the AST for every line of code- add this to a graph of all possibly trees with frequency counts- plot this tree'''import astimport jsonfrom tqdm import tqdm
trie = {}
class Reader: def __init__(self, trie, code): self.trie = trie self.root = ast.parse(code)
def run(self): return self.add_code(self.trie, self.root)
def add_code(self, cursor, node): for child in ast.iter_child_nodes(node): name = type(child).__name__ cursor[name] = cursor.get(name, {' count': 0}) cursor[name][' count'] += 1 cursor[name] = self.add_code(cursor[name], child) return cursor
with open('data.jsonl', 'r', encoding="utf-8") as f: for id_, line in tqdm(enumerate(f), total=8968897): trie = Reader(trie, json.loads(line)['code']).run()
with open('visualise/trie.json', 'w') as f: f.write(json.dumps(trie))visualise/__init__.pyAdded
visualise/flatten.pyAdded+37
visualise/main.pyAdded+37