-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTutils_v2.py
349 lines (294 loc) · 9.68 KB
/
ASTutils_v2.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import random
import json
import pymysql
def getAllLeaves(ast): # ast: list of nodes(dict)
# ast 按照index排序
# 给所有节点增加parent
for node in ast:
if "children" in node.keys():
for childid in node["children"]:
ast[childid]["parent"] = node["index"]
# 获得所有的叶节点
leaves = []
for node in ast:
if "children" not in node.keys():
leaves.append(node)
return ast, leaves
def getPathBetweenSrcTrg(src, trg, ast):
# src, trg: node
# ast: 有parent的ast
srcAncestor = []
trgAncestor = []
ancestor = src
while "parent" in ancestor.keys():
srcAncestor.append(ast[ancestor["index"]])
ancestor = ast[ancestor["parent"]]
srcAncestor.append(ast[ancestor["index"]])
ancestor = trg
while "parent" in ancestor.keys():
trgAncestor.append(ast[ancestor["index"]])
ancestor = ast[ancestor["parent"]]
trgAncestor.append(ast[ancestor["index"]])
srcAncestor.reverse()
trgAncestor.reverse()
i = 0
j = 0
while i < len(srcAncestor) and j < len(trgAncestor):
if srcAncestor[i]["index"] != trgAncestor[j]["index"]:
break
i += 1
j += 1
path = []
srcToken = src["value"] if src["value"] != None else "None"
trgToken = trg["value"] if trg["value"] != None else "None"
i -= 1
while i < len(srcAncestor):
path.append(srcAncestor[i]["type"])
i += 1
path.reverse()
while j < len(trgAncestor):
path.append(trgAncestor[j]["type"])
j += 1
path = " ".join(path)
return path, srcToken, trgToken
def updateVocab(vocab):
size = len(vocab)
keyWord = {
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "continue",
"default", "do", "double", "else", "enum", "exports", "extends", "final", "finally", "float", "for",
"if", "implements", "import", "instanceof", "int", "interface", "long", "long", "module", "native", "new",
"package", "private", "protected", "public", "requires", "return", "short", "static", "strictfp", "super",
"switch", "synchronized",
"this", "throw", "throws", "transient", "try", "void", "volatile", "while", "true", "null", "false", "var",
"const", "goto"
}
keyWord = list(keyWord)
stopwords = {
"i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yourself", "yourselves",
"he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself", "they", "them", "their",
"theirs", "themselves", "what", "which", "who", "whom", "this", "that", "these", "those", "am", "is",
"are", "was", "were", "be", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing",
"a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while", "of", "at", "by", "for", "with",
"about", "against", "between", "into", "through", "during", "before", "after", "above", "below", "to", "from",
"up", "down", "in", "out", "on", "off", "over", "under", "again", "further", "then", "once", "here", "there",
"when", "where", "why", "how", "all", "any", "both", "each", "few", "more", "most", "other", "some", "such",
"no", "nor", "not", "only", "own", "same", "so", "than", "too", "very", "s", "t", "can", "will", "just", "don",
"should", "now", "lbrace", "rbrace", "dot", "comma", "eq", "semi", "lparen", "rparen", "colon", "lbracket",
"rbracket",
"lt", "gt", "{", "}", "(", ")", "[", "]", ",", "."
}
stopwords = list(stopwords)
for i in range(len(keyWord)):
vocab.update([(keyWord[i], size + i)])
size = len(vocab)
for i in range(len(stopwords)):
vocab.update([(stopwords[i], size + i)])
return vocab
def parseInput(sent):
return [z for z in sent.split(' ')]
def toNum(data, vocab_to_int):
# 转为编号表示
res = []
for z in parseInput(data):
res.append(str(vocab_to_int.get(z, vocab_to_int["<UNK>"])))
return res
def astToNum(data, vocab_to_int):
# 转为编号表示
res = []
for z in parseInput(data):
res.append(vocab_to_int.get(z, vocab_to_int["<UNK>"]))
if len(res) < 7:
for i in range(7 - len(res)):
res.append(vocab_to_int["<PAD>"])
if len(res) > 7:
res = res[:7]
return res
def subtokenToNum(data, vocab_to_int):
res = []
for z in data:
res.append(vocab_to_int.get(z, vocab_to_int["<UNK>"]))
if len(res) < 3:
for i in range(3 - len(res)):
res.append(vocab_to_int["<PAD>"])
if len(res) > 3:
res = res[:3]
return res
def getVocabForAST(asts, vocab_size):
vocab = set()
counts = {}
vocab_to_int = {}
int_to_vocab = {}
for ast in asts:
for node in ast:
if "type" in node.keys():
counts[node["type"]] = counts.get(node["type"], 0) + 1
vocab.update([node["type"]])
# # naive方法中path包括value
# if "value" in node.keys():
# counts[node["value"]] = counts.get(counts[node["value"]], 0) + 1
# vocab.update(node["value"])
_sorted = sorted(vocab, reverse=True, key=lambda x: counts[x])
for i, word in enumerate(["<PAD>", "<UNK>", "<START>", "<STOP>"] + _sorted):
if vocab_size is not None and i > vocab_size:
break
vocab_to_int[word] = i
int_to_vocab[i] = word
return vocab_to_int, int_to_vocab
def dfsSimplify(ast, root, path, totalpath):
# 深度遍历 得到多条路径
if "children" in ast[root["index"]].keys():
path.append(root["type"])
for child in root["children"]:
dfsSimplify(ast, ast[child], path, totalpath)
path.pop()
else:
if root["value"] == None:
root["value"] = "None"
path.append(root["value"])
# 叶节点内容包含在path中
totalpath.append(' '.join(path))
path.pop()
return
def getNPathSimplify(ast):
# 随机得到n条路径
path = []
totalpath = []
dfsSimplify(ast, ast[0], path, totalpath)
nPath = []
n = len(totalpath)
for i in range(n):
for j in range(i + 1, n):
sent = ' '.join(reversed(totalpath[i].split(' ')[1:])) + ' ' + totalpath[j]
nPath.append(sent)
return nPath
def getPathSimplify(asts, pathNum, ast_vocab_to_int):
# 每次训练路径都是随机抽取的
astPathNum = [] # 所有ast的所有path的编号表示 三维数组
for ast in asts:
ast = json.loads(ast)
nPath = getNPathSimplify(ast) # 针对每个ast的所有路径
nPathNum = []
for path in nPath: # 每条path的编号表示
nPathNum.append(astToNum(path, ast_vocab_to_int))
astPathNum.append(nPathNum)
return astPathNum
def splitToken(token):
subtokens = []
start = 0
l = len(token)
end = start + 1
while start < l:
sb = ""
sb += token[start].lower()
end = start + 1
while end < l and token[end].islower():
sb += token[end]
end += 1
subtokens.append(sb)
if end < l:
start = end
else:
break
return subtokens
# conn = pymysql.Connect(
# host="10.131.252.198",
# port=3306,
# user="root",
# passwd="17210240114",
# db="repos",
# charset='utf8'
# )
# cursor = conn.cursor()
def astindex():
ast_vocab_to_int = json.load(open("./vocab_ast.json", 'r'))
tokens_vocab_to_int = json.load(open("./vocab_tokens.json", 'r'))
tokens_vocab_to_int = updateVocab(tokens_vocab_to_int)
sql = "select ast from reposfile"
cursor.execute(sql)
conn.commit()
data = cursor.fetchall()
print(len(data))
import tqdm
indexdata = json.load(open('index.json', 'r'))
print(len(indexdata))
ids = json.load(open('ids.json', 'r'))
j = 0
valid = []
validindex = json.load(open('valid.json', 'r'))
with open('test.txt', 'w')as f:
for index, row in tqdm.tqdm(enumerate(data)):
ast = json.loads(row[0])
astWithParent, leaves = getAllLeaves(ast)
path = set()
path_copy = []
ftoken = []
ltoken = []
rspath = []
rsl = []
rsf = []
for i in range(len(leaves) - 1):
for j in range(i + 1, len(leaves)):
pathSrcToTrg, srcToken, trgToken = getPathBetweenSrcTrg(leaves[i], leaves[j], ast)
if pathSrcToTrg not in path:
path.add(pathSrcToTrg)
path_copy.append(pathSrcToTrg)
ftoken.append(srcToken)
ltoken.append(trgToken)
if len(path) > 100:
break
if len(path) > 100:
break
for i in range(len(path)):
rspath.append(astToNum(path_copy[i], ast_vocab_to_int))
rsf.append(str(tokens_vocab_to_int.get(ftoken[i], 1)))
rsl.append(str(tokens_vocab_to_int.get(ltoken[i], 1)))
if index not in ids:
f.write(json.dumps([rspath, ' '.join(rsf), ' '.join(rsl), indexdata[index]])+'\n')
else:
valid.append(json.dumps([rspath, ' '.join(rsf), ' '.join(rsl), validindex[j]]) + '\n')
j += 1
f.close()
open('astvalid.json', 'w').writelines(valid)
def getindex(ast, tokens_vocab_to_int):
ast = json.loads(ast)
ast_vocab_to_int = json.load(open("./vocab_ast.json", 'r'))
tokens_vocab_to_int = updateVocab(tokens_vocab_to_int)
astWithParent, leaves = getAllLeaves(ast)
path = set()
path_copy = []
ftoken = []
ltoken = []
rspath = []
rsl = []
rsf = []
for i in range(len(leaves) - 1):
for j in range(i + 1, len(leaves)):
pathSrcToTrg, srcToken, trgToken = getPathBetweenSrcTrg(leaves[i], leaves[j], ast)
if pathSrcToTrg not in path:
path.add(pathSrcToTrg)
path_copy.append(pathSrcToTrg)
ftoken.append(srcToken)
ltoken.append(trgToken)
if len(path) > 100:
break
if len(path) > 100:
break
for i in range(len(path)):
rspath.append(astToNum(path_copy[i], ast_vocab_to_int))
rsf.append(subtokenToNum(splitToken(ftoken[i]), tokens_vocab_to_int))
rsl.append(subtokenToNum(splitToken(ftoken[i]), tokens_vocab_to_int))
return [rspath, rsf, rsl]
# npath = getNPathSimplify(ast) # 拿到所有路径
# npaths = []
# firstTokens = []
# lastTokens = []
# for path in npath:
# path = path.split(' ')
# npaths.append(astToNum(' '.join(path[1: -1]), ast_vocab_to_int))
# splitFirstToken = splitToken(path[0])
# firstTokens.append(subtokenToNum(splitFirstToken, tokens_vocab_to_int))
# splitLastToken = splitToken(path[-1])
# lastTokens.append(subtokenToNum(splitLastToken, tokens_vocab_to_int))
# cursor.execute(sql2, (json.dumps(npaths), json.dumps(firstTokens), json.dumps(lastTokens), row[0]))
#
# conn.commit()