-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathngrams.py
304 lines (219 loc) · 8.32 KB
/
ngrams.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os
import pprint
from dataclasses import dataclass, field
from pathlib import Path
from typing import Union, List, Any, Tuple
from aura.utils import Analyzer
from aura import python_executor
from aura.analyzers import python_src_inspector
INSPECTOR_PATH = os.path.abspath(python_src_inspector.__file__)
@dataclass
class ASTNodeIdentifier:
label: Union[str, None] = None
node_type: Union[str, None] = ''
children: List[Any] = field(default_factory=list)
def __iadd__(self, other):
if other:
self.children.append(other)
return self
def to_tuple(self):
return (self.label, self.node_type, tuple(x.to_tuple() for x in self.children))
@Analyzer.ID("ast_ngrams")
def extract(pth: Path, mime: str, metadata, **kwargs):
if pth.suffix == '.py':
pass
elif mime != 'text/x-python':
return
spth = os.fspath(pth.absolute())
ast_tree = python_executor.run_with_interpreters(command=[INSPECTOR_PATH, spth], metadata=metadata)
pprint.pprint(ast_tree['ast_tree'])
identifier_tree = extract_identifier(ast_tree['ast_tree'])
tuple_identifiers = identifier_tree.to_tuple()
pprint.pprint(tuple_identifiers)
print('---')
for x in filter_pq_gram_duplicates(tuple_identifiers, 2, 3):
print(x)
yield from []
def pq_gram_index(node: tuple, p: int, q: int, stem=None):
"""
Implementation of the pq Gram Index
Iterates over node identifier tuples producing n-grams of size p+q
p is the stem size (left fill)
q is the base size (right fill)
for the algorithm explanation, refer to the research paper below:
https://files.ifi.uzh.ch/boehlen/Papers/ABG10.pdf
"""
if stem is None:
stem = ((None, '*'),) * p
base = ((None, '*'),) * q
stem = stem[1:] + ((node[0], node[1]),)
if len(node[2]) == 0: # Leaf node
yield stem + base
else:
for child in node[2]:
base = base[1:] + ((child[0], child[1]),)
yield stem + base
yield from pq_gram_index(node=child, p=p, q=q, stem=stem)
for i in range(1, q):
base = base[1:] + ((None, '*'),)
yield stem + base
def filter_pq_gram_duplicates(*args, **kwargs):
grams = []
# TODO: not very effective but would do it's job for now
for gram in pq_gram_index(*args, **kwargs):
if gram not in grams:
grams.append(gram)
return grams
def extract_identifier(node):
"""
Recursively traverse the RAW AST JSON tree and convert them into ASTNodeIdentifier tree
:param node:
:return:
"""
# We want to only process raw ast node which must be dict types and have a `_type` key
if isinstance(node, dict):
t = node.get('_type')
if t in AST_NODE_TYPES: # Process the AST node via a specific node converter
return AST_NODE_TYPES[t](node)
elif t is not None: # Unknown/unsupported node, output it as misc without further recursion
return ASTNodeIdentifier(label=t, node_type='misc')
#---[ Functions to convert raw ast dict nodes into ASTNodeIdentifier based on their type ]---
def node_function_def(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=node['name'], node_type='func_def')
body = ASTNodeIdentifier(label='body', node_type='func_def_body')
for b in node['body']:
body += extract_identifier(b)
stmt += body
return stmt
def node_call(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=None, node_type='call')
func = ASTNodeIdentifier(node_type='call_func')
func += extract_identifier(node['func'])
stmt += func
args = ASTNodeIdentifier(node_type='call_args')
for arg in node['args']:
args += extract_identifier(arg)
stmt += args
return stmt
def node_module(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=None, node_type='module')
body = ASTNodeIdentifier(label=None, node_type='module_body')
for b in node['body']:
body += extract_identifier(b)
stmt += body
return stmt
def node_expr(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=None, node_type='expr')
stmt += extract_identifier(node['value'])
return stmt
def node_class_def(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=node['name'], node_type='class_def')
body = ASTNodeIdentifier(node_type='class_def_body')
for b in node['body']:
body += extract_identifier(b)
stmt += body
return stmt
def node_attribute(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=node['attr'], node_type='attribute')
# TODO: ctx
value = ASTNodeIdentifier(node_type='attribute_value')
value += extract_identifier(node['value'])
stmt += value
return stmt
def node_name(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=node['id'], node_type='name')
# TODO: add ctx
return stmt
def node_str(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=node['s'], node_type='str')
return stmt
def node_assign(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(node_type='assign')
targets = ASTNodeIdentifier(node_type='assign_targets')
for t in node['targets']:
targets += extract_identifier(t)
stmt += targets
value = ASTNodeIdentifier(node_type='assign_value')
value += extract_identifier(node['value'])
stmt += value
return stmt
def node_import(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(node_type='import')
names = ASTNodeIdentifier(node_type='import_names')
for n in node['names']:
name = ASTNodeIdentifier(label=n['name'], node_type='import_name')
if n['asname']:
asname = ASTNodeIdentifier(label=n['asname'], node_type='import_name_asname')
name += asname
names += name
stmt += names
return stmt
def node_import_from(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(label=node['module'], node_type='import_from')
names = ASTNodeIdentifier(node_type='import_from_names')
for n in node['names']:
name = ASTNodeIdentifier(label=n['name'], node_type='import_from_name')
if n['asname']:
asname = ASTNodeIdentifier(label=n['asname'], node_type='import_from_name_asname')
name += asname
names += name
stmt += names
level = ASTNodeIdentifier(label=str(node['level']), node_type='import_from_level')
stmt += level
return stmt
def node_print(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(node_type='print')
dest = ASTNodeIdentifier(node_type='print_dest')
dest += extract_identifier(node['dest'])
stmt += dest
values = ASTNodeIdentifier(node_type='print_values')
for v in node['values']:
value = ASTNodeIdentifier(node_type='print_value')
value += extract_identifier(v)
values += value
stmt += values
return stmt
def node_if(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(node_type='if')
body = ASTNodeIdentifier(node_type='if_body')
for b in node['body']:
body += extract_identifier(b)
stmt += body
orelse = ASTNodeIdentifier(node_type='if_orelse')
for o in node['orelse']:
orelse += extract_identifier(o)
stmt += orelse
test = ASTNodeIdentifier(node_type='if_test')
test += extract_identifier(node['test'])
stmt += test
return stmt
def node_compare(node: dict) -> ASTNodeIdentifier:
stmt = ASTNodeIdentifier(node_type='compare')
left = ASTNodeIdentifier(node_type='compare_left')
left += extract_identifier(node['left'])
stmt += left
comparators = ASTNodeIdentifier(node_type='compare_comparators')
stmt += comparators
for c in node['comparators']:
comparators += extract_identifier(c)
ops = ASTNodeIdentifier(node_type='compare_ops')
stmt += ops
for op in node['ops']:
ops += extract_identifier(op)
return stmt
AST_NODE_TYPES = {
'FunctionDef': node_function_def,
'Call': node_call,
'Module': node_module,
'Expr': node_expr,
'ClassDef': node_class_def,
'Attribute': node_attribute,
'Name': node_name,
'Str': node_str,
'Assign': node_assign,
'Import': node_import,
'ImportFrom': node_import_from,
'Print': node_print,
'If': node_if,
'Compare': node_compare
}