forked from rizwan09/LanModeledProgramGeneartion-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepocess.py
151 lines (111 loc) · 3.76 KB
/
prepocess.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
import os, sys
def genearte_unchanged_change():
f= open("files.txt", "r")
lines = f.readlines()
f.close()
file_counts = {}
for line in lines:
if line != "\r\n":
line = line.split('/')[-1].rstrip("\n")
if line in file_counts:
file_counts[line] +=1
else:
file_counts[line] = 1
with open('unchanged_files.txt', 'w') as f, open('changed_files.txt', 'w') as c_f:
c = 0
for line in lines:
if line != "\r\n":
line2 = line.split('/')[-1].rstrip("\n")
if file_counts[line2] == 1:
f.write(line)
c+=1
else:
c_f.write(line)
print ' unchanged: '+ str(c)+ ' of: ', len(file_counts)
def genearte_variable_version_eliminatating_method_tokens(file, output_file):
f = open(file, "r")
lines = f.readlines()
f.close()
new_lines = list()
for line in lines:
if line != "\r\n":
line = line.split(' ', 1)[1].rsplit(" ", 1)[0] #trim <method start> and <method end>
new_lines.append(line[:-1])
n_total = len(new_lines)
train = open(output_file, "wb")
for line in new_lines:
train.write(line+"\n")
train.close()
# genearte_variable_version_eliminatating_method_tokens('unchanged_train.txt', 'unchanged_train.data')
'''for now we only consider the diff of last two snapshots,
the changed lines of the last snapshots are considered to be test sentences. All other version of a method
except the last snapshot version are considered as training'''
def change_unchanged_v2(final_version_index = -1): #-1 for last
snapshots_history = {}
f= open("files.txt", "r")
lines = f.readlines()
f.close()
file_counts = {}
for line_full in lines:
if line_full != "\r\n":
line = line_full.split('/')[-1].rstrip("\n")
if line in file_counts:
file_counts[line] +=1
snapshots_history[line].append(line_full.rstrip('\n'))
else:
file_counts[line] = 1
snapshots_history[line] = []
snapshots_history[line].append(line_full.rstrip('\n'))
with open('unchanged_files.txt', 'w') as f, open('changed_files.txt', 'w') as c_f, open('all_train_file_list.txt', 'w') as t_f:
c = 0
for line in lines:
if line != "\r\n":
line2 = line.split('/')[-1].rstrip("\n")
if file_counts[line2] == 1:
f.write(line)
t_f.write(line)
c+=1
else:
c_f.write(line)
print ' unchanged: '+ str(c)+ ' of: ', len(file_counts)
old_chnaged_files = {}
with open('old_changed_files.txt', 'w') as ocf:
for ff, pp in snapshots_history.items():
if(len(pp)>1):
# print ff, ' paths: ', pp, "\n\n" # changed files have frequency > 1, taken care already
old_chnaged_files[ff] = pp[:final_version_index]
# print len(old_chnaged_files[ff]), len(pp), " freq: ", file_counts[ff], "\n"
for p in pp[:final_version_index]:
ocf.write(p+'\n')
t_f.write(p+'\n')
# cmd = 'git diff '+"../"+pp[final_version_index-1] + " "+ "../"+pp[final_version_index]
# print cmd
# os.system(cmd)
# exit()
def generate_train_data_from_all_but_last_snapshot():
change_unchanged_v2()
# generate_train_data_from_all_but_last_snapshot()
genearte_variable_version_eliminatating_method_tokens('all_train.txt', 'all_train.data')
'''
# f = open("all_method_body_new.txt", "r")
# lines = f.readlines()
# f.close()
# new_lines = list()
# for line in lines:
# if line != "\r\n":
# line = line.split(' ', 1)[1].rsplit(" ", 1)[0] #trim <method start> and <method end>
# new_lines.append(line[:-1])
# n_total = len(new_lines)
# train = open("train_ori.data", "wb")
# for line in new_lines[:int(0.8*n_total)]:
# train.write(line+"\n")
# train.close()
# val = open("val_ori.data", "wb")
# for line in new_lines[int(0.8*n_total):int(0.9*n_total)]:
# val.write(line+"\n")
# val.close()
# test = open("test_ori.data", 'wb')
# for line in new_lines[int(0.9*n_total):]:
# test.write(line+"\n")
# test.close()
'''