-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpan21_train.py
336 lines (261 loc) · 11.5 KB
/
pan21_train.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
import argparse
import pickle
import pandas as pd
import xml.etree.ElementTree as ET
import glob
import re
import os
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import spacy
from sklearn import ensemble
from sklearn.svm import SVC
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
stop_words_en = stopwords.words('english')
stop_words_es = stopwords.words('spanish')
nlpES = spacy.load('es_core_news_sm')
"""
######---------- Data loading and xml writing Functions adopted from Ashraf2019 ------------######
"""
def iter_docs(author):
author_attr = author.attrib
doc_dict = author_attr.copy()
# print(doc_dict)
doc_dict['text'] = [' '.join([doc.text for doc in author.iter('document')])]
return doc_dict
def create_data_frame(input_folder):
os.chdir(input_folder)
all_xml_files = glob.glob("*.xml")
truth_data = pd.read_csv('truth.txt', sep=':::', names=['author_id', 'author'], engine='python')
temp_list_of_DataFrames = []
text_Data = pd.DataFrame()
for file in all_xml_files:
etree = ET.parse(file) # create an ElementTree object
doc_df = pd.DataFrame(iter_docs(etree.getroot()))
doc_df['author_id'] = file[:-4]
temp_list_of_DataFrames.append(doc_df)
text_Data = pd.concat(temp_list_of_DataFrames, axis=0)
data = text_Data.merge(truth_data, on='author_id')
return data
def getArg():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="Input Directory Path", required=True)
parser.add_argument("-o", "--output", help="Ouput Directory Path", required=True)
args = parser.parse_args()
print("input {} output {} ".format(
args.input,
args.output,
))
return args.input, args.output
"""
######---------- Preprocessing Functions English ------------######
"""
# loading hate speech intensity scale
dictionary = {}
with open(os.path.join('bad-hate-dictionary.txt'), 'r') as f:
for line in f:
s = line.strip().split(" ")
dictionary[s[0]] = s[1]
f.close()
# function to replace multiple entries in a line in one go via regex
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
# text preprocessing function
def en_preprocess(data):
corpus = []
for tweets in data:
tweets_lowered = tweets.lower() # lowercase
tweet = multiple_replace(dictionary, tweets_lowered) # hate speech flags
tweet = re.sub(r'\s+[a-z]\s+', ' ', tweet) # remove single characters like i and a
tweet = re.sub(r'^[a-z]\s+', ' ', tweet) # remove single characters at the beginning like i and a
tweet = re.sub(r'\s+', ' ', tweet) # remove extra spaces
tweets_tokenized = word_tokenize(tweet) # tokenize
tweets_no_stopwords = [w for w in tweets_tokenized if w not in stop_words_en] # remove stopwords
corpus.append(' '.join(tweets_no_stopwords))
return corpus
# search and count functions
def face_concerned(text): return len([c for c in text if c in '😕😟🙁☹😮😯😲😳🥺😦😧😨😰😥😢😭😱😖😣😞😓😩😫🥱🙀😿'])
def face_negative(text): return len([c for c in text if c in '😤😡😠🤬😈👿💀☠😾'])
def people(text): return len([c for c in text if c in '👶🧒👦👧🧑👱👨🧔👩🧓👴👵🙍🙍♂️🙎🙅🙆💁🙋🧏🙇🤦🤦♂️🤷🤷♂️'])
def bad_words(text): return len([c for c in text.split() if c == "INSULT_WORD"])
def hate1_counts(text): return len([c for c in text.split() if c == "HATE_LVL1"])
def hate2_counts(text): return len([c for c in text.split() if c == "HATE_LVL2"])
def hate3_counts(text): return len([c for c in text.split() if c == "HATE_LVL3"])
def hate4_counts(text): return len([c for c in text.split() if c == "HATE_LVL4"])
def hate5_counts(text): return len([c for c in text.split() if c == "HATE_LVL5"])
def hate6_counts(text): return len([c for c in text.split() if c == "HATE_LVL6"])
def hateMisc_counts(text): return len([c for c in text.split() if c == "HATE_MISC"])
def user_count(text): return len([c for c in text.split() if c == "user"])
def hashtag_counts(text): return len([c for c in text.split() if c == "hashtag"])
def url_counts(text): return len([c for c in text.split() if c == "http "])
# collective count function
def counters(data):
data['face_concerned'] = data['preprocessed_text'].apply(face_concerned)
data['face_negative'] = data['preprocessed_text'].apply(face_negative)
data['people'] = data['preprocessed_text'].apply(people)
data['bad_words'] = data['preprocessed_text'].apply(bad_words)
data['hate_lvl1'] = data['preprocessed_text'].apply(hate1_counts)
# data['hate_lvl2'] = data['preprocessed_text'].apply(hate2_counts) # no effect on accuracy
data['hate_lvl3'] = data['preprocessed_text'].apply(hate3_counts)
data['hate_lvl4'] = data['preprocessed_text'].apply(hate4_counts)
# data['hate_lvl5'] = data['preprocessed_text'].apply(hate5_counts) # no effect on accuracy
# data['hate_lvl6'] = data['preprocessed_text'].apply(hate6_counts) # no effect on accuracy
data['hate_MISC'] = data['preprocessed_text'].apply(hateMisc_counts)
# data['user_count'] = data['preprocessed_text'].apply(user_count) # no effect on accuracy
# data['hashtag_counts'] = data['preprocessed_text'].apply(hashtag_counts) # no effect on accuracy
data['url_count'] = data['preprocessed_text'].apply(lambda x: len(re.findall('http\S+', x))) # from ashraf2019
data['space_count'] = data['preprocessed_text'].apply(lambda x: len(re.findall(' ', x))) # from ashraf2019
"""
######---------- Preprocessing Functions Spanish ------------######
"""
def es_preprocess(data):
corpus = []
for tweets in data:
tweets_lowered = tweets.lower()
# Further tweet sanitation
tweet = re.sub(r'\s+[a-z]\s+', ' ', tweets_lowered) # remove single characters like i and a
tweet = re.sub(r'^[a-z]\s+', ' ', tweet) # remove single characters at the beginning like i and a
tweet = re.sub(r'\srt\s+', '', tweet) # remove extra spaces
tweet = re.sub(r'#user#', ' #user# ', tweet) # remove extra spaces
tweet = re.sub(r'#url#', '', tweet) # remove extra spaces
tweet = re.sub(r'\s+', ' ', tweet) # remove extra spaces
tokenizedTweet = nlpES(tweet)
processedTweet = []
for l in tokenizedTweet:
processedTweet.append(f"{l.lemma_}")
tweets_no_stopwords = [w for w in processedTweet if w not in stop_words_es]
corpus.append(' '.join(tweets_no_stopwords))
return corpus
"""
######---------- Build model and predict training English/Spanish ------------######
"""
# configurations
en_model = ensemble.RandomForestClassifier(max_features='sqrt', n_estimators=2000, random_state=0)
es_model = SVC(kernel='linear', C=1000)
es_vectorizer = TfidfVectorizer(analyzer='word', token_pattern=r'\w{1,}', ngram_range=(1, 2), max_features=1000,
stop_words=stop_words_es)
# model and prediction function
def model_predict(ml_model, X_train, X_test, y_train, y_test, lang):
model = ml_model
model.fit(X_train, y_train)
y_predict = model.fit(X_train, y_train).predict(X_test)
print(f"{ml_model} on {lang} data:", accuracy_score(model.predict(X_test), y_test))
report_dict = classification_report(y_test, y_predict, output_dict=True)
print(pd.DataFrame(report_dict))
print(confusion_matrix(y_test, y_predict))
pickleModel(model, lang)
return model
# train the models
def train(input_folder, output_folder, lang):
if lang == "en":
input_fold = os.path.join(input_folder, lang)
# loading data
data = create_data_frame(input_fold)
X, y = data['text'], data['author']
# preprocessing
training_data = pd.DataFrame()
training_data['preprocessed_text'] = en_preprocess(X)
counters(training_data)
features = training_data.drop(['preprocessed_text'], axis=1)
# print(features) #print this to see the features
X_train, X_test, y_train, y_test = train_test_split(features, y, test_size=0.2, random_state=0)
# prediction
model = model_predict(en_model, X_train, X_test, y_train, y_test, lang)
else:
input_fold = os.path.join(input_folder, lang)
# loading data
data = create_data_frame(input_fold)
X, y = data['text'], data['author']
# splitting
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# preprocessing
preprocessed_X_train = es_preprocess(X_train)
preprocessed_X_test = es_preprocess(X_test)
# vectorizing
X_train = es_vectorizer.fit_transform(preprocessed_X_train).toarray()
X_test = es_vectorizer.transform(preprocessed_X_test).toarray()
# prediction
model = model_predict(es_model, X_train, X_test, y_train, y_test, lang)
pickleVectorizer(es_vectorizer, 'es')
def pickleModel(model, lang):
print(root)
try:
os.chdir(root)
print('Change current Dir to '+root)
except Exception as e:
print(e)
try:
os.mkdir('models')
print('Make Dir to models')
except Exception as e:
print(e)
try:
os.chdir('models')
print('Change current Dir to models')
except Exception as e:
print(e)
try:
os.mkdir(lang)
print('Make Dir '+lang)
except Exception as e:
print(e)
try:
os.chdir(lang)
print('Change current Dir to '+lang)
except Exception as e:
print(e)
print('writing model')
pickle.dump(model, open('model', 'wb'))
try:
os.chdir(root)
print('Change current Dir to '+root)
except Exception as e:
print(e)
def pickleVectorizer(model, lang):
print(root)
try:
os.chdir(root)
print('Change current Dir to '+root)
except Exception as e:
print(e)
try:
os.mkdir('vectorizers')
print('Make Dir to vectorizers')
except Exception as e:
print(e)
try:
os.chdir('vectorizers')
print('Change current Dir to vectorizers')
except Exception as e:
print(e)
try:
os.mkdir(lang)
print('Make Dir '+lang)
except Exception as e:
print(e)
try:
os.chdir(lang)
print('Change current Dir to '+lang)
except Exception as e:
print(e)
print('writing vectorizer')
pickle.dump(model, open('vectorizer', 'wb'))
try:
os.chdir(root)
print('Change current Dir to '+root)
except Exception as e:
print(e)
def main():
global root
root = os.getcwd()
input_folder, output_folder = getArg()
# input_folder, output_folder = 'D:/pan_data/', 'D:/pan_data/test/output'
train(input_folder, output_folder, 'en')
train(input_folder, output_folder, 'es')
if __name__ == "__main__":
main()
# we need to output and write xml files