File size: 941 Bytes
9569e58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
- 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))