-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabledata.py
More file actions
146 lines (123 loc) · 6.47 KB
/
Labledata.py
File metadata and controls
146 lines (123 loc) · 6.47 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
import os
import shutil
def _get_positive_src_file_list(lines, solution, smell_type,
code_split_out_folder_class, code_split_out_folder_method):
pos_src_file_list = list()
for line in lines:
tokens = line.split(",")
if smell_type == "Design":
file = os.path.join(os.path.join(os.path.join(os.path.join(
os.path.join(code_split_out_folder_class, solution), solution), str(_filter_str(tokens[1]))),
str(_filter_str(tokens[2])).replace('<', '').replace('>', '') + ".code"))
else:
file = os.path.join(os.path.join(os.path.join(
os.path.join(os.path.join(code_split_out_folder_method, solution), solution),
str(_filter_str(tokens[1]))),
str(_filter_str(tokens[2])).replace('<', '').replace('>', '')),
str(_filter_str(tokens[3])) + ".code")
if os.path.exists(file):
if file not in pos_src_file_list:
pos_src_file_list.append(file)
else:
print("Path doesn't exists: " + str(file))
return pos_src_file_list
def _put_files_in_right_bucket(pos_source_file_list, solution, positive_cases_folder, negative_cases_folder,
smell_type, code_split_out_folder_class, code_split_out_folder_method):
if not os.path.exists(positive_cases_folder):
os.makedirs(positive_cases_folder)
if not os.path.exists(negative_cases_folder):
os.makedirs(negative_cases_folder)
total_files_copied = 0
base_folder_path = os.path.join(code_split_out_folder_class, solution) if smell_type == "Design" \
else os.path.join(code_split_out_folder_method, solution)
pos_counter = 0
neg_couter = 0
for root, dirs, files in os.walk(base_folder_path):
for file in files:
src_file_path = os.path.join(root, file)
if smell_type == "Design":
namespace = root.replace(code_split_out_folder_class + os.path.sep, "").replace(os.path.sep, "_")
else:
namespace = root.replace(code_split_out_folder_method + os.path.sep, "").replace(os.path.sep, "_")
if _is_present(pos_source_file_list, src_file_path):
dest_file_path = os.path.join(positive_cases_folder, namespace + str(pos_counter) + file)
if not os.path.exists(dest_file_path):
try:
shutil.copyfile(src_file_path, dest_file_path)
except:
pass
total_files_copied += 1
pos_counter += 1
else:
print("File already exists: " + str(dest_file_path)) # This should not be the case
else:
dest_file_path = os.path.join(negative_cases_folder, namespace + str(neg_couter) + file)
if not os.path.exists(dest_file_path):
try:
shutil.copyfile(src_file_path, dest_file_path)
except:
pass
total_files_copied += 1
neg_couter += 1
else:
print("File already exists: " + str(dest_file_path)) # This should not be the case
return total_files_copied
def _is_present(an_list, item):
if item in an_list:
return True
item_upper = item.upper()
for obj in an_list:
if obj.upper() == item_upper:
return True
return False
def _filter_str(token):
line = bytes(token, 'utf-8').decode('utf-8', 'ignore')
return line
def _scan_solution(solution, positive_cases_folder, negative_cases_folder,
smell_name_str, smell_type, smells_results_folder,
code_split_out_folder_class, code_split_out_folder_method):
print("Processing solution: " + solution)
if smell_type == "Impl":
solution_folder = os.path.join(code_split_out_folder_method, solution)
else:
solution_folder = os.path.join(code_split_out_folder_class, solution)
if os.path.exists(solution_folder):
total_file_count = sum([len(files) for r, d, files in os.walk(solution_folder)])
if total_file_count == 0:
return
else:
return
pos_source_file_list = list()
for root, dirs, files in os.walk(os.path.join(smells_results_folder, solution)):
for file in files:
if smell_type == "Design":
if not file.endswith("DesignSmells.csv"):
continue
else:
if not file.endswith("ImplementationSmells.csv"):
continue
print("Processing file: " + os.path.join(root, file))
lines = []
with open(os.path.join(root, file), encoding="utf8", errors='ignore') as fp:
for line in fp:
if smell_name_str in line:
lines.append(line)
pos_source_files = _get_positive_src_file_list(lines, solution, smell_type,
code_split_out_folder_class, code_split_out_folder_method)
pos_source_file_list.extend(pos_source_files)
total_copied_files = _put_files_in_right_bucket(pos_source_file_list, solution, positive_cases_folder, negative_cases_folder,
smell_type, code_split_out_folder_class, code_split_out_folder_method)
def generate_data(smells_results_folder, code_split_out_folder_class, code_split_out_folder_method, learning_data_folder_path):
SMELL_NAME_LIST = ["LongStatement", "MagicNumber", "UnutilizedAbstraction"]
SMELL_NAME_STR_LIST = ["Long Statement", "Magic Number", "Unutilized Abstraction"]
SMELL_TYPE_LIST = ["Impl", "Impl", "Design"]
if not os.path.exists(learning_data_folder_path):
os.makedirs(learning_data_folder_path)
for smell in range(len(SMELL_NAME_LIST)):
positive_cases_folder = os.path.join(learning_data_folder_path, SMELL_NAME_LIST[smell], "Positive")
negative_cases_folder = os.path.join(learning_data_folder_path, SMELL_NAME_LIST[smell], "Negative")
for solution in os.listdir(smells_results_folder):
_scan_solution(solution, positive_cases_folder, negative_cases_folder,
SMELL_NAME_STR_LIST[smell], SMELL_TYPE_LIST[smell], smells_results_folder,
code_split_out_folder_class, code_split_out_folder_method)
print("Learning Data Done.")