-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_std.py
More file actions
334 lines (314 loc) · 9.43 KB
/
feature_std.py
File metadata and controls
334 lines (314 loc) · 9.43 KB
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
from threading import Timer
import time,os
import sys
import json
from collections import Counter
import pymongo
import unicodedata
class creat_node:
def __init__(self, pair, token_pair, pos, value, parent):
self.pair=pair
self.token_pair=token_pair
self.pos=pos
self.value=value
self.parent=parent
self.child=[]
def build_tree(cur_parent,cur_parent_class):
#print(cur_parent,"\n",cur_parent_class,"\n", eval(cur_parent))
global node_num,n1,terminal_node
i=cur_parent_class.pos+1
while i<list_len:
if node_list[i]=="(":
if node_list[i+1]=="(":
cur_parent_class.token_pair=1
i+=1
continue
elif node_list[i+1][0].isalnum() == False:
i+=2
cur_parent_class.token_pair=1
continue
else:
cur_parent_class.pair=1
i+=1
node_num+=1 #num of nodes that has been processed so far
exec("global "+"n"+str(node_num)+";"+"n"+str(node_num)+"=creat_node(0,0,i,node_list[i],cur_parent)")
#print("n"+str(node_num)+"=creat_node(0,0,i,node_list[i],cur_parent)")
cur_parent_temp="n"+str(node_num)
cur_parent_class.child.append(cur_parent_temp)
i=build_tree(cur_parent_temp,eval(cur_parent_temp))
elif node_list[i]==")":
if cur_parent_class.token_pair ==1:
cur_parent_class.token_pair=0
else:
return i
elif node_list[i][0].isalnum() == False:
i+=1
continue
else:
node_num+=1
exec("global "+"n"+str(node_num)+";"+"n"+str(node_num)+"=creat_node(0,0,i,node_list[i],cur_parent)")
cur_parent_class.child.append("n"+str(node_num))
terminal_node.append("n"+str(node_num))
i+=1
return i
def check_exist(path, list):
for p in list:
if p["top"]==path["top"] and ((p["end1"]==path["end1"] and p["end2"]==path["end2"]) or (p["end1"]==path["end2"] and p["end2"]==path["end1"])):
return p
return False
def check_literal(node):
if ("iteral" in node) or (node == "number") or (node == "string"):
return True
else:
return False
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def tokenize(token):
new_token=""
for i in range(len(token)):
if token[i].isupper():
new_token=new_token+" "+token[i]
elif token[i] in ["_", "/", ",","?","!","<",">","[","]","{","}","@","#","$","|","-","^","*","&"]:
new_token=new_token+" "
elif token[i]==".":
if (i!=0 and i!=len(token)-1 and is_number(token[i-1]) and is_number(token[i+1])) == False:
new_token=new_token+" "
else:
new_token=new_token+token[i]
return new_token.split()
def path_abstract(topv,end1v,end2v):
abs_path={"top":[topv.value],"end1":[end1v.value],"end2":[end2v.value]}
return abs_path
def is_exsit(path):
if len(path["top"])!=0 and len(path["end1"])!=0 and len(path["end2"])!=0:
return True
else:
return False
def token_sta(token_list):
token=[]
token_num=[]
for t in token_list:
if t in token:
token_num[token.index(t)]+=1
else:
token.append(t)
token_num.append(1)
return token,token_num
def add_record(node_list,t_lang,abs_path_list,pathnum_list,pathtoken_list,pathtype,pathtypefile):
record={}
path_list=[]
for p in abs_path_list:
p1={"top":[],"end1":[],"end2":[]}
for i in p["top"]:
if (i in node_list) and (t_lang in node_list[i]):
p1["top"]=list(set(p1["top"]+node_list[i][t_lang]))
for i in p["end1"]:
if (i in node_list) and (t_lang in node_list[i]):
p1["end1"]=list(set(p1["end1"]+node_list[i][t_lang]))
for i in p["end2"]:
if (i in node_list) and (t_lang in node_list[i]):
p1["end2"]=list(set(p1["end2"]+node_list[i][t_lang]))
path_list.append(p1)
ll=len(abs_path_list)
for i in range(ll):
cur_path=path_list[i]
if is_exsit(cur_path):
path={"top":cur_path["top"][0],"end1":cur_path["end1"][0],"end2":cur_path["end2"][0]}
path1={"top":cur_path["top"][0],"end1":cur_path["end2"][0],"end2":cur_path["end1"][0]}
if path in pathtype["path"]:
path_name=pathtype["name"][pathtype["path"].index(path)]
elif path1 in pathtype["path"]:
path_name=pathtype["name"][pathtype["path"].index(path1)]
else:
path_name="p"+str(pathtype["amount"])
pathtype["path"].append(path)
pathtype["name"].append(path_name)
pathtype["amount"]+=1
#print(pathtype)
f=open(pathtypefile,"w")
json.dump(pathtype,f)
f.close()
token,token_num=token_sta(pathtoken_list[i])
record[path_name]=[pathnum_list[i],token,token_num]
print("Path abstracted...")
return record
def initTime():
global TIME
timer1=Timer(TIME, closeProcess)
timer1.start()
return timer1
def closeProcess():
print("close ")
os._exit(0)
def proc_string(s):
i=0
while i<len(s):
if s[i]=='"':
a=i
if s[i+1]=='"' and s[i+2]=='"':
for j in range(i+3, len(s)):
if s[j]=='"' and s[j+1]=='"' and s[j+2]=='"':
b=j+3
s=s[:a]+" thisisnote "+s[b:]
i=a+11
break
else:
st=""
for j in range(i+1, len(s)):
st+=s[j]
if s[j]=='"':
st=st[0:-1].replace("'","_").replace("(","_").replace(")","_").replace(" ","_")
st=" "+st+" "
b=j+1
s=s[:a]+st+s[b:]
i=a+len(st)-1
break
elif s[i]=="'":
a=i
if s[i+1]=="'" and s[i+2]=="'":
for j in range(i+3, len(s)):
if s[j]=="'" and s[j+1]=="'" and s[j+2]=="'":
b=j+3
s=s[:a]+" thisisnote "+s[b:]
i=a+11
break
else:
st=""
for j in range(i+1, len(s)):
st+=s[j]
if s[j]=="'":
st=st[0:-1].replace("'","_").replace("(","_").replace(")","_").replace(" ","_")
st=" "+st+" "
b=j+1
s=s[:a]+st+s[b:]
i=a+len(st)-1
break
i+=1
return s
if __name__ == '__main__':
TIME = 180
timer1=initTime()
#print(sys.argv[1])
if sys.argv[2]=="JavaScript":
fn="./testjs.txt"
if sys.argv[2]=="Java8":
fn="./testj8.txt"
if sys.argv[2]=="Python3":
fn="./testp3.txt"
if sys.argv[2]=="CPP14":
fn="./testc14.txt"
f=open(fn,'r')
lisp_tree = f.read()
f.close()
lisp_tree = lisp_tree.replace("("," ( ").replace(")"," ) ")
lisp_tree=proc_string(lisp_tree)
node_list = lisp_tree.split()[1:-1]
list_len = len(node_list)
node_num=1
ROOT=creat_node(0,0,-1,"ROOT","")
try:
n1=creat_node(0,0,0,node_list[0],"ROOT")
except IndexError:
timer1.cancel()
terminal_node=[]
build_tree("n1",n1)
print("Tree builded...")
for x in terminal_node:
tmp=eval(eval(x).parent)
if len(tmp.child)>1 or eval(x).value=="thisisnote":
terminal_node.remove(x)
abs_path_list=[]
pathnum_list=[]
pathtoken_list=[]
print(len(terminal_node))
if len(terminal_node)>800:
os._exit(0)
for p in range(len(terminal_node)-1):
for q in range(p+1,len(terminal_node)):
x=eval(terminal_node[p]).parent
y=eval(terminal_node[q]).parent
px=[x]
py=[y]
temp_list=[]
while True:
if x!="ROOT":
x=eval(x).parent
px.append(x)
if x!="ROOT" and (x in temp_list):
cur_abs_path=path_abstract(eval(x),eval(px[0]),eval(py[0]))
cp=check_exist(cur_abs_path,abs_path_list)
if cp:
tmp=abs_path_list.index(cp)
pathnum_list[tmp]+=1
pathtoken_list[tmp].extend(tokenize(eval(eval(px[0]).child[0]).value))
pathtoken_list[tmp].extend(tokenize(eval(eval(py[0]).child[0]).value))
else:
#print(cur_abs_path)
abs_path_list.append(cur_abs_path)
pathnum_list.append(1)
tmp_token=[]
tmp_token.extend(tokenize(eval(eval(px[0]).child[0]).value))
tmp_token.extend(tokenize(eval(eval(py[0]).child[0]).value))
pathtoken_list.append(tmp_token)
break
else:
temp_list.append(x)
if y!="ROOT":
y=eval(y).parent
py.append(y)
if y!="ROOT" and (y in temp_list):
cur_abs_path=path_abstract(eval(y),eval(px[0]),eval(py[0]))
cp=check_exist(cur_abs_path,abs_path_list)
if cp:
tmp=abs_path_list.index(cp)
pathnum_list[tmp]+=1
pathtoken_list[tmp].extend(tokenize(eval(eval(px[0]).child[0]).value))
pathtoken_list[tmp].extend(tokenize(eval(eval(py[0]).child[0]).value))
else:
#print(cur_abs_path)
abs_path_list.append(cur_abs_path)
pathnum_list.append(1)
tmp_token=[]
tmp_token.extend(tokenize(eval(eval(px[0]).child[0]).value))
tmp_token.extend(tokenize(eval(eval(py[0]).child[0]).value))
pathtoken_list.append(tmp_token)
break
else:
temp_list.append(y)
print("Path extracted...")
s_lang=sys.argv[2]
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
db=myclient["codetrans"]
lang_collection=["Python3","Java8","CPP14","JavaScript"]
for sl in lang_collection:
if s_lang==sl:
tbl=db[s_lang]
f=open("./node/"+s_lang+".json","r")
node_match_list=json.load(f)
lang_collection_tmp=lang_collection.remove("s_lang")
for t_lang in lang_collection_tmp:
tb1_tl=tb1[t_lang]
f1=open("./pathtype/"+t_lang+"_"+s_lang+".json","r")
pathtype=json.load(f1)
f1.close()
pathtypefile="./pathtype/"+t_lang+"_"+s_lang+".json"
fp=add_record(node_match_list,t_lang,abs_path_list,pathnum_list,pathtoken_list,pathtype,pathtypefile)
prog={"file":sys.argv[1],"feature":fp}
for p in pathtype["name"]:
if p in fp:
prog[p]=fp[p][0]
else:
prog[p]=0
tb1_tl.insert_one(prog)
print("finished")
timer1.cancel()