-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessing.py
434 lines (320 loc) · 18.5 KB
/
preprocessing.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import string
import pickle
import re
import inflection
import nltk
from nltk.stem.porter import PorterStemmer
from assets import stop_words, python_keywords
from parsers import Parser
from datasets import DATASET
class ReportPreprocessing:
"""Class to preprocess bug reports"""
__slots__ = ['bug_reports']
def __init__(self, bug_reports):
self.bug_reports = bug_reports
def extract_stack_traces(self):
"""Extracting stack traces from bug reports"""
# Simple pattern to retrieve stack traces
pattern = re.compile(r' at (.*?)\((.*?)\)')
# Signs of a true stack trace to check in the retrieved regex grouping
signs = ['.py', 'Unknown Source', 'Native Method']
for report in self.bug_reports.values():
st_candid = re.findall(pattern, report.description)
# Filter actual stack traces from retrieved candidates
st = [x for x in st_candid if any(s in x[1] for s in signs)]
report.stack_traces = st
def pos_tagging(self):
"""Extracing specific pos tags from bug reports' summary and description"""
for report in self.bug_reports.values():
# Tokenizing using word_tokeize for more accurate pos-tagging
summ_tok = nltk.word_tokenize(report.summary)
desc_tok = nltk.word_tokenize(report.description)
sum_pos = nltk.pos_tag(summ_tok)
desc_pos = nltk.pos_tag(desc_tok)
report.pos_tagged_summary = [token for token, pos in sum_pos
if 'NN' in pos or 'VB' in pos]
report.pos_tagged_description = [token for token, pos in desc_pos
if 'NN' in pos or 'VB' in pos]
def tokenize(self):
"""Tokenizing bug reports into tokens"""
for report in self.bug_reports.values():
report.summary = nltk.wordpunct_tokenize(report.summary)
report.description = nltk.wordpunct_tokenize(report.description)
def _split_camelcase(self, tokens):
# Copy tokens
returning_tokens = tokens[:]
for token in tokens:
split_tokens = re.split(fr'[{string.punctuation}]+', token)
# If token is split into some other tokens
if len(split_tokens) > 1:
returning_tokens.remove(token)
# Camel case detection for new tokens
for st in split_tokens:
camel_split = inflection.underscore(st).split('_')
if len(camel_split) > 1:
returning_tokens.append(st)
returning_tokens += camel_split
else:
returning_tokens.append(st)
else:
camel_split = inflection.underscore(token).split('_')
if len(camel_split) > 1:
returning_tokens += camel_split
return returning_tokens
def split_camelcase(self):
"""Split CamelCase identifiers"""
for report in self.bug_reports.values():
report.summary = self._split_camelcase(report.summary)
report.description = self._split_camelcase(report.description)
report.pos_tagged_summary = self._split_camelcase(report.pos_tagged_summary)
report.pos_tagged_description = self._split_camelcase(report.pos_tagged_description)
def normalize(self):
"""Removing punctuation, numbers and also lowercase conversion"""
# Building a translate table for punctuation and number removal
punctnum_table = str.maketrans({c : None for c in string.punctuation + string.digits})
for report in self.bug_reports.values():
summary_punctnum_rem = [token.translate(punctnum_table)
for token in report.summary]
desc_punctnum_rem = [token.translate(punctnum_table)
for token in report.description]
pos_sum_punctnum_rem = [token.translate(punctnum_table)
for token in report.pos_tagged_summary]
pos_desc_punctnum_rem = [token.translate(punctnum_table)
for token in report.pos_tagged_description]
report.summary = [token.lower() for token
in summary_punctnum_rem if token]
report.description = [token.lower() for token
in desc_punctnum_rem if token]
report.pos_tagged_summary = [token.lower() for token
in pos_sum_punctnum_rem if token]
report.pos_tagged_description = [token.lower() for token
in pos_desc_punctnum_rem if token]
def remove_stopwords(self):
"""Removing stop words from tokens"""
for report in self.bug_reports.values():
report.summary = [token for token in report.summary
if token not in stop_words]
report.description = [token for token in report.description
if token not in stop_words]
report.pos_tagged_summary = [token for token in report.pos_tagged_summary
if token not in stop_words]
report.pos_tagged_description = [token for token in report.pos_tagged_description
if token not in stop_words]
def remove_python_keywords(self):
"""Removing Python language keywords from tokens"""
for report in self.bug_reports.values():
report.summary = [token for token in report.summary
if token not in python_keywords]
report.description = [token for token in report.description
if token not in python_keywords]
report.pos_tagged_summary = [token for token in report.pos_tagged_summary
if token not in python_keywords]
report.pos_tagged_description = [token for token in report.pos_tagged_description
if token not in python_keywords]
def stem(self):
"""Stemming tokens"""
# Stemmer instance
stemmer = PorterStemmer()
for report in self.bug_reports.values():
report.summary = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in report.summary],
report.summary]))
report.description = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in report.description],
report.description]))
report.pos_tagged_summary = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token
in report.pos_tagged_summary],
report.pos_tagged_summary]))
report.pos_tagged_description = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token
in report.pos_tagged_description],
report.pos_tagged_description]))
def preprocess(self):
"""Run the preprocessing"""
self.extract_stack_traces()
self.pos_tagging()
self.tokenize()
self.split_camelcase()
self.normalize()
self.remove_stopwords()
self.remove_python_keywords()
self.stem()
class SrcPreprocessing:
"""Class to preprocess source codes"""
__slots__ = ['src_files']
def __init__(self, src_files):
self.src_files = src_files
def pos_tagging(self):
"""Extracing specific pos tags from comments"""
for src in self.src_files.values():
# Tokenizing using word_tokeize for more accurate pos-tagging
comments_tok = nltk.word_tokenize(src.comments)
comments_pos = nltk.pos_tag(comments_tok)
src.pos_tagged_comments = [token for token, pos in comments_pos
if 'NN' in pos or 'VB' in pos]
def tokenize(self):
"""Tokenizing source codes into tokens"""
# print('Tokenizing source codes into tokens')
for src in self.src_files.values():
src.all_content = nltk.wordpunct_tokenize(src.all_content)
src.comments = nltk.wordpunct_tokenize(src.comments)
def _split_camelcase(self, tokens):
# Copy tokens
returning_tokens = tokens[:]
for token in tokens:
split_tokens = re.split(fr'[{string.punctuation}]+', token)
# If token is split into some other tokens
if len(split_tokens) > 1:
returning_tokens.remove(token)
# Camel case detection for new tokens
for st in split_tokens:
camel_split = inflection.underscore(st).split('_')
if len(camel_split) > 1:
returning_tokens.append(st)
returning_tokens += camel_split
else:
returning_tokens.append(st)
else:
camel_split = inflection.underscore(token).split('_')
if len(camel_split) > 1:
returning_tokens += camel_split
return returning_tokens
def split_camelcase(self):
"""Split CamelCase identifiers"""
for src in self.src_files.values():
src.all_content = self._split_camelcase(src.all_content)
src.comments = self._split_camelcase(src.comments)
src.class_names = self._split_camelcase(src.class_names)
src.attributes = self._split_camelcase(src.attributes)
src.method_names = self._split_camelcase(src.method_names)
src.variables = self._split_camelcase(src.variables)
src.file_name = self._split_camelcase(src.file_name)
src.pos_tagged_comments = self._split_camelcase(src.pos_tagged_comments)
def normalize(self):
"""Removing punctuation, numbers and also lowercase conversion"""
# Building a translate table for punctuation and number removal
punctnum_table = str.maketrans({c : None for c in string.punctuation + string.digits})
for src in self.src_files.values():
content_punctnum_rem = [token.translate(punctnum_table)
for token in src.all_content]
comments_punctnum_rem = [token.translate(punctnum_table)
for token in src.comments]
classnames_punctnum_rem = [token.translate(punctnum_table)
for token in src.class_names]
attributes_punctnum_rem = [token.translate(punctnum_table)
for token in src.attributes]
methodnames_punctnum_rem = [token.translate(punctnum_table)
for token in src.method_names]
variables_punctnum_rem = [token.translate(punctnum_table)
for token in src.variables]
filename_punctnum_rem = [token.translate(punctnum_table)
for token in src.file_name]
pos_comments_punctnum_rem = [token.translate(punctnum_table)
for token in src.pos_tagged_comments]
src.all_content = [token.lower() for token
in content_punctnum_rem if token]
src.comments = [token.lower() for token
in comments_punctnum_rem if token]
src.class_names = [token.lower() for token
in classnames_punctnum_rem if token]
src.attributes = [token.lower() for token
in attributes_punctnum_rem if token]
src.method_names = [token.lower() for token
in methodnames_punctnum_rem if token]
src.variables = [token.lower() for token
in variables_punctnum_rem if token]
src.file_name = [token.lower() for token
in filename_punctnum_rem if token]
src.pos_tagged_comments = [token.lower() for token
in pos_comments_punctnum_rem if token]
def remove_stopwords(self):
"""Removing stop words from tokens"""
for src in self.src_files.values():
src.all_content = [token for token in src.all_content
if token not in stop_words]
src.comments = [token for token in src.comments
if token not in stop_words]
src.class_names = [token for token in src.class_names
if token not in stop_words]
src.attributes = [token for token in src.attributes
if token not in stop_words]
src.method_names = [token for token in src.method_names
if token not in stop_words]
src.variables = [token for token in src.variables
if token not in stop_words]
src.file_name = [token for token in src.file_name
if token not in stop_words]
src.pos_tagged_comments = [token for token in src.pos_tagged_comments
if token not in stop_words]
def remove_python_keywords(self):
"""Removing Python language keywords from tokens"""
for src in self.src_files.values():
src.all_content = [token for token in src.all_content
if token not in python_keywords]
src.comments = [token for token in src.comments
if token not in python_keywords]
src.class_names = [token for token in src.class_names
if token not in python_keywords]
src.attributes = [token for token in src.attributes
if token not in python_keywords]
src.method_names = [token for token in src.method_names
if token not in python_keywords]
src.variables = [token for token in src.variables
if token not in python_keywords]
src.file_name = [token for token in src.file_name
if token not in python_keywords]
src.pos_tagged_comments = [token for token in src.pos_tagged_comments
if token not in python_keywords]
def stem(self):
"""Stemming tokens"""
# Stemmer instance
stemmer = PorterStemmer()
for src in self.src_files.values():
src.all_content = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.all_content],
src.all_content]))
src.comments = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.comments],
src.comments]))
src.class_names = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.class_names],
src.class_names]))
src.attributes = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.attributes],
src.attributes]))
src.method_names = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.method_names],
src.method_names]))
src.variables = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.variables],
src.variables]))
src.file_name = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.file_name],
src.file_name]))
src.pos_tagged_comments = dict(zip(['stemmed', 'unstemmed'],
[[stemmer.stem(token) for token in src.pos_tagged_comments],
src.pos_tagged_comments]))
def preprocess(self):
"""Run the preprocessing"""
self.pos_tagging()
self.tokenize()
self.split_camelcase()
self.normalize()
self.remove_stopwords()
self.remove_python_keywords()
self.stem()
def main():
parser = Parser(DATASET)
src_prep = SrcPreprocessing(parser.src_parser())
src_prep.preprocess()
with open(DATASET.root /'preprocessed_src.pickle', 'wb') as file:
pickle.dump(src_prep.src_files, file, protocol=pickle.HIGHEST_PROTOCOL)
print('Source Preprocessed')
report_prep = ReportPreprocessing(parser.report_parser())
report_prep.preprocess()
with open(DATASET.root / 'preprocessed_reports.pickle', 'wb') as file:
pickle.dump(report_prep.bug_reports, file, protocol=pickle.HIGHEST_PROTOCOL)
print('Report Preprocessed')
if __name__ == '__main__':
main()