-
Notifications
You must be signed in to change notification settings - Fork 0
/
SGID4SE.py
415 lines (332 loc) · 18.5 KB
/
SGID4SE.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
# Copyright Software Engineering Analytics Lab (SEAL), Wayne State University, 2023
# Authors: Sayma Sultana <[email protected]>, Jaydeb Sarker <[email protected]> ,and Amiangshu Bosu <[email protected]>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 3 as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import os.path
import pickle
import numpy as np
import pandas as pd
from imblearn.over_sampling import RandomOverSampler
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, matthews_corrcoef
from sklearn.model_selection import KFold, StratifiedKFold
from ContractionPreprocessor import expand_contraction, rem_special_sym, remove_url
from OverSampler import WordReplaceBasedOversampler, MixedOversampler
from PatternProcessor import PatternTokenizer, IdentifierTokenizer
from CLEModels import CLEModel
from sklearn.metrics import classification_report
import argparse
import warnings
import random
import timeit
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=UserWarning)
def read_dataframe_from_excel(file):
dataframe = pd.read_excel(file)
return dataframe
class SGID4SE:
def __init__(self, ALGO="BERT", model_file="models/SGID-dataset-full.xlsx", split_identifier=True,
remove_keywords=True, load_pretrained=False, count_words =False):
self.classifier_model = None
self.modelFile = model_file
self.split_identifier = split_identifier
self.remove_keywords = remove_keywords
self.source_code_checker = IdentifierTokenizer()
self.pattern_checker = PatternTokenizer()
self.ALGO = ALGO
self.training_data = read_dataframe_from_excel(model_file)
self.load_pretrained = load_pretrained
self.count_words = count_words
def preprocess(self, dataframe):
dataframe["message"] = dataframe.message.astype(str).apply(self.process_text)
if self.count_words:
dataframe["role_reference"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_women_roles)
dataframe["women_kin_reference"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_women_kins_reference)
dataframe["lgbtq_reference"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_lgbtq_reference)
dataframe["pejorative_count"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_pejoratives)
dataframe["appearance_reference"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_appearance_reference)
dataframe["women_body_part_reference"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_women_body_parts)
dataframe["women_cloth_reference"] = dataframe.message.astype(str). \
apply(self.pattern_checker.count_women_clothes)
else:
dataframe["role_reference"] =0
dataframe["women_kin_reference"] = 0
dataframe["lgbtq_reference"] = 0
dataframe["pejorative_count"] = 0
dataframe["appearance_reference"] = 0
dataframe["women_body_part_reference"] = 0
dataframe["women_cloth_reference"] = 0
def get_training_data(self):
self.preprocess(self.training_data)
return self.training_data
def __get_pretrained_model(self):
return True
def process_text(self, text):
processed_text=text
# mandatory preprocessing
processed_text = remove_url(processed_text)
processed_text = expand_contraction(processed_text)
processed_text = self.pattern_checker.process_text(processed_text)
processed_text = rem_special_sym(processed_text)
# optional preprocessing
if self.split_identifier:
processed_text = self.source_code_checker.split_identifiers(processed_text)
if self.remove_keywords:
processed_text = self.source_code_checker.remove_keywords(processed_text)
return processed_text
def init_predictor(self, strategy ="random", ratio =1):
if self.load_pretrained:
filename = self.getPTMName()
loadstatus = self.load_pretrained_model(filename)
if loadstatus:
print("Successfully loaded pretrained model from "+filename)
return
else:
print("Unable to load pretrained model.."+filename)
self.__train_predictor( strategy, ratio)
def getPTMName(self):
ALGO=self.ALGO
filename = "./pre-trained/model-" + ALGO \
+ "-keyword-" + str(self.remove_keywords) + "-split-" \
+ str(self.split_identifier)
if ((ALGO == "CNN") | (ALGO == "LSTM") | (ALGO == "GRU") ):
filename = filename + ".h5"
elif((ALGO =="BERT") | (ALGO =="ALBERT")| (ALGO =="SBERT")):
filename = filename + ".h5"
elif ((ALGO == "RF") | (ALGO == "SVM") | (ALGO == "DT") | (ALGO == "LR") | (ALGO == "XGB") ):
filename = filename + ".pickle"
return filename
def __train_predictor(self, strategy ="random", ratio =1 ):
self.preprocess(self.training_data)
X_train = self.training_data[["message", "role_reference","women_kin_reference","lgbtq_reference",
"pejorative_count","appearance_reference", "women_body_part_reference",
"women_cloth_reference", "CommentID"]]
Y_train = self.training_data[['target']]
# train model using full dataset
if strategy=="random":
oversampler = RandomOverSampler(sampling_strategy=ratio)
elif strategy=="generate":
oversampler =WordReplaceBasedOversampler(sampling_strategy=ratio)
elif strategy=="mixed":
oversampler =MixedOversampler(sampling_strategy=ratio)
X_train, Y_train = oversampler.fit_resample(X_train, Y_train)
self.get_model(X_train, Y_train)
def save_trained_model(self):
ALGO = self.ALGO
filename = self.getPTMName()
if ((ALGO == "BERT") | (ALGO == "CNN") | (ALGO == "LSTM") | \
(ALGO == "GRU") | (ALGO == "ALBERT")| (ALGO == "SBERT")):
self.classifier_model.save_to_file(filename)
elif ((ALGO == "RF") | (ALGO == "SVM") | (ALGO == "DT") | (ALGO == "LR")):
pickle.dump(self.classifier_model, open(filename, "wb"))
print("Model stored as: "+filename)
def load_pretrained_model(self, filename):
if not os.path.exists(filename):
print("File: "+ filename +" not exists!")
return False
if filename.endswith(".pickle"):
self.classifier_model = pickle.load(open(filename, "rb"))
return True
ALGO = self.ALGO
try:
if ((ALGO == "CNN") | (ALGO == "GRU") | (ALGO == "LSTM")):
import DNNModels
self.classifier_model = DNNModels.DNNModel(algo=ALGO, load_from_file=filename)
return True
elif ((ALGO == "BERT" )| (ALGO == "ALBERT") | (ALGO == "SBERT")):
from TransformerModel import TransformerModel
self.classifier_model = TransformerModel(load_from_file=filename, model_name=ALGO)
return True
except Exception as e:
print(e)
return False
def get_model(self, X_train, Y_train, minority_bias=1):
ALGO = self.ALGO
if (ALGO == "RF") | (ALGO == "SVM") | (ALGO == "DT") | (ALGO == "LR") | (ALGO =="XGB"):
self.classifier_model = CLEModel(X_train=X_train, Y_train=Y_train, algo=self.ALGO, minority_bias= minority_bias,)
elif (ALGO == "BERT") | (ALGO == "ALBERT") | (ALGO == "SBERT") :
from TransformerModel import TransformerModel
self.classifier_model = TransformerModel(X_train=X_train, Y_train=Y_train, model_name=ALGO,
minority_bias=minority_bias)
elif (ALGO == "CNN") | (ALGO == "LSTM") | (ALGO == "GRU") :
import DNNModels
self.classifier_model = DNNModels.DNNModel(X_train=X_train,
Y_train=Y_train, minority_bias= minority_bias, algo=ALGO )
else:
print("Unknown algorithm: "+ALGO)
exit(1)
return self.classifier_model
def get_SGID_probablity(self, texts):
dataframe = pd.DataFrame(texts, columns=['message'])
self.preprocess(dataframe)
#print(dataframe)
results = self.classifier_model.predict(dataframe)
return results
def get_detail_classifications(dataframe, labels, Y_prob, predictions):
predictions = pd.DataFrame(data=predictions, columns=["predicted"])
probabilities = pd.DataFrame(data=Y_prob, columns=["probability"])
newdf = dataframe.reset_index(drop=True)
labels_reset = labels.reset_index(drop=True)
merged_df = pd.concat([newdf, predictions], axis=1)
merged_df = pd.concat([merged_df, labels_reset], axis=1)
merged_df = pd.concat([merged_df, probabilities], axis=1)
return merged_df
def binary_class_by_threshold(predicttion_probablities, threshold=0.5):
predictions =[1 if pred >= threshold else 0 for pred in predicttion_probablities]
return predictions
def ten_fold_cross_validation(amiClassifier, oversample ="random", ratio=0, bias=1, threshold_vary =False, rand_state =55):
dataset = amiClassifier.get_training_data()
kf = StratifiedKFold(n_splits=10, shuffle=True, random_state=rand_state)
results = ""
count = 1
all_predictions = pd.DataFrame()
for train_index, test_index in kf.split(dataset, dataset["target"]):
start = timeit.default_timer()
print("Using split-" + str(count) + " as test data..")
X_train, X_test = dataset.loc[train_index, ["message", "role_reference","women_kin_reference","lgbtq_reference",
"pejorative_count","appearance_reference", "women_body_part_reference",
"women_cloth_reference", "CommentID"]], \
dataset.loc[test_index, ["message", "role_reference","women_kin_reference","lgbtq_reference",
"pejorative_count","appearance_reference", "women_body_part_reference",
"women_cloth_reference", "CommentID"]]
Y_train, Y_test = dataset.loc[train_index, "target"], dataset.loc[test_index, "target"]
#if append:
# X_extra =extra_train_df[["message"]]
# Y_extra =extra_train_df[["target"]].squeeze()
# X_train =pd.concat([X_train,X_extra], axis=0, ignore_index=True)
# Y_train =pd.concat([Y_train, Y_extra], axis=0, ignore_index=True)
if ratio!=0:
strategy =1.0/ratio
#oversampler = RandomOverSampler(sampling_strategy=ratio)
if oversample=="random":
oversampler = RandomOverSampler(sampling_strategy=strategy)
elif oversample=="generate":
oversampler =WordReplaceBasedOversampler(sampling_strategy=strategy)
elif oversample=="mixed":
oversampler =MixedOversampler(sampling_strategy=strategy)
X_train, Y_train = oversampler.fit_resample(X_train, Y_train) #resampling only the training data
classifier_model = amiClassifier.get_model(X_train, Y_train, minority_bias=bias)
Y_prob = classifier_model.predict(X_test)
if not threshold_vary:
results = results + str(count) + "," + ALGO + ","
predictions =binary_class_by_threshold(Y_prob)
detail_Classification = get_detail_classifications(X_test, Y_test, Y_prob, predictions)
stop = timeit.default_timer()
time_elapsed = stop - start
if len(all_predictions.columns) == 0:
all_predictions = detail_Classification
else:
all_predictions = pd.concat([all_predictions, detail_Classification], axis=0)
precision_1 = precision_score(Y_test, predictions, pos_label=1)
recall_1 = recall_score(Y_test, predictions, pos_label=1)
f1score_1 = f1_score(Y_test, predictions, pos_label=1)
precision_0 = precision_score(Y_test, predictions, pos_label=0)
recall_0 = recall_score(Y_test, predictions, pos_label=0)
f1score_0 = f1_score(Y_test, predictions, pos_label=0)
accuracy = accuracy_score(Y_test, predictions)
mcc = matthews_corrcoef(Y_test, predictions)
results = results + str(precision_0) + "," + str(recall_0) + "," + str(f1score_0)
results = results + "," + str(precision_1) + "," + str(recall_1) + "," + str(f1score_1) + \
"," + str(accuracy) + "," + str(mcc) + "," + str(time_elapsed) + "\n"
print(classification_report(Y_test, predictions))
count += 1
else:
best_threshold=0.05
best_fscore=0.00
best_mcc=0.00
best_mcc_threshold=0.00
for probablity in np.arange(0.01,1.00, 0.01):
predictions =binary_class_by_threshold(Y_prob, probablity)
precision_1 = precision_score(Y_test, predictions, pos_label=1)
recall_1 = recall_score(Y_test, predictions, pos_label=1)
f1score_1 = f1_score(Y_test, predictions, pos_label=1)
precision_0 = precision_score(Y_test, predictions, pos_label=0)
recall_0 = recall_score(Y_test, predictions, pos_label=0)
f1score_0 = f1_score(Y_test, predictions, pos_label=0)
accuracy = accuracy_score(Y_test, predictions)
mcc = matthews_corrcoef(Y_test, predictions)
results = results + str(count) +"," + str(precision_0) + "," + str(recall_0) + "," + str(f1score_0)
results = results + "," + str(precision_1) + "," + str(recall_1) + "," + str(f1score_1) + \
"," + str(accuracy) + "," + str(mcc) + "," + str(probablity) + "\n"
if f1score_1>best_fscore:
best_threshold=probablity
best_fscore=f1score_1
if best_mcc<mcc:
best_mcc_threshold=probablity
best_mcc=mcc
print("Best f1 : "+ str(best_fscore)+ "at threshold: "+str(best_threshold))
print("Best mcc: "+ str(best_mcc)+ "at threshold: "+str(best_mcc_threshold))
count += 1
return (results, all_predictions)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='SGID4SE: Sexual orientation and Gender identity Discriminatory\n'+
' Text Detector for the SE domain')
parser.add_argument('--algo', type=str,
help='Classification algorithm. Choices are: RF| DT| SVM| LR| CNN| LSTM| GRU| BERT| ALBERT| SBERT',
default="RF")
parser.add_argument('--oversample', type=str, help='Oversampling strategy: '+
'random | generate | mixed', default="random")
parser.add_argument('--ratio', type=int, help='Oversample minority class before training. Argument: '+
'majority/minority ratio', default=1)
parser.add_argument('--bias', type=int, help='Class weighting bias' +
'Minority multiply factor', default=1)
parser.add_argument('--repeat', type=int, help='Iteration count', default=1)
parser.add_argument('--split', help='Split identifiers', action='store_false', default=True)
parser.add_argument('--wc', help='Count word features', action='store_true', default=False)
parser.add_argument('--vary', help='Experment threshold variation', action='store_true', default=False)
parser.add_argument('--keyword', help='Remove programming keywords', action='store_false', default=True)
parser.add_argument('--retro', help='Print missclassifications',
action='store_true', default=False) # default False, will not write
parser.add_argument('--mode', type=str,
help='Execution mode. Choices are: eval | pretrain ',
default="eval")
args = parser.parse_args()
print(args)
ALGO = str(args.algo).upper()
REPEAT = args.repeat
mode = args.mode
oversample=args.oversample
ratio=args.ratio
bias = args.bias
wordcount=args.wc
threshold_vary =args.vary
amiClassifier = SGID4SE(split_identifier=args.split, remove_keywords=args.keyword,
ALGO=ALGO, count_words=wordcount)
if mode == 'pretrain':
amiClassifier.init_predictor()
amiClassifier.save_trained_model()
exit(0)
timers = []
filename = "results/cross-validation-" +str(ALGO) + "-oversample-" + str(args.oversample)+"-ratio-" \
+ str(args.ratio) +"-bias-" + str(args.bias) +"-wc-" +str(args.wc) + ".csv"
if args.vary:
filename = "results/vary-evaluation-" +str(ALGO) + "-oversample-" + str(args.oversample)+"-ratio-" \
+ str(args.ratio) +"-bias-" + str(args.bias) +"-wc-" +str(args.wc) + ".csv"
training_log = open(filename, 'w')
if threshold_vary:
training_log.write("Fold,precision_0,recall_0,f-score_0,precision_1,recall_1,f-score_1,accuracy,mcc,threshold\n")
else:
training_log.write("Fold,Algo,precision_0,recall_0,f-score_0,precision_1,recall_1,f-score_1,accuracy,mcc,time\n")
random.seed(200)
for k in range(0, REPEAT):
print(".............................")
print("Run# {}".format(k))
(results, misclassified) = ten_fold_cross_validation(amiClassifier, oversample,ratio, bias, threshold_vary, random.randint(1, 10000))
training_log.write(results)
training_log.flush()
if (args.retro & (k == 0)):
misclassified.to_excel("results/"+ str(ALGO) + "-oversample-" + str(args.oversample)+
"-ratio-" + str(args.ratio)
+ "_misclassified.xlsx")
##########################
training_log.close()