| ''' | |
| - parse the AST for every line of code | |
| - add this to a graph of all possibly trees with frequency counts | |
| - plot this tree | |
| ''' | |
| import ast | |
| import json | |
| from 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)) | |